//Global variables
var curMainDiv = 1;
var maxMainDiv = 0;
var intervalTopStory;
var durationTopStory = 5000;
var numLong = 0;
//ON DOM READY
$(document).ready(function() {
	if(document.getElementById('main_content_wrapper')) {
		$('#main_content_wrapper .main_slider').siblings().each(
			function(){
				maxMainDiv++;
		});
		
		intervalTopStory = setInterval(slideLeft, durationTopStory);
		
		$('#wrap .quickJump').click(quickSlide);
		$('#wrap .pausePlay').click(autoSliderState);
		
		$('#wrap .left_side a').click(function(){
			killInterval();
			slideRight();
			return false;
		});
		$('#wrap .right_side a').click(function(){
			killInterval();
			slideLeft();
			return false;
		});
		
		$('#small_stories .story_radio_buttons input').click(switcher);
	}
	
	/*$('#HP_News .main_slider').each(function(){
		//alert($(this).find('img').attr('height'));
		var imgHeight = $(this).find('img').attr('height');
		var marginTop = (330 - imgHeight)/2;
		$(this).find('img').css('margin-top', marginTop);
	});*/
	
});

//ON IMAGES READY
$(window).load(function()
{

});

//HELPER FUNCTIONS

function switcher(e){
	//alert($(this).attr('value'));
	if( $(this).attr('value')  == "story1"){
		$('#small_stories .bottom_story').css("visibility","visible");
	}else if( $(this).attr('value')  == "story2"){
		$('#small_stories .bottom_story').css("visibility","hidden");
	}else if( $(this).attr('value')  == "story3"){
		$('#small_stories .bottom_story3').css("visibility","visible");
	}else if( $(this).attr('value')  == "story4"){
		$('#small_stories .bottom_story3').css("visibility","hidden");
	}
}

function slideLeft(e){
	if(curMainDiv < maxMainDiv){
		$('#main_content_wrapper').animate({ marginLeft: (curMainDiv *(-995)) }, 500);
	}else{
		curMainDiv = 0;
		$('#main_content_wrapper').animate({ marginLeft: (curMainDiv *(-995)) }, 500);
	}
	curMainDiv++;
	return false;
}

function slideRight(e){
	var minMainDiv = curMainDiv - 1;
	if(minMainDiv > 0){
		$('#main_content_wrapper').animate({ marginLeft: ((minMainDiv - 1) * (-995)) }, 500);
	}else{
		minMainDiv = maxMainDiv;
		$('#main_content_wrapper').animate({ marginLeft: ((minMainDiv - 1) * (-995)) }, 500);
		curMainDiv = maxMainDiv+1;
	}
	curMainDiv--;
	return false;
}

function quickSlide(e){
	killInterval();
	curMainDiv =$(this).text();
	$('#main_content_wrapper').animate({ marginLeft: ((curMainDiv - 1) * (-995)) }, 500);
	//alert("ji");
	return false;
}

function autoSliderState(e){
	if( intervalTopStory != ""){
		killInterval();
	} else {
		intervalTopStory = setInterval(slideLeft, durationTopStory);
		$('#wrapper .pausePlay').html('<img src="/cms/images/site_layout/pause_btn.png"/>');
	}
		return false;
}

function killInterval(){
	clearInterval(intervalTopStory);
	intervalTopStory = "";
	$('#wrapper .pausePlay').html('<img src="/cms/images/site_layout/play_btn.png"/>');
}
