var lastHash = '';

var keepcheckin = '';

function afterSlide(){

	var categories = getHashArray();
	

	if (categories[1] !== undefined){
	
		if ($('li[name="'+categories[1]+'"]','#left_nav_wrap').length){
		
			//alert($('li[name="'+categories[1]+'"]').attr('name'));

			$('li[name="'+categories[1]+'"]','#left_nav_wrap').addClass('active').siblings('li').removeClass('active');
			
			actWithoutActionSub($('li[name="'+categories[1]+'"]','#left_nav_wrap'));
			
			
		} else {
		
			//chopHash();
			
			if ($('li','#left_nav_wrap').length){
				$('li:first','#left_nav_wrap').addClass('active').siblings('li').removeClass('active');
				//setHash();
			}		
					
			
		}
	} else {
	
			if ($('li','#left_nav_wrap').length){
				$('li:first','#left_nav_wrap').addClass('active').siblings('li').removeClass('active');
				//setHash();
			}	
	
	}
	

	
	
	
}


function chopHash(){
	if (getHashArray()[0] !== undefined){
		window.location.hash = '/'+getHashArray()[0]+'/';
		lastHash = '/'+getHashArray()[0]+'/';
	}
}


function getMyPage(){
	return $('#currentPage').val();
}

function setHash(){
	
	//if the open class isn't set yet.. grab it from the current li
	var category = ($('li.active','#main_nav').length) ? $('li.active','#main_nav').attr('name') : $(this).attr('name');

	
	var subSection = ($('li.active','#left_nav_wrap').length) ? $('li.active','#left_nav_wrap').attr('name') : '';
	
	if (subSection === ''){
		var allhashitems = getHashArray();
		if (allhashitems.length>1){
			subSection = allhashitems[1];
		}
	} else {
		subSection = subSection + '/';
	}
	
	var hashy = ('/'+category+'/'+subSection);
	
	window.location.hash = hashy;
	lastHash = hashy;
	
}


function getLastHash(){
	if (lastHash !== undefined){
		return lastHash;
	} else {
		return '';
	}
}



function getHash(){
	if (window.location.hash !== undefined){
		return window.location.hash.replace(/#/,'').replace(/^\/+|\/+$/g,"");
	} else {
		return '';
	}
}

function getHashArray(){
	var array = [];
	if (window.location.hash){
		var hash = window.location.hash.replace(/#/,'').replace(/^\/+|\/+$/g,"");
		var array = hash.split('/');	
	}
	return array;
}


function goToSectionBasedOnHash(){

	var categories = getHashArray();
	

	if (categories[0] !== undefined){
		
		//alert($('li[name="'+categories[0]+'"]','#main_nav').length);
		
		//alert(typeof($('li[name="'+categories[0]+'"]','#main_nav')));
		
		//alert(categories[0]);
		
	
		actWithoutAction($('li[name="'+categories[0]+'"]','#main_nav'));
		
	} else {
	
		actWithoutAction($('#item_0'));
	
	}

	
	
}



function actWithoutActionSub(element){

	$('#left_nav_wrap ul li').removeClass('active').removeClass('open').removeClass('clicked');
	$(element).addClass('active').addClass('open').addClass('clicked');
	var slide_amount = -367*$(element).attr('id').substr(8);	
	
	
	$('#slider').animate({'top':slide_amount+'px'},600);
	
}

function actWithoutAction(element){
	
	var id = Number($(element).attr('id').substr(5));
	
	$("#main_nav li.open").removeClass('open active');
	
	$(element).addClass('open active');
	
	if (id === 0 && mainNav0 !== undefined){
		
		left_nav_items = mainNav0['nav_items'];
		theclass = '';
		html = mainNav0["html"];
		
	} else if (id === 1 && mainNav1 !== undefined){

		left_nav_items = mainNav1['nav_items'];
		theclass = 'color_cards';
		html = mainNav1['html'];
	} else if (id === 2 && mainNav2 !== undefined) {

		left_nav_items = mainNav2['nav_items'];
		theclass = 'color_systems';
		html = mainNav2["html"];
	} else if (id === 3 && mainNav3 !== undefined) {

		left_nav_items = mainNav3['nav_items'];
		theclass = 'auto';
		html = mainNav3["html"];
	} else if (id === 4 && mainNav4 !== undefined) {

		left_nav_items = mainNav4['nav_items'];
		theclass = 'r_d';
		html = mainNav4["html"];
	} else if (id === 5 && mainNav5 !== undefined) {
		
		left_nav_items = mainNav5['nav_items'];
		theclass = 'colortac';
		html = mainNav5["html"];
	} else if (id === 6 && mainNav6 !== undefined) {
		
		left_nav_items = mainNav6['nav_items'];
		theclass = 'color_solutions';
		html = mainNav6["html"];
			
	}
	
	//alert(id+left_nav_items);
	
	
	if (id === 0) {
		$('#main_nav').removeClass('main');
	} else {
		$('#main_nav').addClass('main');
	}
	
	slideAndLoad(id,left_nav_items,theclass,html);
	
	if ($('.scroll-pane').length){
		$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 13, arrowSize: 5});
	}
	
}



