$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	var prevImg;
	
	$('#slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	$('#slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */
	
	$('#menu ul li a').mouseover(function(e,keepScroll){

			/* On a thumbnail click */

			//$('li.menuItem').removeClass('act').addClass('inact');
			//$(this).parent().addClass('act');
			
			$('li.menuItem a img').each(function(i){
				$(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
			})
			$(this).find("img").attr("src",$(this).find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"));
			$('li.menuItem a img').unbind(handlers).bind(handlers);
			$(this).find("img").unbind(handlers);
							
			var pos = $(this).parent().prevAll('.menuItem').length;
			$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
			/* Start the sliding animation */
			
			e.preventDefault();
			/* Prevent the default action of the link */
			
			
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) {
				clearInterval(itvl);
				/*current = pos+1;
				alert(current + "/" + pos);
				itvl = setInterval(function(){autoAdvance()},changeEvery*1000);*/
			}
	});
	
	//$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	//$('#menu ul li.menuItem:first a img').attr("src",$('#menu ul li.menuItem:first a img').attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"));
	/* On page load, mark the first thumbnail as active */
	
	$("#firstSlide").find("img").hide().fadeIn("slow");
	
	
	
	// slide up (about trymedia)
	var flag = "up";
	$('#slideTabArea #slideTab a.clickTarget').click(function(e){
			var tabA = $(this);
			var tabH = $(this).parent().height();
			//alert(tabH);

			if(flag == "up") {
				//$('#slideTabArea').css('top', 580 - tabH + 'px').css('height', tabH + 'px');
				//$('#slideTab').stop().animate({marginTop:'0px'},450);
				$('#slideTabArea').stop().animate(
					{top:580 - tabH + 'px',height:tabH+'px'},
					450,
					"linear",
					function() {
						tabA.find("img").attr("src",tabA.find("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_down$2"))
					});
				flag = "down";
			}else {
				//$('#slideTab').stop().animate({marginTop:tabH - 42 + 'px'},450);
				$('#slideTabArea').stop().animate(
					{top:580 - 42 + 'px',height:'42px'},
					450,
					"linear",
					function() {
						tabA.find("img").attr("src",tabA.find("img").attr("src").replace(/^(.+)_down(\.[a-z]+)$/, "$1$2"))
					});
				flag = "up";
			}
			/* Start the sliding animation */
			
			e.preventDefault();
			/* Prevent the default action of the link */
			
	});
	
	
	/*****
	 *
	 *	mouseover mouseout.
	 *
	 ****/
	
	var handlers = {
		mouseover: function() {
			$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))
		},
		mouseout: function() {
			$(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
		}
	}
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=-1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		$('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('mouseover',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
		//alert(current)
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 3;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});
