/*
	Bail Hotline 2012
	Author: Anthony Rivera 
	Contact: anthony.rivera08@gmail.com
*/

$(function(){
	rollovers.init();
	nav_bar.init();
	customer_comm.init();
	slideshow.init();
});

var rollovers = {
	
	/*Initialise rollovers
	---------------------------------------------------------------------------------------------------*/
	init:function(){
		rollovers.subnav();
		rollovers.page_content();	
	},
	
	/*Sub navigation rollovers
	---------------------------------------------------------------------------------------------------*/
	subnav: function(){
		$('#sub_nav ul li:nth-child(4) a').hover(
		function(){$('#sub_nav ul li:nth-child(4) span').css('backgroundPosition', '0px -16px');}, 
		function(){$('#sub_nav ul li:nth-child(4) span').css('backgroundPosition', '0px 0px');});
		
		$('#sub_nav ul li:nth-child(3) a').hover(
		function(){$('#sub_nav ul li:nth-child(3) span').css('backgroundPosition', '0px -16px');}, 
		function(){$('#sub_nav ul li:nth-child(3) span').css('backgroundPosition', '0px 0px');});
	},
	
	/*Page content rollovers
	---------------------------------------------------------------------------------------------------*/
	page_content: function(){
		//Insert div for background overlay and prep css with overflow
		$('#home_quick_links a').css('overflow', 'hidden');
		$('#home_quick_links a').append('<div class="home_qlinks_overlay"></div>');
		
		$('#home_quick_links a').hover(
		function(){ $(this).find('div.home_qlinks_overlay').stop().animate({'width': '650px'}, 400, 'easeOutSine');
					$(this).find('div').css('backgroundPosition', '0px -72px');}, 
		function(){$(this).find('div.home_qlinks_overlay').stop().animate({'width': '0px'}, 650, 'easeOutSine');
				   $(this).find('div').css('backgroundPosition', '0px 0px')});
		}
	
}

var nav_bar = {

	/*Initialise Nav Bar
	---------------------------------------------------------------------------------------------------*/
	init: function(){
		nav_bar.insert_elements();
		nav_bar.set_index();
		nav_bar.on_link_hover();
	},
	
	/*Insert Elements
	---------------------------------------------------------------------------------------------------*/
	insert_elements: function(){
		$('#main_nav').prepend('<div id="main_nav_bar"></div><!--main_nav_bar-->');
		$('#main_nav_bar').append('<div id="nav_slider"></div>');		
	},

	/*Calculate Slide
	---------------------------------------------------------------------------------------------------*/
	calc_slide: function(tar_link, duration, easing){
		var num_of_links = $('#main_nav ul li').length-1;
		var curr_pg_width = $('li.current_page').width();
		var curr_parent = tar_link;
		var link_index = tar_link.index();
		var link_width = tar_link.width();
		var link_dis = 0;
		var link_margin = 11;

		while(link_index < num_of_links){
			link_index < num_of_links ? link_index ++ : link_index--;
			link_dis += curr_parent.next().width() + link_margin;
			curr_parent = curr_parent.next();
		}
		
		nav_bar.slide(link_width, link_dis, duration, easing);
	},
	
	/*Set Page Index
	---------------------------------------------------------------------------------------------------*/
	set_index : function(){
				var curr_pg_index = $('#main_nav li.current_page');
				nav_bar.calc_slide(curr_pg_index, 0, 'easeOutSine');
	},
	
	/*On Link Hover
	---------------------------------------------------------------------------------------------------*/
	on_link_hover: function(){
		$('#main_nav ul li a').hover(
			function(){ 
				var tar_link = $(this).parent();
				nav_bar.calc_slide(tar_link, 450, 'easeOutSine');
			}, 
			function(){ 
				var curr_pg_index = $('#main_nav li.current_page');
				nav_bar.calc_slide(curr_pg_index, 1250, 'easeOutSine');
		});
	},
	
	/*Slide
	---------------------------------------------------------------------------------------------------*/
	slide: function(link_width, link_dis, duration, easing, set_index){
				$('#nav_slider').stop().animate({'width' : link_width, 'margin-left': link_dis}, duration, easing);
			}
}

