/**
 * This is used to manage the Home page Welcome Introductory page
 * @author Srinivas Gurajala
 * Sourcebits Technologied Pvt Ltd,.
 * Bangalore
 */

$('document').ready(function(){
	/**
	 * Video player
	 */ 
	 
	//flowplayer("introductionPlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf");
	
	 /*
	$('#close_this_video').livequery('click',function(){
		// get a handle to the player just installed and call it's method "getVersion" 
		$f("introPlayer").getVersion(); 
		// get a handle to the first player and stop it 
		$f().stop(); 
	});
	*/
	 
	/**
	 * for custom carousels
	 */
	var isAnimStopped  = true;
	var isAnimStopped2  = true;
	var isJMAnimStopped = true;
	/**
	 * Select video li length to slide
	 */
	var liElm = $('.page_screens');
	var slideWidth = parseInt(liElm[0].offsetWidth);
	
	/**
	 * Selected video title Carousel.
	 */
	function slideTitleCarousel(elm, sign) {
		var no = parseInt(elm.css('left'));
		
		isAnimStopped = false;
		if(sign == '+') { no += slideWidth; }
		else { no -= slideWidth; }
		elm.animate({left:no}, 400, 'easeInQuad', function() {
			isAnimStopped = true;
		});
	}
	
	$('#title_left_arrow').livequery('click', function(){
		$('.wc_video').hide();
		$('.wc_page').fadeIn('slow');
		var elm = $('#titles_container');
		var no = parseInt(elm.css('left'));	
		if(no == 0) { 
			isAnimStopped = false;
			no = parseInt(elm[0].offsetWidth)- 2 * slideWidth;
			elm.animate({left:'-'+no}, 400, 'easeInQuad', function() {
				isAnimStopped = true;
			}); 
			return false;
		}
		if(isAnimStopped) {
			slideTitleCarousel(elm, '+');
		}
	});
	
	$('#title_right_arrow').livequery('click', function(){
		$('.wc_video').hide();
		$('.wc_page').fadeIn('slow');
		var elm = $('#titles_container');
		//var el = document.getElementById('checkoutlist_holder_table').style.left;
		if((Math.abs(parseInt(elm.css('left')))+1394) >= elm[0].offsetWidth) { 
			isAnimStopped = false;
			elm.animate({left:0}, 400, 'easeInQuad', function() {
				isAnimStopped = true;
			});
			return false;
		}
		if(isAnimStopped) {
			slideTitleCarousel(elm, '-');
		}
	});
	
	
	// Right side bar slider in home page
	
	/*********************************/
 	/*   To slide up and down        */
 	/*********************************/
	function slideCarousel(elm, sign) {
		var no = parseInt(elm.css('top'));
		isAnimStopped2 = false;
		if(sign == '+') { no += 55; }
		else { no -= 55; }
		elm.animate({top:no}, 200, 'easeInQuad', function() {
			isAnimStopped2 = true;
		});
	}

	/***************************/
	/* on clikc of LEFT arrow  */
	/***************************/
	$('.rightside_scroller_up').click( function(){
		var elm = $('#tourlistholder');
		var no = parseInt(elm.css('top'));	
		if(no == 0) { 
			isAnimStopped2 = false;
			no = parseInt(elm[0].offsetHeight)-297;
			elm.animate({top:'-'+no}, 400, 'easeInQuad', function() {
				isAnimStopped2 = true;
			}); 
			return false;
		}
		if(isAnimStopped2) {
			slideCarousel(elm, '+');
		}
	});
	
	/*******************************/
	/*  on click of RIGHT arrow    */
	/*******************************/
	$('.rightside_scroller_down').click( function() {
		var elm = $('#tourlistholder');
		//var el = document.getElementById('checkoutlist_holder_table').style.left;
		if((Math.abs(parseInt(elm.css('top')))+114) >= elm[0].offsetWidth) { 
			isAnimStopped2 = false;
			elm.animate({top:0}, 400, 'easeInQuad', function() {
				isAnimStopped2 = true;
			});
			return false;
		}
		if(isAnimStopped2) {
			slideCarousel(elm, '-');
		}
	});
	
	
	//On click of options in right side bar,load the select video content with relevant information
	$('ul#tourlistholder li').livequery('click', function(){
		var linkId = $(this).attr('id');
		linkId = linkId.split('_');
		var selectedlink = linkId['1'];
		// show proper title for video
		$('.wc_video').hide();
		$('#'+selectedlink+'_page').fadeIn('slow');
		var clickedIndex = $('ul#tourlistholder li').index(this);
		
		var moveUpto = clickedIndex * slideWidth;
		isAnimStopped = false;
		$('#titles_container').animate({left: '-'+moveUpto+'px'}, 400, 'easeInQuad', function() {
			isAnimStopped = true;
		});
		return false;
		// show proper video select option for video
		$('#sel_container >div').hide();
		$('#sel_'+selectedlink).show();
		
	});
	
	function slideJMCarousel(elm, sign) {
		var no = parseInt(elm.css('left'));
		isJMAnimStopped = false;
		if(sign == '+') { no += 965; }
		else { no -= 965; }
		elm.animate({left:no}, 600, 'easeInQuad', function() {
			isJMAnimStopped = true;
			setActiveBtns();
		});
	}
	
	function setActiveBtns()
	{
		var elm = $('#job_bank_gal');
		$('#slider_head_txt h3').hide();
		if(parseInt(elm.css('left')) == 0){
			$('.btn_control_2').find('a').removeClass("selected");
			$('.btn_control_2 a').eq(0).addClass("selected");
			$('#slider_head_txt h3').eq(0).show();
		} else if(parseInt(elm.css('left')) == -965){
			$('.btn_control_2').find('a').removeClass("selected");
			$('.btn_control_2 a').eq(1).addClass("selected");
			$('#slider_head_txt h3').eq(1).show();
		} else if(parseInt(elm.css('left')) == -1930){
			$('.btn_control_2').find('a').removeClass("selected");
			$('.btn_control_2 a').eq(2).addClass("selected");
			$('#slider_head_txt h3').eq(2).show();
		}
	}
	
	$('#previous_page_arrow').livequery('click', function(){
		var elm = $('#job_bank_gal');
		var no = parseInt(elm.css('left'));		
		if(no == 0) {
			isJMAnimStopped = false;
			no = parseInt(elm[0].offsetWidth)- 5 * 193;
			elm.animate({left:'-'+no}, 600, 'easeInQuad', function() {
				isJMAnimStopped = true;
				setActiveBtns();
			}); 
			return false;
		}
		if(isJMAnimStopped) {
			slideJMCarousel(elm, '+');
		}
		
	});
	
	$('#next_page_arrow').livequery('click', function(){	
		var elm = $('#job_bank_gal');
		//var el = document.getElementById('checkoutlist_holder_table').style.left;
		if((Math.abs(parseInt(elm.css('left')))+965) >= elm[0].offsetWidth) {
			isJMAnimStopped = false;
			elm.animate({left:0}, 600, 'easeInQuad', function() {
				isJMAnimStopped = true;
				setActiveBtns();
			});
			return false;
		}	
		if(isJMAnimStopped) {
			slideJMCarousel(elm, '-');
		}
		
	});
	
	/*
	$('.btn_control_2 a').livequery('click',function(){
		var clickedBtn = $(this).attr('id');
		clickedBtn = clickedBtn.split('_');
		var btn = parseInt(clickedBtn['2']);
		if(doIt){
			$('#gallery_container >div').hide();
			currPage = btn;
			doIt = false;
			$('#gallery_container').find(':nth-child('+currPage+')').fadeIn('slow');
			$('.btn_control_2').find('a').removeClass("selected");
			$(this).addClass('selected');
			setTimeout(function(){
				doIt = true;
			},500);
		}
	});
	*/
	
	$('#watch_intro_video').livequery('click',function(){
		$('#introduction_page').fadeOut('fast',function(){
			$('#introduction_video').fadeIn('slow');
		});
	});
	$("#go_to_top").livequery("click", function(){
		$("#watch_intro_video").trigger('click');
	});
	 
	$('#watch_jobbank_video').livequery('click',function(){
		$('#jobbank_page').fadeOut('fast',function(){
			$('#jobbank_video').fadeIn('slow');
		});
		//flowplayer("jobbankPlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf");
	});
	
	$('#watch_gradegoals_video').livequery('click',function(){
		$('#gradegoals_page').fadeOut('fast',function(){
			$('#gradegoals_video').fadeIn('slow');
		});
		//flowplayer("gradegoalsPlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf");
	});
	
	$('#watch_bookclub_video').livequery('click',function(){
		$('#bookclub_page').fadeOut('fast',function(){
			$('#bookclub_video').fadeIn('slow');
		});
		//flowplayer("bookclubPlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf");
	});
	
	$('#watch_superstore_video').livequery('click',function(){
		$('#superstore_page').fadeOut('fast',function(){
			$('#superstore_video').fadeIn('slow');
		});
		//flowplayer("superstorePlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf");
	});
	
	$('#watch_thewire_video').livequery('click',function(){
		$('#thewire_page').fadeOut('fast',function(){
			$('#thewire_video').fadeIn('slow');
		});
		//flowplayer("thewirePlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf"); 
	});
	$('#watch_contract_video').livequery('click',function(){
		$('#contract_page').fadeOut('fast',function(){
			$('#contract_video').fadeIn('slow');
		});
		//flowplayer("contractPlayer", "http://flowplayer.org/swf/flowplayer-3.1.1.swf"); 
	});
	
	$('.close_video').livequery('click', function(){
		$('.wc_video').hide();
		$('.wc_page').show();
	});
	
});
