var path = 'http://'+document.domain;

	$.ajaxSetup ({
		cache: false
	});
	
	ajax_load = "<img class='loading' src='/images/load.gif' alt='loading...' />";
	
$(document).ready(function(){

	var curBody = $('div.container').get(0).getAttribute('rel');

	if( curBody != 'no_slide') {
		$('a.home').attr('href', '#home');
		$('a.feat').attr('href', '#products');
		$('a.event' ).attr('href', '#events');
		$('a.promo').attr('href', '#promotions');
		$('a.jobs').attr('href', '#careers');
		$('a.story').attr('href', '#story');
		// Home Page Postcards
		$('div#pc_list').cycle({ 
			speed:    500,
			// timeout: 13000,
			timeout: 0,
			pause:  1,
			pager:  '#pc_nav' 
		});
	}	
	if( curBody == 'no_slide') {
		$('a.home').attr('href', '/home/index#home');
		$('a.feat').attr('href', '/home/index#products');
		$('a.event' ).attr('href', '/home#events');
		$('a.promo').attr('href', '/home/index/#promotions');
		$('a.jobs').attr('href', '/home/index/#careers');
		$('a.story').attr('href', '/home/index/#story');
	}		

	checkPath(path);
	initAccordian();

	// Sliding Navigation
	$("a.home").click(function () { 
		slider('0');
		stickNav('home');			
	});

	$("a.feat").click(function () { 
		loadProducts();
		slider('-2000');
		stickNav('feat');
	});
		
	$("a.event").click(function () { 
		loadEvents();
		slider('-4000');
		stickNav('event');
	});

	$("a.promo").click(function () { 
		loadPromotions();
		slider('-6000');
		stickNav('promo');
	});
	$("a.jobs").click(function () { 
		loadCareers();
		slider('-8000');
		stickNav('jobs');
	});
	$("a.story").click(function () { 
		loadStory();
		slider('-10000');
		stickNav('story');
	});
	
	// Clear search box
	$("input.r_zip_home").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});

});


function slider(distance) {
	$('div#slide_wrp').animate( { marginLeft: distance }, {duration:1000} );
}


function stickNav(title){
	$('ul#main_nav li a').removeClass('selected');
	$('li.' + title + ' a').addClass('selected');
}


// loads Permalinks content
function checkPath (path) {
	var url = document.location.toString();
	if (url.match('#')) {
		var urlAnchor = url.split('#')[1];
	};

	if (urlAnchor == 'home') {
		$('div#slide_wrp').css("margin-left","0px");
		stickNav('home');
	};
	
	if (urlAnchor == 'products') {
		$('div#slide_wrp').css("margin-left","-2000px");
		loadProducts ();
		stickNav('feat');
	};

	if (urlAnchor == 'events') {
		$('div#slide_wrp').css("margin-left","-4000px");
		loadEvents ();
		stickNav('event');
	};

	if (urlAnchor == 'promotions') {
		$('div#slide_wrp').css("margin-left","-6000px");
		loadPromotions ();
		stickNav('promo');
	};
	if (urlAnchor == 'careers') {
		$('div#slide_wrp').css("margin-left","-8000px");
		loadCareers ();
		stickNav('jobs');
	};
	if (urlAnchor == 'story') {
		$('div#slide_wrp').css("margin-left","-10000px");
		loadStory ();
		stickNav('story');
	};

}


function initAccordian() {
	// Accordian Features Boxes
	$("div.feature_box h1").hover(function (){
		$(this).css("cursor", "pointer");
	});
	$("div.feature_box h1").click(function (){
		var curFeat = $(this).next('div');		
		if ( $(curFeat).css("display") == "none") {
			$('div.feature_body').slideUp(250);		
			$(this).next().slideDown(250);
		}
	});
}


// Load Seperate Pages
function loadProducts () {
	$('div#slide02 div.container div.page_bg').html(ajax_load).load( path + '/home/p_products',  function(){
		initAccordian();
		setTimeout("CSBfleXcroll('content_product')" ,500);	
    //setTimeout(CSBfleXcroll('content_product') ,500);
	//	CSBfleXcroll('content_product');

	});
}
function loadEvents () {
	$('div#slide03 div.container div.page_bg').html(ajax_load).load( path + '/home/p_events',  function(){
		$('ul.gallery a').lightBox();
		CSBfleXcroll('content_events');
		initAccordian();
	});

}
function loadPromotions () {
	$('div#slide04 div.container div.page_bg').html(ajax_load).load( path + '/home/p_promotions',  function(){
		initAccordian();
		setTimeout("CSBfleXcroll('content_promotions')" ,500);	

	});
}
function loadCareers() {
	$('div#slide05 div.container div.page_bg').html(ajax_load).load( path + '/home/p_careers',  function(){
		CSBfleXcroll('content_careers');
		initAccordian();
	});
}
function loadStory() {
	$('div#slide06 div.container div.page_bg').html(ajax_load).load( path + '/home/p_story',  function(){
		CSBfleXcroll('content_story');
		initAccordian();
	});
}
