var imgdata=[];
var rows=0;
var cols=0;
var r=3;
var htmlx="<div id='plate' style='position:relative; top:0px; left:0px;'>\n";
var counter=0;
var max_counter=0;
var intervalID=null;
var duration=6500;
var autoplay=true;
var scrollamount=438;
var inbetween=false;
var playing=true;
var playSource="portfolio-short-cut-assets/portfolio_short-cut_arrow_play.jpg"
var pauseSource="portfolio-short-cut-assets/portfolio_short-cut_arrow_pause.jpg"

$(document).ready(function(n){
						   

							   
						   
						   
						   max_counter = Math.ceil($('.portfoilo-content-holder .thumb_container').find('li').length/6)
						   $('.bottom-controller').find('span').eq(0).text("page "+Number(counter+1)+" of "+max_counter);
						   $('.bottom-controller img').bind('click',arrowFunctions)
						   $('.portfoilo-content-holder .thumb_container').find('li').each(function(n){
																
							rm = n % 2;
							
							if(n>0){
							if(rm==0)
							   {
								  cols++;
								  rows=0;
							   }
							   else
							   {
								rows++;   
							   }}
							
							style = "top:"+(rows*100)+"px; left:"+(cols*146)+"px;";
							htmlx = htmlx+"<div style='"+style+"' id='th_"+n+"' class='thumbnail'>";
							htmlx = htmlx+"<img src='"+$(this).find('img').eq(0).attr('src')+"' rel='"+$(this).find('a').eq(0).attr('href')+"'/>";
							htmlx = htmlx+"<div class='description-text'>"+$(this).find('img').eq(0).attr('alt')+"</div>"
							htmlx = htmlx+"</div>";
							
																								
																									
																									
																									  });
						   
						   htmlx = htmlx+"\n</div>";
						   
						   $('.portfoilo-content-holder .thumb_container').html(htmlx);
						  
						   $('.portfoilo-content-holder .thumb_container').find('.description-text').hide();
						   $('.portfoilo-content-holder .thumb_container').find('.description-text').css('opacity',0.9);
						   $('.portfoilo-content-holder .thumb_container').find('.thumbnail').bind('mouseover',thumbnailEffect);
						   $('.portfoilo-content-holder .thumb_container').find('.thumbnail').bind('mouseleave',thumbnailEffect);
						   $('.portfoilo-content-holder .thumb_container').find('.thumbnail').bind('click',thumbnailEffect);
						   
						   if(autoplay)
						   {
							  playing = true;
							  intervalID = setInterval(slideshow,duration);
							  
						   }
						   
						   });


function thumbnailEffect(e)
{
	
	switch(e.type)
	{
	   case "mouseover":
	   $(this).find('.description-text').eq(0).slideDown("fast");
	   $(this).find('img').eq(0).animate({opacity:0.7},550)
	   break;
	   
	   case "mouseleave":
	    $(this).find('.description-text').eq(0).slideUp(850);
		$(this).find('img').eq(0).animate({opacity:1},150)
	   break;
	   
	   case "click":
	   window.open($(this).find('img').eq(0).attr('rel'));
	   break;
	
	}
	
}


function arrowFunctions(e)
{
  	var classProp = $(this).attr('alt');
	
	switch(classProp)
	{
		case "left":
		if(counter>0){
		counter--;
		movePanel(450)
		clearInterval(intervalID);
		}
		break;
		
		case "right":
		if(counter<max_counter-1)
		{
		counter++;
		movePanel(450)
		clearInterval(intervalID);
		}
		break;
		
		case "pause":
		if(playing)
		{
		 playing = false;
		 $('.bottom-controller img').eq(1).attr('src',playSource);
		 clearInterval(intervalID);
		}
		else
		{
		 playing = true;	
		  $('.bottom-controller img').eq(1).attr('src',pauseSource);
		  clearInterval(intervalID);
		  movePanel(1500);
		  intervalID = setInterval(slideshow,duration);
		}
		break;
		
	}
	
}

function slideshow()
{
	
	if(counter < max_counter-1)
	{counter++;}
	else
	{counter=0;}
	
	movePanel(1500);
}

function movePanel(time)
{
	
	var tgtx = scrollamount*(-1)*counter;
	$('#plate').animate({left:tgtx},time);
	$('.bottom-controller').find('span').eq(0).text("page "+Number(counter+1)+" of "+max_counter);
}