var slideshow = {
	/*Initialise Slideshow
	---------------------------------------------------------------------------------------------------*/
	init: function(){
		slideshow.dom_prep();
		slideshow.call_change();
	},
	
	/*DOM Prep
	---------------------------------------------------------------------------------------------------*/
	dom_prep: function(){
		$('#slideshow div.wrapper').css('overflow', 'hidden');
		$('#slideshow div.wrapper').append('<div id="slide_controls"><div id="slideshow_right_arrow"></div><div id="slideshow_left_arrow"></div><div id="slideshow_slider"><div id="slideshow_slidebar"></div></div></div><!--slide_controls-->');
		$('#slideshow_slider').css('overflow', 'hidden');
		$('#slideshow_text_container').append('<div class="slideshow_text"><h1 class="slide_text">The Lowest Bail Bonds Rates Guaranteed</h1><p class="slide_text">What sets us apart from other bail bond companies, is our ablility to <br/>offer zero down payment plans. We can also provide bail at an <br/>8% premium, which means you save more.<br/><a href="contact.php" title="Get an Estimate" class="slide_link">Get an Estimate Today &raquo;</a></p></div><!--slideshow_text-->');
		$('#slideshow_text_container').append('<div class="slideshow_text"><h1 class="slide_text">Convenient Bail Office Locations</h1><p class="slide_text">The Bail Hotline has grown very drastically do to success built on <br/>Honesty, Reliability and Openness. We now have more than 25 <br/>office locations in California to help in your time of need.<br/><a href="branch-offices.php" title="Find a Branch Office Location" class="slide_link">Find a Bail Office Near You &raquo;</a></p></div><!--slideshow_text-->');
		$('#slideshow div.slideshow_text, img.slideshow_img:not("img.current_img")').css('opacity', 0);
		$('#slideshow div.current_text').delay(1000).animate({'opacity':1}, 2500, 'easeOutSine');

	},
	
	/* Slide Configuration
	---------------------------------------------------------------------------------------------------*/
	slide_config:{
		margin:0,
		counter:0,
		current_img: $('#slideshow img.current_img'),
		current_text: $('div.current_text'),
	},
	
	/*Call The Slide Change
	---------------------------------------------------------------------------------------------------*/
	call_change : function(){
		var config = slideshow.slide_config;
		$('#slideshow_right_arrow').click(function(){slideshow.change_slide('right_arrow');});
		$('#slideshow_left_arrow').click(function(){slideshow.change_slide('left_arrow');});
	},
	
	/*Change Slide
	---------------------------------------------------------------------------------------------------*/
	change_slide: function(arrow_clicked){
		var config = slideshow.slide_config;
		var get_img_sib = config.current_img.next();
		var get_text_sib = config.current_text.next();
		var get_img_prev = config.current_img.prev();
		var get_text_prev = config.current_text.prev();
		
		//fade out current slide
		$('img.current_img').stop().animate({'opacity':0},1000).removeClass('current_img');
		$('div.current_text').stop().animate({'opacity':0},1000,'easeOutSine').removeClass('current_text');
		
		if(arrow_clicked == 'right_arrow'){
			get_img_sib.addClass('current_img').stop().animate({'opacity' : 1}, 1000);
			get_text_sib.addClass('current_text').stop().delay(1000).animate({'opacity': 1}, 2500, 'easeOutSine');
			config.current_text = get_text_sib;
			config.current_img = get_img_sib;
			
			if(get_img_sib.length == 0){
				$('#slideshow_img_container img:first').addClass('current_img').stop().animate({'opacity' : 1}, 1000);
				$('#slideshow_text_container div.slideshow_text:first').addClass('current_text').stop().delay(1000).animate({'opacity' : 1}, 2500, 'easeOutSine');
				config.current_text = $('#slideshow_text_container div.current_text');
				config.current_img = $('#slideshow_img_container img:first');
			}
			
			config.margin += 50;
			config.counter < 2 ? config.counter++ : (config.margin = 0 ,config.counter=0);
			$('#slideshow_slidebar').stop().animate({'margin-left':config.margin}, 1000);
		}
		else{
			get_img_prev.addClass('current_img').stop().animate({'opacity' : 1}, 1000);
			get_text_prev.addClass('current_text').stop().delay(1000).animate({'opacity': 1}, 2500, 'easeOutSine');
			config.current_text = get_text_prev;
			config.current_img = get_img_prev;
			
			
			
			if(get_img_prev.length == 0){
				$('#slideshow_img_container img.slideshow_img:last').addClass('current_img').stop().animate({'opacity' : 1}, 1000);
				$('#slideshow_text_container div.slideshow_text:last').addClass('current_text').stop().delay(1000).animate({'opacity' : 1}, 2500, 'easeOutSine');
				config.current_text = $('#slideshow_text_container div.current_text');
				config.current_img = $('#slideshow_img_container img.current_img');
			}
			config.margin -= 50;
			config.counter > 0 ? config.counter-- : (config.margin = 100, config.counter = 2);
			$('#slideshow_slidebar').stop().animate({'margin-left':config.margin}, 1000);
		}
	}
}