function killInterval(){
	keepcheckin = clearInterval(keepcheckin);
}

function checkForChange(){
	
	
	
	goToSectionBasedOnHash();
	
}

function shouldireload(e){
	var cat = $(this).attr('name');
	
	if (getMyPage() !== '/'){
		
		//alert(cat);
		
		window.location = '/'+cat;

	}
	
	e.stopPropagation();
	
	return false;	
}


function restoreLastHash(href){
	
	lastHash = href;

}


$(document).ready(function() {	

	$('div.bucket a','#bucket_wrapper').click(function(e) {
		
		$('#main_nav li').removeClass('open').removeClass('active');
		
		$('#main_nav li:eq(6)').addClass('open active').trigger('click');
	
		lastHash = $(this).attr('href');
		
		restoreLastHash()
		
		var timey = setTimeout('setHash()',1000);
/*
		if ($(this).siblings('.headline').text() === 'Trend Report'){
			
			window.location.hash = $(this).attr('href');
			
			checkForChange();
			
			//$('#main_nav li:eq(3)').trigger('click');
			
			goToSectionBasedOnHash();
			
			//slideAndLoad($(this).attr('id').substr(5),left_nav_items,theclass,html);
			
			return false;
			
		}
*/
		
	});
	
	
	
	
	
	
	$('a.seolink').click(function(e){
		$(this).parents('li').trigger('click');
		return false;
	});
	
	
	$('html').hover(function(e){
	
		killInterval();
	
	}, function(e){
		//leaving the window
		keepcheckin = setInterval("checkForChange()",1000);
		
	});
	
	
	
	$('li','#main_nav').click(shouldireload);
	
	$('li','#main_nav').click(setHash);
	$('li','#left_nav_wrap').live('click',setHash);


	
	$('ul.aligned').vAlign();

	$("#main_nav li.open").addClass('active');

	// set hover states for LIs
	jQuery('#main_nav li').hover(function() {
		$("#main_nav li.open").removeClass('active');
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
		$("#main_nav li.open").addClass('active');
	});
	
	// make open class active
	$("#top_nav li.open").addClass('active');
	
	// set hover states for LIs
	jQuery('#top_nav li').hover(function() {
		$("#top_nav li.open").removeClass('active');
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
		$("#top_nav li.open").addClass('active');
	});
	
	$('#left_nav_wrap ul li').live('click', function(event) {
		
   	$('#left_nav_wrap ul li').removeClass('active').removeClass('open').removeClass('clicked');
		$(this).addClass('active').addClass('open').addClass('clicked');
    var slide_amount = -367*$(this).attr('id').substr(8);	
		$('#slider').animate({'top':slide_amount+'px'},750);
			
	});	
	
	$("#left_nav_wrap ul li.open").addClass('active clicked');
	
	//lets style and set hover states for the left nav, even the stuff being loaded dynamically
	$('#left_nav_wrap ul li').livequery(function(e){ 
	
		// use the helper function hover to bind a mouseover and mouseout event 
		//$("#left_nav_wrap ul li.open").addClass('active');
		$(this).hover(function(e) {
			
			if ($(this).not('.clicked')){
			
				$("#left_nav_wrap ul li.open").removeClass('active');
				$(this).addClass('active');
				
			}
			
		}, function(e) {
				
				if ($(this).not('.clicked')){
					
					
					if (!$("#left_nav_wrap ul li.clicked").length){
						
						$("#left_nav_wrap ul li").removeClass('active');
						
						$("#left_nav_wrap ul li.open").addClass('active');
						
					
					} else {
					
						$("#left_nav_wrap ul li.open").addClass('active');
						
						
						if ($("#left_nav_wrap ul li.open").hasClass('clicked')){
							
							$("#left_nav_wrap ul li:not('.open')").removeClass('active');
							
						} else {
							
											
							$(this).removeClass('active');
							
							$("#left_nav_wrap ul li.open").removeClass('active');
						
						}	
						
					}
					
					
					
				} else {
					
						$(this).addClass('active');
						$("#left_nav_wrap ul li.open").removeClass('active');
				}
				
		});			 
	}, function() { 
		// unbind the mouseover and mouseout events 
		$(this).unbind('mouseover').unbind('mouseout'); 
	});
		
});


