$(document).ready(function() {
	if ($('.cake-sql-log').length > 0) {
		$('.cake-sql-log').hide();
		$('<a href="#">Toggle Sql Log<\/a>')
			.insertBefore($('.cake-sql-log'))
			.click(function() {
				$('.cake-sql-log').toggle();
				return false;
			});
	}

	window.setTimeout(function() {
		$('ul.messages').fadeOut(function() {
			$('ul.messages').remove();
		});
	}, 5000);


	if (client_slug != '') {
		$('h1 > a', '#nav').click(function() {
			var clicked_division_slug = $(this).attr('id').replace('division_', '');
			$('#clients_' + clicked_division_slug).slideToggle(500);
			if (clicked_division_slug == division_slug) {
				$('.current_artist').slideToggle(500);
			}
		});
	}
});

function setupPortfolio() {
	// 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 = 1;
	$('#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:                     3500,
		numThumbs:                 8,
		preloadAhead:              8,
		enableTopPager:            false,
		enableBottomPager:         true,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		navContainerSel:           '#controls2',
		renderSSControls:          false,
		renderNavControls:         false,
		playLinkText:              'Play Slideshow',
		pauseLinkText:             'Pause Slideshow',
		prevLinkText:              '',
		nextLinkText:              '',
		nextPageLinkText:          '',
		prevPageLinkText:          '',
		enableHistory:             true,
		autoStart:                 false,
		syncTransitions:           true,
		defaultTransitionDuration: 750,
		onSlideChange:             function(prevIndex, nextIndex) {
			var nextThumb = $('#thumb' + nextIndex);

			if (nextThumb.is('.video')) {
				$('#still').hide();
				$('#video').show();
				$('#prev-area').hide();
				$('#next-area').hide();

				var img = $('img', nextThumb);

				jwplayer("videoholder").setup({
					width: 720,
					height: 530,
					file: img.attr('rel'),
					image: img.attr('src'),
					skin: "/skin/chrisboalsartists/chrisboalsartists.xml",
					players: [
						{type: "flash", src: "/player.swf"},
						{type: "html5"}
					]
				});
			} else {
				$('#still').show();
				$('#video').hide();
				$('#prev-area').show();
				$('#next-area').show();
			}

			// '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);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			var prevPageLink = this.find('a.prev').css('visibility', 'visible');
			var nextPageLink = this.find('a.next').css('visibility', 'visible');
			var prevPageLink2 = this.find('a.prevLink').css('visibility', 'visible');
			var nextPageLink2 = this.find('a.nextLink').css('visibility', 'visible');
			// Show appropriate next / prev page links
			if (this.displayedPage > 0){
				prevPageLink.css('visibility', 'visible');
				prevPageLink2.css('visibility', 'visible');
			}
			var lastPage = this.getNumPages() - 1;
			if (this.displayedPage < lastPage){
				nextPageLink.css('visibility', 'visible');
				nextPageLink2.css('visibility', 'visible');
			}
			this.fadeTo('fast', 1.0);
		}
	});

	$('#thumbs a').click(function(e) {
		var i = $(this).parent().attr('id').replace('thumb', '');
		if (i) {
			e.preventDefault();
			gallery.gotoIndex(i);
		}
	});

	/**************** Event handlers for custom next / prev page links **********************/

	gallery.find('a.prev').click(function(e) {
		gallery.previousPage();
		e.preventDefault();
	});
	

	gallery.find('a.next').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) {
		// alert("pageload: " + hash);
		// hash doesn't contain the first # character.
		if(hash) {
			$.galleriffic.gotoImage(hash);
		} else {
			gallery.gotoIndex(0);
		}
	}

	// 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

	/****************************************************************************************/
	$('#prev-area').mouseout(function(){
		$('#prevcursor').hide();
		return false;
	});
	$('#prev-area').mouseenter(function(){
		$('#prevcursor').show();
		return false;
	});

	$('#prev-area').mousemove(function(e){
		$('#prevcursor').css('left', e.clientX - 20).css('top', e.clientY + 7);
	});

	$('#next-area').mouseout(function(){
		$('#nextcursor').hide();
		return false;
	});
	$('#next-area').mouseenter(function(){
		$('#nextcursor').show();
		return false;
	});

	$('#next-area').mousemove(function(e){
		$('#nextcursor').css('left', e.clientX - 20).css('top', e.clientY + 7);
	});
}

function opendiv(id, lnkid) {
	$('#' + id).slideToggle(750);
	if ($('#' + id + ':visible')) {
		$('#' + lnkid).removeClass('share-slide').addClass('active');
	} else {
		$('#' + lnkid).removeClass('active').addClass('share-slide');
	}
}

function selectedimages(namestr) {
	var arr_nam = namestr.split(" ");
	//alert(arr_nam[0]);
	var fname = arr_nam[0];

	document.getElementById('selected_img').style.display = "inline";
	document.getElementById('mixgal').style.display = "none";
	document.getElementById('0_' + fname).className = 'show_'+fname;
	document.getElementById('1_' + fname).className = 'show_'+fname;
	document.getElementById('2_' + fname).className = 'show_'+fname;
}

function deselectimage(namestr) {
/*
	var arr_nam = namestr.split(" ");
	//alert(arr_nam[0]);
	var fname = arr_nam[0];

	//document.getElementById('selected_img').style.display = "inline";
	//document.getElementById('mixgal').style.display = "none";
	document.getElementById('0_' + fname).className = 'hide_'+fname;
	document.getElementById('1_' + fname).className = 'hide_'+fname;
	document.getElementById('2_' + fname).className = 'hide_'+fname;
	*/
}

