var images=[];
var description=[];
var counter=0;
var temp_next=0;
var SCROLL_AMOUNT=842;
var playing=true;
var imgLink=[];
var intervalID;
var btnLink=0;
var css="position:absolute; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFFFFF; font-weight:bold; top:262px; left:40px; height:26px; width:504px; padding-left:10px;padding-top:8px; background:url(images/des_bg.png); overflow:hidden;";
var descriptionDIV = "<div id='des' style='"+css+"'></div>";

$(document).ready(function(){
	
	var HTML_CONTENT="";
	$('.gallery_img img').each(function(n){  
					
											images[n]=$(this).attr('src');
											imgLink[n]=$('.gallery_img a').eq(n).attr('href');
											$('.control .pause_btn').eq(n).attr('instance','inc_'+n);
											description[n]=$('.gallery_img span').eq(n).html();
											HTML_CONTENT = HTML_CONTENT+"<a href="+imgLink[n]+"><img width='"+SCROLL_AMOUNT+"' src='"+images[n]+"'/></a>";
											
										  });
	HTML_CONTENT = HTML_CONTENT+"<img width='"+SCROLL_AMOUNT+"' src='"+images[0]+"'/>";
	description.push($('.gallery_img span').eq(0).html());
	$('.gallery_img').html(HTML_CONTENT);
	$('#gallery').append(descriptionDIV);
		
	loadImg();		  
	function loadImg()
	{
	  	$('.gallery_img').css('width',842*(images.length+1));
		$('.leftArrow').bind('click',arrowAction);
		$('.rightArrow').bind('click',arrowAction);
		
	}

	function moveNext()
	{
		if(counter<images.length-1)
		{ 
		$('.pause_btn img').eq(temp_next).attr('src',"images/square-off.jpg");
		
		temp_next++;
		  counter++; 
		  tgt_left = counter*(-1)*SCROLL_AMOUNT;
		  $('.gallery_img').animate({left:tgt_left},500,"easeInOutExpo");
		  $('.pause_btn img').eq(temp_next).attr('src',"images/square-on.jpg");
		}
		else
		{
		 counter=images.length;
		 tgt_left = counter*(-1)*SCROLL_AMOUNT;
		 $('.gallery_img').animate({left:tgt_left},500,"easeInOutExpo",function(){counter=0; $('.gallery_img').css('left',0);});
		$('.pause_btn img').eq(temp_next).attr('src',"images/square-off.jpg");
		temp_next=0;
		$('.pause_btn img').eq(temp_next).attr('src',"images/square-on.jpg");
		}
	}
	
	function arrowAction()
	{
	  var tgt_left=0;
	  pause(); 
	  playing=false;
	  switch($(this).attr('class'))
	  {
		  
		case "leftArrow":  
			if(counter>0)
			{
			$('.pause_btn img').eq(temp_next).attr('src',"images/square-off.jpg");
			counter--;
			temp_next--;
			$('.pause_btn img').eq(counter).attr('src',"images/square-on.jpg");
			tgt_left = counter*(-1)*SCROLL_AMOUNT;
			$('.gallery_img').animate({left:tgt_left},500,"easeInOutExpo");
			}
		break;
		
		case "rightArrow":
			if(counter<images.length-1)
			{
				$('.pause_btn img').eq(temp_next).attr('src',"images/square-off.jpg");
			counter++;
			temp_next++;
			tgt_left = counter*(-1)*SCROLL_AMOUNT;
			$('.gallery_img').animate({left:tgt_left},500,"easeInOutExpo");
			$('.pause_btn img').eq(temp_next).attr('src',"images/square-on.jpg");
			$('#des').html(description[counter]);
			}
		break;
	  }
	}
	
	function play()
	{ 
	clearInterval(intervalID);
	intervalID = setInterval(moveNext,5000);
	}
	
	function pause()
	{	
	  clearInterval(intervalID);
	  play();
	}
	play();
	
	

});



