var $j = jQuery.noConflict();

var tml_carousel = {
	carousel			: null,
	carousel_pieces		: null,
	carousel_bttns		: null,
	carousel_contents	: Array(),
	pieces_displayed	: Array(0, 1),
	
	cell_total			: null,
	viewer_width		: 940,
	cell_width			: 470,
	curr_cell			: 1,
	curr_position		: 0,
	speed				: 600,
	inner_speed			: 300,

	init: function(carousel_id){
		$j('#'+carousel_id).prepend('<div id="carousel_container"><div id="carousel"></div></div>');
		tml_carousel.cell_total = $j('.piece').length - 1;
		tml_carousel.prep_carousel();
		tml_carousel.carousel = $j('#carousel');
		tml_carousel.carousel_pieces = $j('.carousel_pieces');
		tml_carousel.carousel_bttns = $j('.carousel_bttn');
		
		tml_carousel.carousel_bttns.click(function(e){ tml_carousel.advance($j(this)); e.preventDefault(); });
	},

	prep_carousel: function(){
		$j('.piece').each(function(index, item){
			$j(this).attr('tml_carousel_id', index);
			var html_snippet = '<div class="carousel_piece" tml_carousel_id="' + index + '">' + $j(this).html() + '</div>';
			if(index <= 1){
				$j('#carousel').append(html_snippet);
			}
			tml_carousel.carousel_contents[index] = html_snippet;
		});	
		$j('#carousel_pieces').remove();			
	},
	
	advance: function(bttn){
		var direction = bttn.attr('id').split('_')[1];
		if(direction == 'left'){
			var curr_piece = parseInt(tml_carousel.pieces_displayed[0]);
			if(curr_piece == 0){
				var new_piece = tml_carousel.cell_total;
			}else{
				var new_piece = curr_piece - 1;
			}
			tml_carousel.pieces_displayed.unshift(new_piece);
			tml_carousel.carousel.css({'width': '1410px', 'left': '-470px'});
			$j('#carousel').prepend(tml_carousel.carousel_contents[tml_carousel.pieces_displayed[0]]);
			$j('#carousel .carousel_piece:first right_side').animate({'width': '0px'}, 1200);
			tml_carousel.carousel.animate({'left': '0px'}, 300, function(){
				tml_carousel.pieces_displayed.pop();
				$j('#carousel .carousel_piece:last').remove();
				tml_carousel.carousel.css({'width': '940px', 'left': '0px'});
			});
		}else{
			var curr_piece = tml_carousel.pieces_displayed[tml_carousel.pieces_displayed.length - 1];
			if(curr_piece == tml_carousel.cell_total){
				var new_piece = 0;
			}else{
				var new_piece = curr_piece + 1;
			}
			tml_carousel.pieces_displayed.push(new_piece);
			tml_carousel.carousel.css({'width': '1410'});
			$j('#carousel').append(tml_carousel.carousel_contents[tml_carousel.pieces_displayed[tml_carousel.pieces_displayed.length-1]]);
			
			// Last Left
			$j('#carousel .carousel_piece:last left_side').animate({'width': '0px'}, 1200);
			tml_carousel.carousel.animate({'left': '-470px'}, 300, function(){
				tml_carousel.pieces_displayed.shift();
				$j('#carousel .carousel_piece:first').remove();
				tml_carousel.carousel.css({'width': '940px', 'left': '0px'});
			});
		}
	}

};

