/* resets the state of the menu and workspace */
function reset_workspace() {
	$('.menuitem').removeClass('selected');
	$('.dropdownmenu').hide();
	$('.contentItem').hide();
}

function hide_rollovers() {
	$('.menuitem.selected .dropdownmenu').slideUp().parent().removeClass('selected');
}

function show_content(section) {
	reset_workspace();
	$('#menu' + section).addClass('selected');
	$('#content' + section).fadeIn(350);
    $(".selected .border").css({width : '25%'}).animate({ width: "100%", opacity: 1}, 300);
}

function show_dropdown(section) {
	if (!$('#menu' + section).hasClass('selected')) {
		reset_workspace();
		$('#menu' + section).addClass('selected');
		$('#menu' + section + ' .dropdownmenu').slideDown('fast', function() {
			$('.dropdownmenu').not('.selected .dropdownmenu').hide();
			$('#recentWork').stop(true, true).hide();
			$('#recentWorkTerm').stop(true, true).hide();			
		});
	}
}

function hookup_home_page() {
	// fade in  logo
	$('#logo').fadeIn(750);

	// fade in menu items
	$('#menu').fadeTo(750,1,function() {
		$('#menu').fadeIn(1000);
	});

	// fade in borders
	$('.border').fadeTo(750,1,function() {
		$(".border").animate({ width: "100%", opacity: 1}, 1000);
	});

	$('#menuPhotographers').unbind('mouseover').bind('mouseover', function() {
		show_dropdown('Photographers');
	});
	
	$('#menuStylists').unbind('mouseover').bind('mouseover', function() {
		show_dropdown('Stylists');
	});
	
	// hover on the li to animate bg color and show subnav
	$('.dropdownmenu li').hover(function() {
		$(this).children('a').animate({backgroundColor:'#ffffff', color:'#a4a698'}, 300);
		$(this).children('ul.subnav').show();
	}, function() {
		$(this).children('a').animate({backgroundColor:"#f0037f", color:"#ffffff"}, 300);
		$(this).children('ul.subnav').hide();
	});
	
	// hover on subnav item to animate bg color
	$('ul.subnav li a').hover(function() {
		$(this).animate({backgroundColor:"#ffffff", color:"#a4a698"}, 300);
	}, function() {
		$(this).animate({backgroundColor:"#f0037f", color:"#ffffff"}, 300);
	});
}

$(document).ready(function(){
	hookup_home_page();

	var tab = window.location.hash;

	if (tab == '#About' || tab == '#Contact') {
		reset_workspace();
		show_content(tab.replace('#', ''));
		// $('#recentWork').hide();
		// $('#recentWorkTerm').hide();
	} else {
		//For fade in Recent Work text and image
		$('#recentWork').fadeTo(1500,1,function() {
			$('#recentWork').fadeIn(2000);
		});
		$('#recentWorkTerm').fadeTo(1500,1,function() {
					$('#recentWorkTerm').fadeIn(2000);
		});
	}

	//For portfolio in Border
	$("#portfolioBorderAnimated").animate({ width: "100%", opacity: 1}, 1000);
});

window.onload = function() {
	//initialiseStateFromURL();
}

function initialiseStateFromURL() {
	var tab = window.location.hash.split('/');

	if(tab[0] == '#Photographers') {
		if(tab.length >= 2) {
			get_photographers(tab[1], '', 'ajax_url');
			$("body").switchClass('homebg', 'innerPage', 750);
		}
		if (tab.length >= 3) {
			setTimeout("get_photographers_gallery('"+tab[1]+"', '', '"+tab[2]+"', 'ajax_url')", 850);
		}
	} else if (tab[0] == '#Stylists') {
		if(tab.length >= 2) {
			get_stylist_pro(tab[1], '', 'ajax_url');
			$("body").switchClass('homebg', 'innerPage', 750);
		}
		if (tab.length >= 3) {
			setTimeout("get_photographers_gallery('"+tab[1]+"', '', '"+tab[2]+"', 'ajax_url')", 950);
		}
	} else if (tab[0] == '#News') {
		setTimeout("show_content('News');", 1800);
	} else if (tab[0] == '#About') {
		setTimeout("show_content('About');", 1800);
	} else if (tab[0] == '#Contact') {
		setTimeout("show_content('Contact');", 1800);
	}
}

function chkObject(inParent,theVal) {
	if (inParent) {
		return (window.opener.document.getElementById(theVal) != null);
	} else {
		return (document.getElementById(theVal) != null);
	}
}

var intr;
function cap_pos(){
	if(chkObject("",'captionContainer') && chkObject("",'spanCaption')) {
		intr = setInterval(function() {
			document.getElementById("captionContainer").style.display = "block";
			document.getElementById("spanCaption").style.display = "block";
		caption_pos(); }, 1500);
		document.getElementById("spanCaption").style.opacity = 1;
	}
}

