/**
 * @author J.Oliemans
 */



$(document).ready(function()
{	
	if( $('div.polaroid').length > 0 )
	{
		getFLickrImages();
	}
	
	if( $('#artistic').length > 0 )
	{
		startLightBox();
	}
	
	if(  $('ul#portfolioMenu').length > 0 )
	{
		//setPortfolioMenu();
		// click the first button to load content
		//$('a#webdeveloping').click();
		startAccordion();
	}
	
	// set hover animation on links
	if(  $('ul#menu').length > 0 )
	{
		setHoverAnimation( $('ul#menu a') );
	}
	if(  $('ul#portfolioMenu').length > 0 )
	{
		setHoverAnimation( $('ul#portfolioMenu a') );
	}
});

function startLightBox()
{
	 $('#artistic a').lightBox(
	 {
	 	overlayBgColor: '#E4E4E4',
		overlayOpacity: 0.6,
	 	imageLoading: 'includes/img/lightbox-ico-loading.gif',
		imageBtnClose: 'includes/img/lightbox-btn-close.gif',
		imageBtnPrev: 'includes/img/lightbox-btn-next.gif',
		imageBtnNext: 'includes/img/lightbox-btn-prev.gif'
	 }
	 );
}

function setHoverAnimation( linkArray )
{
	//console.log( linkArray );
	for( var i = 0; i< linkArray.length; i++ )
	{
		$(linkArray[i]).hover(function() 
		{
	        $el = $(this);
	        $el.animate({ color: "#ff0000" }, 500);
	
	    }, function() {
	
	        $el = $(this);
	        $el.stop().animate({ color: "#000000" }, 500);
	    });

	}
}

function setPortfolioMenu()
{
	$('a#webdeveloping').bind("click", portFolioClickHandler );
	$('a#webdesign').bind("click", portFolioClickHandler );
	$('a#artistic').bind("click", portFolioClickHandler );
}

function portFolioClickHandler(e)
{
	var clickedButton = $(this).attr( 'id' );
	var xmlPath = $(this).attr( 'href' );
	var xslPath;
	
	// get the right xsl
	switch(clickedButton)
	{
	case "webdeveloping":
	  xslPath = "includes/portfolio/xslt/wowebdevelop.xsl";
	  break;
	case "webdesign":
	  xslPath = "includes/portfolio/xslt/wowebdesign.xsl";
	  break;
  	case "artistic":
	  xslPath = "includes/portfolio/xslt/woartistic.xsl";
	  break;
	default:
	  alert( "button is not regocnized" );
	}
	// get xml and xsl
	$('#portfolioXml').xslt( xmlPath, xslPath);		
	return false;
}




function getFLickrImages()
{
	$.getJSON("http://flickr.com/services/rest/?method=flickr.photosets.getPhotos&user_id=49293629@N05&api_key=2acd8ed8c7441cfcc080402f9e852f26&photoset_id=72157624280091024=&format=json&jsoncallback=?",
	    function(data)
		{
			var flickrObject = new Array();	
		   	var imgPaths = new Array();
			var linkPaths = new Array();
		   	flickrObject = data.photoset.photo;
			
			for( var i = 0; i < flickrObject.length; i++ )
			{
				
				var imgSrc = "http://farm" + flickrObject[i].farm + ".static.flickr.com/" + flickrObject[i].server + "/" + flickrObject[i].id + "_" + flickrObject[i].secret + "_m.jpg";
				var linkPath= "http://www.flickr.com/photos/jeroenoliemans/" + flickrObject[i].id;
				imgPaths.push( imgSrc );
				linkPaths.push( linkPath );
			}
			for (var j = 0; j < imgPaths.length; j++) 
			{
				$("<img/>").attr("src", imgPaths[j]).appendTo("#images").wrap("<a href='" + linkPaths[j] + "'></a>");
			}
			
			startSlideShow();
    	});
}


	function startSlideShow()
	{
		//remove preloader
		$( "#images" ).css( "background", "none" );	
		
		$('#images').cycle({ 
	    prev:   '#prev', 
	    next:   '#next', 
	    timeout: 3000 
	});
	}

function startAccordion()
{
	//start slideshows
	var slideShowEl = $('.acc_container').find( '.slideshow' );
	
	slideShowEl.cycle({  
	    timeout: 4000,
		pause: true 
	});


	
	
	//Set default open/close settings
	$('.acc_container').hide(); //Hide/close all containers
	$('.acc_header:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
	
	
	//On Click
	$('.acc_header').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('.acc_header').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
		}
		return false; //Prevent the browser jump to the link anchor
	});
	
	// set hover animation
	setAccordionHoverAnimation();
}

	function setAccordionHoverAnimation()
	{
		$("#portfolioXml h3").hover(
		    function(){
		        $(this).animate({ 	color: "#000000", 
									backgroundColor: "#ff0000", 
									borderBottomColor: "#ff0000", 
									borderTopColor: "#ff0000",
									borderLeftColor: "#ff0000",
									borderRightColor: "#ff0000"
									}, 500);
		    }, 
		    function(){
		        $(this).stop().animate({ 	color: "#ffffff", 
									backgroundColor: "#000000", 
									borderBottomColor: "#000000",
									borderTopColor: "#000000",
									borderLeftColor: "#000000",
									borderRightColor: "#000000", 
									}, 500);
		    });

	}