var customer_comm = {
	
	/*Initialise Customer Comments
	---------------------------------------------------------------------------------------------------*/
		init: function(){
			customer_comm.start_feed.get_feed_items();
			customer_comm.start_feed.dom_prep();
			customer_comm.start_feed.call_switch();
		},
	/*Start Comment Feed
	---------------------------------------------------------------------------------------------------*/
		start_feed:{
			get_feed_items: function(){
				$('#sidebar_comment_bubble p:first').addClass('curr_comment');
				$('#what_people p.customer:first').addClass('curr_customer');
			},

			dom_prep: function(){
				$('#sidebar_comment_bubble').append('<p class="comment"><em>"The people were extremely helpful and happy. They made me feel like things were going to be fine. Fast service and reliability. A++"</em></p>', 
													'<p class="comment"><em>"This is a great bail bonds company! I dealt with them and was very satisfied with the results. Thank you Bail Hotline Bail Bonds for your great service."</em></p>', 
													'<p class="comment"><em>"Everyone I called wanted too much money or a co-signer. The Bail HotLine just asked if I had a job and had me out in no time. Now that\'s service!"</em></p>');
				
				$('#what_people').append('<p class="customer">Alyssa H. Stevenson Ranch, CA </p>', 
										 '<p class="customer">Brian F. San Francisco, CA</p>', 
										 '<p class="customer"> Juanita R. Riverside, CA </p>');
										
				$('#sidebar_comment_bubble p.comment:not("p.curr_comment")').css({'top':5,'opacity':0});
				$('#what_people p.customer:not("p.curr_customer")').css('opacity', 0);	
			},
					
			call_switch:function(){
				setInterval(function(){customer_comm.switch_feed();}, 7000);	
			}
		},
		
	/*Switch Comment Feed
	---------------------------------------------------------------------------------------------------*/
		switch_feed: function(){
			
			var curr_customer = $('#what_people p.curr_customer');
			var curr_comment = $('#sidebar_comment_bubble p.curr_comment');
			var get_cust_sib = curr_customer.next();
			var get_comm_sib = curr_comment.next();

			curr_comment.animate({'opacity':0},800).removeClass('curr_comment');
			curr_customer.animate({'opacity':0},800).removeClass('curr_customer');
			get_cust_sib.addClass('curr_customer').animate({'opacity':1},800);
			get_comm_sib.addClass('curr_comment').animate({'opacity':1},800);
			
			if(get_cust_sib.length == 0 && get_comm_sib.length == 0){
				$('#sidebar_comment_bubble p:first').addClass('curr_comment').animate({'opacity':1},800);
				$('#what_people p.customer:first').addClass('curr_customer').animate({'opacity':1},800);
			}
		}
}


