/*************CHANGE!!!! for strict validation*******************/
//<![CDATA[
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };


maincontainer = new Array('.slideshow', '#featured-slider-content');

//when document is ready the function declared is excecuted
$(function() {
	$(maincontainer).each(function(i, o) {
		var slidewidthes = new Array(475, 290);
 		var animations = new Array('slideleft', 'slideright');
		
		var name = String(this);
		var animation = animations[i];
		var slideWidth = slidewidthes[i];
		
		var currentPosition = 0;
		
		var outCP=0;//i use this variable for the autoscrolling feauture,you dont have to change it 
		var outnOS=0;//shame as above	  var currentPosition = 0;//the current position of the slider
		//if not,remember to set it everytime you use it.
		var slides = $(this + ' .slide');//the slides are all divs of class slide
		var numberOfSlides = slides.length;//how many slides we got
		outnOS=numberOfSlides;//just for autoscroller
		var autoscrollable=false;
		var to = null;
		var tim = null;
	
	
	  $(name + ' .slidesContainer').css('overflow', 'hidden');//we set overflow hidden so we can hide the hiden slides 
	 
	  // Wrap all .slides with #slideInner div
		//that means we insert an HTML structure around the slides
	  if (animation == 'slideleft') {
		slides.wrapAll('<div class="slideInner"></div>').css({ width: slideWidth, 'float' : 'left'} );
		$(name + ' .slideInner').css('width', slideWidth * numberOfSlides);
	  } else if (animation == 'slideright') {
		slides.css({ width: slideWidth, position:'absolute', 'top':'0','left':-slideWidth} );
		slides.eq(0).css({ left: 0 });
	  } else if (animation == 'slidetop') {
		slides.css({ width: slideWidth, position:'absolute', 'top':-slides.eq(0).height(),'left':0} );
		slides.eq(0).css({ top: 0 });
	  }
		
	  // Set #slideInner width equal to total width of all slides
	  $(name).parent().find(' .controlLeft').bind('click', function(){
		// determine new position
		currentPosition = currentPosition-1;
		if (currentPosition < 0)
			currentPosition = outnOS-1;
			
		outCP=currentPosition;
		if (animation == 'slideleft') {
			$(name + ' .slideInner').animate({
			  'marginLeft' : slideWidth*(-currentPosition)
			});
		} else if (animation == 'slideright') {
			$(name + ' .slide').not(':eq('+outCP+')').animate({ left:slideWidth }, {queue:false, complete:function() { $(this).css('left', -slideWidth) } });
			$(name + ' .slide').eq(outCP).animate({ left:0 }, {queue:false});
		} else if (animation == 'slidetop') {
		}
		updateStuff();
	  });
	  $(name).parent().find(' .controlRight').bind('click', function(){
		// determine new position
		currentPosition = currentPosition+1;
		if (currentPosition >= outnOS)
			currentPosition = 0;
			
		outCP=currentPosition;
		if (animation == 'slideleft') {
			$(name + ' .slideInner').animate({
			  'marginLeft' : slideWidth*(-currentPosition)
			});
		} else if (animation == 'slideright') {
			$(name + ' .slide').not(':eq('+outCP+')').animate({ left:slideWidth }, {queue:false, complete:function() { $(this).css('left', -slideWidth) } });
			$(name + ' .slide').eq(outCP).animate({ left:0 }, {queue:false});
		}
		updateStuff();
	  });
	  
	  $(name).parent().find(' .controlPause').bind('click', function() {
		autoScroll();
	  });
		updateStuff();
		autoScroll();

		function updateStuff() {
			text = '';
			$(slides.eq(outCP).find('a, h2')).each(function() {
				if ($(this).text() != '...' && $(this).text().trim() != '') {
					text = $(this).text();
					return;
				}
			});
			$(name).parent().find(' .current-slide').html('<a href="'+slides.eq(outCP).find('a').attr('href')+'">'+text+'</a>');
			$(name).parent().find(' .slideshow-counter').html((outCP+1) + ' / ' + outnOS);
		}
	
		var tim;
		var into=false;

		function stopAndScroll(to)//moves the slider to the given place (to)
		{
			stoper=true;//stops the scrollit;
			if (autoscrollable);
				autoScroll();
			if(outCP==to)
				return;//we are on the right place
			else if(outCP>to)//we have to scroll left
			{
				for(var i=outCP;i>to;i--)
					$(name).parent().find(' .controlLeft').click();
				
			}
			else if(outCP<to)//we have to scroll right
			{
				for(var i=outCP;i<to;i++)
					$(name).parent().find(' .controlRight').click();
				
			}
				outCP=to;
		}
		 
		function autoScroll() {
			if (to != null) {
				clearTimeout(to);
			}
			if (tim != null) {
				clearTimeout(tim);
			}

			if (autoscrollable) {
				autoscrollable = false;
				$(name).parent().find(' .controlPause').css('opacity', '0.5');
			}
			else {
				autoscrollable = true;
				$(name).parent().find(' .controlPause').css('opacity', '1');	
				scrollIt();
			}
			
		}

		function scrollIt()//autoscrolls the slider
		{
			to = setTimeout(clickRight,3500);
			tim=setTimeout(scrollIt,8000);
		}
		
		function clickRight() {
			$(name).parent().find('.controlRight').click();
		}
			
		 
		/*************CHANGE!!!! for strict validation*******************/
		//]]>

	
	});
});


