// on page load...
$(function() { 

//declare some functions
/////

function colorMenu() {
	var checkhash = window.location.hash;
	hack_hash = checkhash.replace('#','');
	checkhash =  (hack_hash>0)?'#events':checkhash;
	if(checkhash == '') {
		$("#access .menu-item a").css({ 'color': '#fff'});
	} else {
		var strStart = "#access ";
		var strEnd = " a";
		checkhash = checkhash.replace("#", ".");
		checkhash = strStart.concat(checkhash, strEnd)
		$("#access .menu-item a").css({ 'color': '#fff'});
		$(checkhash).css({ 'color': '#f8f859' });

		$("#access .menu-item .sub-menu a").css({ 'color': '#fff'});
	}
}

colorMenu();

//createCookie function 
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else var expires = '';
	var myhash = window.location.hash;	
	document.cookie = 'hashcookie'+'='+myhash+expires+'; path=/';
}

// ajax function
function myajax() {
	// set cookie with page data
	createCookie();	
	$.ajax({ 
	url: '/wp-content/themes/hpr-custom/loadme.php',
	type: 'get',
	data: {'page':page},
	success: function(data) { 	
		// stick the html we got from the server into our content div
		$('#mywrapper').html(data); 
			$('#mywrapper').fadeIn('slow', function() { $('#spinner').hide(); });
			
		}
	});
}

//-------copy --------------------
var a_campaigns = Array();
i=0;
$('#menu-main-menu li.campaigns li a').each(function(){
	href = $(this).attr('href');        
	a_href = href.split('/');
	if(href.substr(href.lenght,1)=='/') 
		href = a_href[a_href.length-3];
	else
		href = a_href[a_href.length-2];
	$(this).attr('href','#campaigns/'+href);
	a_campaigns[i++] = '#campaigns/'+ href;
});
function in_array(v,arr){
	for(i=0;i<arr.length;i++){
		if(v==arr[i])
			return true;
	}
}
//end copy
//call ajax for menu clicks (but not sub-menus)
var page = '';

function IsNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}

$('.menu-item a').click(function(evt) {
	
	var elementClicked = $(this).attr("href"); // am i a sub-menu?
	page = elementClicked.replace('#','');
	
	if(
		elementClicked == '#aboutus' || 
		elementClicked == '#history' || 
		elementClicked == '#hoh' || 
		elementClicked == '#founders' || 
		elementClicked == '#brandedviralcontent' || 
		elementClicked == '#eventmarketing' || 
		elementClicked == '#digitalsocialmedia' || 
		elementClicked == '#celebrityoutreach' || 
		elementClicked == '#grassrootsmarketing' || 
		elementClicked == '#licensingendorsements' || 
		elementClicked == '#partnershipdevelopment' || 
		elementClicked == '#productplacement' || 
		elementClicked == '#publicrelations' || 
		elementClicked == '#specialevents' || IsNumeric(page)) 
		// well don't do ajax on sub-menu clicks
	{
		$('ul.sub-menu li.menu-item a').css ({
			'color' : '#FFF'
		});
		$(this).css({
			'color': '#f8f859'
		});
		
		//show event
		if(IsNumeric(page) && page>2000) {

			$('#slide1 div h1').css('color','#FFFFFF');
			h1_event =  elementClicked.replace('#','h1_');	
			$('#slide1 h1').removeClass('active');
			$('#slide1 div.' + h1_event + ' h1').addClass('active');

			ev_content =  elementClicked.replace('#','ev_');	
			$('#content .ev_inner').fadeOut('slow');
			$('#'+ev_content).fadeIn('slow');
		}
		return false;
		// do nothing
	} else if (
		elementClicked == '#alcohol' || 
		elementClicked == '#personal-care' || 
		elementClicked == '#beverages' || 
		elementClicked == '#online' || 
		elementClicked == '#fashion' || 
		elementClicked == '#electronics' || 
		elementClicked == '#corporate' || 
		elementClicked == '#media' || 
		elementClicked == '#packaged-goods' || 
		elementClicked == '#transportation' || 
		elementClicked == '#hospitality' ||
		elementClicked == '#entertainment'
		
	) {
		$("div.clients div.client_float > div").css ({
				'color' : 'white'
		});
		class_ = this.hash;
		class_ = class_.replace('#','');
		$("div.clients div.client_float div." + class_).css ({
				'color' : '#F9F91D'
		});
		return false;
	} else {		
	
		//now do ajax
		$('#spinner').show();
		$('#mycontainer').fadeOut('slow', function() { });
		$('#mywrapper').fadeOut('fast', function() { });
		//wait for hash to set + enjoy that spinner
		setTimeout(function(){		
			myajax();
		},1200);
		if (in_array(elementClicked,a_campaigns) ) return false; //copy
	}

})


	// check to see if hash is set (are we on homepage?)
	var checkhash = window.location.hash;
	//check comment # here
	if(checkhash.lastIndexOf('#comment')!=-1 || checkhash.lastIndexOf('#respond')!=-1 ) {
			return;
		}
	if(checkhash == '') {
		//fade in bg + etc
		$('#mycontainer').fadeIn('slow', function() { });
		setTimeout("$('#carousel_bar').show('slide', {direction:'down'}, 1000);", 1250);
		setTimeout("$('#carousel_nav').fadeIn('fast', function() { });", 2000);
		setTimeout("$('.carousel_cont').fadeIn('fast', function() { });", 2000);
		
	} else {
		//load ajax content
		$('#spinner').show();
		$('#mycontainer').fadeOut('slow', function() { });
		$('#mywrapper').fadeOut('fast', function() { });
		setTimeout(function(){
			myajax();
		},1200);
	}
});

$(document).ready(function(){
	
	// Slide in navigation from left
    setTimeout("$('#access').show('slide', {direction:'left'}, 2000);", 250);
    // set up external links
	$('.story li a').click(function(){
		var slide = $(this).attr('href').substring(1);
		$('#story').anythingSlider(slide);
		return false;
	});
	$('.services li a').click(function(){
		var slide = $(this).attr('href').substring(1);
		$('#services').anythingSlider(slide);
		return false;
	});
    
	//change menu colors on click
	$('#access .menu-item a').click(function() {
		$("#access .menu-item a").css({ 'color': '#fff' });
		$(this).css({ 'color': '#F9F91D' });
	});	

	
	// toggle sub-menus
	$(".menu-item").click(function () {
		$('.menu-item ul').slideUp("slow");
     	$("ul", this).slideToggle("slow");
    });    
});
//-------------------------------
$(document).ready(function(){
	$('.menu-item a').click(function(){

		$('#a-blog').removeClass('blog-active');
		$('#a-blog').addClass('blog-normal');

	    $('.menu-item a').removeClass('active');
	    $(this).addClass('active');
	    ul_parent = $(this).parent();
	    a = $('ul.sub-menu li a',ul_parent ).eq(0);
	    if(a.length>0){     
			if(a.attr('href')!='#alcohol'){ 
	        	a.addClass('active');
			}
	    }else {
	        ul_parent = $(this).parent().parent().parent();			
	        ul_p2 = $(this).parent().parent();
            id = ul_p2.attr('id');                
	        if(ul_parent.length>0 && id!='menu-main-menu'){
	            a = ul_parent.find('a').eq(0).addClass('active');
	        }
	    }
	});
	//campaigns
	$('#chrysler .camp_nav a').click(function(){
	    $('#chrysler .camp_nav a').css({'color':'#ffffff'});
	    $(this).css({'color':'#F9F91D'});
	});
});