var imgFader = {
	
	img_fader: 		null,
	current_slide: 	1,
	total_slides:	null,
	
	init : function(){
		imgFader.img_fader = $j('.img_fader');
		imgFader.total_slides = imgFader.img_fader.children('img').length;
		imgFader.img_fader.children('img:nth-child(1)').addClass('display');
		imgFader.img_fader.children('img:nth-child(2)').addClass('on_deck');
		imgFader.img_fader.children('img:nth-child(1n+3)').addClass('off');
		imgFader.run();
	},
	
	run : function(){
		setTimeout(function(){
			imgFader.img_fader.children('.display').fadeOut(600, function(){
				imgFader.img_fader.children('img:nth-child('+imgFader.current_slide+')').addClass('off').removeClass('display');
				if(imgFader.current_slide == imgFader.total_slides){
					imgFader.current_slide = 1;
					imgFader.img_fader.children('img:nth-child('+imgFader.current_slide+')').addClass('display').removeClass('on_deck').show();
					imgFader.img_fader.children('img:nth-child('+(imgFader.current_slide+1)+')').addClass('on_deck').removeClass('off').show();
				}else{
					imgFader.current_slide++;
					if(imgFader.current_slide == imgFader.total_slides){
						imgFader.img_fader.children('img:nth-child('+imgFader.current_slide+')').addClass('display').removeClass('on_deck').show();
						imgFader.img_fader.children('img:nth-child(1)').addClass('on_deck').removeClass('off').show();
					}else{
						imgFader.img_fader.children('img:nth-child('+imgFader.current_slide+')').addClass('display').removeClass('on_deck').show();
						imgFader.img_fader.children('img:nth-child('+(imgFader.current_slide+1)+')').addClass('on_deck').removeClass('off').show();
					}
				}
			});
			imgFader.run();
		}, 3600);
		
//var t=setTimeout("alert('I am displayed after 3 seconds!')",3000)
		
		
		
	}
}
var secondImgFader = {
	
	img_fader: 		null,
	current_slide: 	1,
	total_slides:	null,
	
	init : function(){
		secondImgFader.img_fader = $j('.img_fader_2');
		secondImgFader.total_slides = secondImgFader.img_fader.children('img').length;
		secondImgFader.img_fader.children('img:nth-child(1)').addClass('display');
		secondImgFader.img_fader.children('img:nth-child(2)').addClass('on_deck');
		secondImgFader.img_fader.children('img:nth-child(1n+3)').addClass('off');
		secondImgFader.run();
	},
	
	run : function(){
		setTimeout(function(){
			secondImgFader.img_fader.children('.display').fadeOut(600, function(){
				secondImgFader.img_fader.children('img:nth-child('+secondImgFader.current_slide+')').addClass('off').removeClass('display');
				if(secondImgFader.current_slide == secondImgFader.total_slides){
					secondImgFader.current_slide = 1;
					secondImgFader.img_fader.children('img:nth-child('+secondImgFader.current_slide+')').addClass('display').removeClass('on_deck').show();
					secondImgFader.img_fader.children('img:nth-child('+(secondImgFader.current_slide+1)+')').addClass('on_deck').removeClass('off').show();
				}else{
					secondImgFader.current_slide++;
					if(secondImgFader.current_slide == secondImgFader.total_slides){
						secondImgFader.img_fader.children('img:nth-child('+secondImgFader.current_slide+')').addClass('display').removeClass('on_deck').show();
						secondImgFader.img_fader.children('img:nth-child(1)').addClass('on_deck').removeClass('off').show();
					}else{
						secondImgFader.img_fader.children('img:nth-child('+secondImgFader.current_slide+')').addClass('display').removeClass('on_deck').show();
						secondImgFader.img_fader.children('img:nth-child('+(secondImgFader.current_slide+1)+')').addClass('on_deck').removeClass('off').show();
					}
				}
			});
			secondImgFader.run();
		}, 3600);
		
//var t=setTimeout("alert('I am displayed after 3 seconds!')",3000)
		
		
		
	}
}
$j(document).ready(function(){

	var toggle_profile_bttn = $j('.toggle_profile_list');
	var toggle_client_bttn = $j('.toggle_client_list');
	var header_more_bttn = $j('.header_more_bttn');
	var sticky_header = $j('nav#access');

	toggle_profile_bttn.click(function(e){
		$j('.profile_off').slideToggle(600);
		if(toggle_profile_bttn.html() == 'View All'){
			toggle_profile_bttn.html('Close');
			toggle_profile_bttn.addClass('close');
		}else{
			toggle_profile_bttn.html('View All');
			toggle_profile_bttn.removeClass('close');
		}		
		e.preventDefault();
	});
	
	toggle_client_bttn.click(function(e){
		$j('.entry-content .hidden').slideToggle(600);
		if(toggle_client_bttn.html() == 'View All'){
			toggle_client_bttn.html('Close');
			toggle_client_bttn.addClass('close');
		}else{
			toggle_client_bttn.html('View All');
			toggle_client_bttn.removeClass('close');
		}		
		e.preventDefault();
	});
	
	if($j('#tml_carousel').length == 1){ tml_carousel.init('tml_carousel'); }
	
	if($j('.page-services .entry-content .gray').length >= 1){
		$j('.page-services .entry-content .gray div').append('<div class="gray_arrow"></div>');
	}
	if($j('.page-home .entry-content .gray').length >= 1){
		$j('.page-home .entry-content .gray div').append('<div class="gray_arrow"></div>');
	}
		
	if($j('.page-services .entry-content .orange').length >= 1){
		$j('.page-services .entry-content .orange div').append('<div class="orange_arrow"></div>');
	}

	if($j('.page-results .entry-content .gray').length >= 1){
		$j("div.gray:first").css("zIndex", "10");
		$j("div.gray:first h3").css("backgroundPosition", "-2701px 0");
		$j('.page-results .entry-content .gray div.clearfix').append('<div class="gray_arrow"></div>');
	}
	
	if($j('.page-results .entry-content .white').length >= 1){
		$j("div.orange:first").css("zIndex", "9");
		$j("div.orange:first h3").css("backgroundPosition", "-3152px 0");
		$j('.page-results .entry-content .white div.clearfix').append('<div class="white_arrow"></div>');
	}
	
	if($j('.page-results .entry-content .orange').length >= 1){
		$j("div.white:first").css("zIndex", "8");
		$j("div.white:first h3").css("backgroundPosition", "-3601px 0");
		$j('.page-results .entry-content .orange div.clearfix').append('<div class="orange_arrow"></div>');
	}	
	
	if($j('.img_fader').length >= 1){ imgFader.init(); }
	
	if($j('.img_fader_2').length >= 1){ secondImgFader.init(); }
	
	
	header_more_bttn.click(function(e){
		$j('#header_bottom .hidden_section').slideToggle(600);
		
		if(header_more_bttn.html() == 'Continue'){
			header_more_bttn.html('Close');
			header_more_bttn.addClass('close');
		}else{
			header_more_bttn.html('Continue');
			header_more_bttn.removeClass('close');
		}		

		
		e.preventDefault();
	});
	
	$j(window).scroll(function(){ 
		var menu_pos = sticky_header.offset().top - $j(window).scrollTop();
		var scroll_pos = find_scrollTop();
		if (scroll_pos > 133) {
			sticky_header.addClass('fixed-menu');
			//sticky_header.css('marginTop', '-72px');
		}else if (scroll_pos < 133) {
			sticky_header.removeClass('fixed-menu');
		}	
	 }); 

});

	/* Scrolling Details */
function find_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	)
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}














