$(document).ready(function(){

	var imageHeight = $(".sliderContent").height(),
		slides = $('.slides'),
		imageSum = $(".slides li").size(),
		slidesHeight = imageHeight * imageSum,
		preImg = $('.pre-image'),
		$active,
		controls = $(".sliderControls");
	
	//Shows the promotion banner then shows the tabbed banners
	init = function(){		
		preImg.find('.slide-text').fadeIn('slow');
		preImg.css('z-index', '50000')
		preImg.pause(9000).animate({top:-342}, function(){																													
			if($('.pre-image').length) $('.pre-image').remove();
		});
		
		controls.find("a").hover(function() {			
			preImg.remove();
		});		
	};
	contentFade = function(){
		$active.parent().find(":nth-child(2)").css('display', 'block').animate({
			top:20, 			
			opacity:1
		}, 950, function(){$(this).css('filter','');});
	};
	
	rotate = function(){
		var triggerID = $active.attr("rel") - 1, //Get number of times to slide
			slidesPosition = triggerID * imageHeight; //Determines the distance the image reel needs to slide
			slides.css('opacity', '.25');
			
			controls.find("a.inactive").removeClass('inactive');
			controls.find("a.active").removeClass('active');
			controls.find('div').css('display', 'none');		
		
			$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function
																											
		//Slider Animation
		slides.stop(true).animate({top: -slidesPosition, opacity:1 }, 800);
		contentFade();		
	};// end rotate function
	
	rotateSwitch = function(){    		
		play = setInterval(
			function(){ //Set timer				
					$active = $('.sliderControls a.active').parent().next('li').children(':first'); //Move to the next paging
					$active.parent().prev('li').children(':first').addClass('inactive'); //Remove all active class
					if ( $active.length === 0) { //If paging reaches the end...
						$active = $('.sliderControls a:first'); //go back to first
					}			 
					rotate(); //Trigger the paging and slider function
					$active.parent().prev('li').children(':first').removeClass('active');
			}, 9000); //Timer speed in milliseconds 
	};//end rotateSwitch function

	slides.css({'height' : slidesHeight});

	init(); //Run function on launch
	rotateSwitch(); //Run function on launch

	//On Hover
	slides.find("a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	
	
//for ios	
	var device = navigator.userAgent.toLowerCase();
	var tabEvent = device.match(/(iphone|ipod|ipad)/) ? "touchstart" : "mouseover";			
			controls.find("a.sliderTrigger").bind(tabEvent, function(){
		 	if($(this).hasClass('active')){}else{
			$active = $(this); //Activate the clicked paging
			clearInterval(play); //Stop the rotation
			$('.slide-text').css({'top':'70px', 'opacity':0});
			//$active.style.removeAttribute('filter');
			rotateSwitch();
			rotate();
			return false;
			};
			});
});


//Delay / Pause function
$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