(function ($) {
	$.fn.vAlign = function(container) {
		return this.each(function(i){
			if(container == null) {
				container = 'div';
			}
			$(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
			var el = $(this).children(container + ":first");
			var elh = $(el).height(); //new element height
			var ph = $(this).height(); //parent height
			var nh = (ph - elh) / 2; //new height to apply
			$(el).css('margin-top', nh);
		});
	};
})(jQuery);


var currentItem = 0;
	
function slideAndLoad(nav_item_id,menu_items,theclass,html) {
	

	if (currentItem!=nav_item_id) {
	
		currentItem = nav_item_id;
		var slide_amount = -839*nav_item_id;

		
		
		//var slide = $('#slider').css('top').replace(/[^\d\.]/g, '');
		var slide = parseInt($('#slider').css('top'));
		if (slide==0) {
		
			$('#slider').animate({'left':slide_amount+'px'},{
				queue:false, 
				duration:1500, 
				complete:afterSlide
			});
			
			$('#left_nav_wrap ul').animate({opacity: 0}, 750, function() {
				$(this).html(menu_items);
				// make open class active
				$("#left_nav_wrap ul li.open").addClass('active');					
				$(this).removeClass('color_cards');
				$(this).removeClass('color_systems');
				$(this).removeClass('auto');
				$(this).removeClass('r_d');
				$(this).removeClass('colortac');
				$(this).removeClass('color_solutions');
				$(this).addClass(theclass);
				$(this).vAlign();
				$('#left_nav_wrap ul').animate({opacity: 1}, 750);	
			});
		} else {
			$('#slider').animate({'top':0+'px'} ,750, function () {
				
				$('#slider').animate({'left':slide_amount+'px'} ,1500, afterSlide);
				
				$('#left_nav_wrap ul').animate({opacity: 0}, 750, function() {
					$(this).html(menu_items);
					// make open class active
					$("#left_nav_wrap ul li.open").addClass('active');					
					$(this).removeClass('color_cards');
					$(this).removeClass('color_systems');
					$(this).removeClass('auto');
					$(this).removeClass('r_d');
					$(this).removeClass('colortac');
					$(this).removeClass('color_solutions');
					$(this).addClass(theclass);
					$(this).vAlign();
					$('#left_nav_wrap ul').animate({opacity: 1}, 750);	
				});
			});
		}
		
		
		if (($('#horizontal_'+nav_item_id).attr('class'))!='section_wrapper loaded') {
		
			($('#horizontal_'+nav_item_id)).addClass('loaded');
			
			//alert($('#horizontal_'+nav_item_id).html());
			
			//$('#horizontal_'+nav_item_id).find('div.page_container').prepend('<div class="funnn" style="display:none; border:5px solid purple;">'+html+'</div>');
			
			//$('body').prepend('<img src="/assets/img/funny-pictures-chipmunk-asks-you-to-go-on.jpg" width="1" height="1" id="chip" />');
			
			//$('#horizontal_'+nav_item_id).prepend($('#horizontal_'+nav_item_id).find('div.funn'));
			//$('#horizontal_'+nav_item_id).find('div.funn:first').remove();
			
			//$('#horizontal_'+nav_item_id).find('div.funn').show();
			
			
			
/*
			$('#horizontal_'+nav_item_id).find('div.page_container').after('<div class="new"></div>');
			
			$('#horizontal_'+nav_item_id).find('div.new').prepend('<div style="clear:both;"></div>'+html);
*/
			
			//alert(html);
			
			$('#horizontal_'+nav_item_id).append('<div style="clear:both;"></div>'+html+'<div style="clear:both;"></div>');
			
			
			//alert($('#horizontal_'+nav_item_id).html());	
			//alert($('#horizontal_'+nav_item_id).find('img#chip').length);
			
		} else {
		
			//alert('false');
			
		}
		
	} else {
		afterSlide();
	}
}