function caption_pos() {
	widthSlideshow = ($("#slideshow img").width());
	heightSlideshow = ($("#slideshow img").height());

	document.getElementById("caption").style.marginTop = heightSlideshow + "px";  
	document.getElementById("captionContainer").style.width = widthSlideshow + "px";  

	clearInterval(intr);
}

function make_li_active(portfolio_name) {
	$('#linksPortfolio li a').each(function(i) {
		if (this.title == portfolio_name) {
			$(this).attr('class', 'active');
		} else {
			$(this).attr('class', '');
		}
	});
}

function initializePortfolioGallery() {
	$('#left_controls').html('<div class="controls" id="controls"></div><div class="showthumbnails"><a href="javascript://" id="showThumbnails">Show Thumbnails</a></div><br>');
	prash();
}

function empty_hash() {
	window.location.hash = "";
}

function show_bio() {
	$('#left_controls').html('');
	$('#downloadPdf').html('');
	$('#portfolioContentImage').html($('#artist_bio').val());
}

function refresh_page(artist_name, portfolio_id, portfolio_name, referenceFrom) {
	if(referenceFrom!='ajax_url')
	{
		var initialTab = window.location.hash;
		var tab1; var tab;
		tab=initialTab.split('/');
		if(tab[0]!='')
		{
			if(tab.length>=1)
			{
				cat_name=tab[0].split('#');
				//alert(cat_name[1]);
			}
		}
		window.location.hash = cat_name[1]+'/'+tab[1]+'/'+portfolio_name;
		location.reload(true);
	}
}

function prash() {
	startgallery();

	// We only want these styles applied when javascript is enabled
	$('div.content').css('display', 'block');

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.35;
	$('#thumbs ul.thumbs li, div.navigation a.pageLink').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'slow',
		exemptionSelector: '.selected'
	});

	// Initialize Advanced Galleriffic Gallery
	var gallery = $('#thumbs').galleriffic({
		delay:                     5000,
		numThumbs:                 6,
		preloadAhead:              3,
		enableTopPager:            false,
		enableBottomPager:         false,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          true,
		renderNavControls:         true,
		playLinkText:              'Slide show',
		pauseLinkText:             'Pause',
		prevLinkText:              'Previous&nbsp;',
		nextLinkText:              'Next',
		nextPageLinkText:          '',
		prevPageLinkText:          '',
		enableHistory:             false,
		autoStart:                 true,
		syncTransitions:           false,
		defaultTransitionDuration: 750,
		onSlideChange:             function(prevIndex, nextIndex) {
			// 'this' refers to the gallery, which is an extension of $('#thumbs')
			this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);

			// Update the photo index display
			this.$captionContainer.find('div.photo-index')
				.html(nextIndex+1 +'/'+ this.data.length);

			//trackPageview(prevIndex, nextIndex);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			var prevPageLink = this.find('a.prevLink').css('visibility', 'hidden');
			var nextPageLink = this.find('a.nextLink').css('visibility', 'hidden');

			// Show appropriate next / prev page links
			if (this.displayedPage > 0)
				prevPageLink.css('visibility', 'visible');

			var lastPage = this.getNumPages() - 1;
			if (this.displayedPage < lastPage)
				nextPageLink.css('visibility', 'visible');

			this.fadeTo('fast', 1.0);
		}
	});

	$('a.prevLink').click(function(e) {
		gallery.previousPage();
		e.preventDefault();
	});

	$('a.nextLink').click(function(e) {
		gallery.nextPage();
		e.preventDefault();
	});

	/**** Functions to support integration of galleriffic with the jquery.history plugin ****/

	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
			$.galleriffic.gotoImage(hash);
		} else {
			gallery.gotoIndex(1);
		}
	}

	$("#showThumbnails").click(function() {
		if ($('#showThumbnails').text() == 'Show Thumbnails') {
			$('#showThumbnails').text('Hide Thumbnails');
			$(".thumbs").fadeIn(500);
			$(".nav-controls2").fadeIn(500);
		} else {
			$('#showThumbnails').text('Show Thumbnails');
			$(".thumbs").fadeOut(500);
			$(".nav-controls2").fadeOut(500);
		}
	 });

	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	$.historyInit(pageload, "advanced.html");

	// set onlick event for buttons using the jQuery 1.3 live method
	$("a[rel='history']").live('click', function(e) {
		if (e.button != 0) return true;

		var hash = this.href;
		hash = hash.replace(/^.*#/, '');

		// moves to a new page.
		// pageload is called at once.
		// hash don't contain "#", "?"
		$.historyLoad(hash);

		return false;
	});
}
