$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	var timeout_for_slide = 5000;
	var timer = null;
	var linkPosition = 0;
	new rss_ticker("v4x", 60, "v4xboxrss", "v4xboxrssclass", 4000);
	
	var totWidth=0;
	var positions = new Array();
	
	$('#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 */
	var onClick = function(linkEl){
		clearTimeout(timer);
		for (var i = 0 ; i < links.length ; ++i){
			if ( links[i] === linkEl ) {
				linkPosition = i;
				break;
			}
		}
		/* On a thumbnail click */

		$('span.menuItem').removeClass('act').addClass('inact');
		$(linkEl).parent().addClass('act');
		
		var pos = $(linkEl).parent().prevAll('.menuItem').length;
		
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},930);
		/* Start the sliding animation */
		
		timer = setTimeout(loop,timeout_for_slide);
	};
	var links = $('#menu ul span a');
	links.click(function(e){
		onClick(this);
		e.preventDefault();
		/* Prevent the default action of the link */
	});
	$('#menu ul span.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	var loop = function(){
		linkPosition = linkPosition + 1 == links.length ? 0 : linkPosition + 1;
		onClick(links[linkPosition]);		
	};
	timer = setTimeout(loop,timeout_for_slide);
	/* start to loop */
});