// ---------- set page heights and widths
var allPageTags=new Array();
var bposts=new Array();
var a, b, c, old_hash, pcid, pid, bpos, epos, loaded;
var theme_dir="http://www.littletoncyclery.com/wp-content/themes/littletoncyclery/";
// product category loader vars
var category, subcategory, brand, title;
var p_old=new Array();
bpos=1;
function loader(theClass) {
//	var allPageTags=document.getElementsByTagName("*"); 
//	for (i=0; i<allPageTags.length; i++) { 
//		if (allPageTags[i].className==theClass) { 
//			allPageTags[i].style.height=document.documentElement.clientHeight+"px";
////			allPageTags[i].style.width=document.documentElement.clientWidth-40+"px";
//		} 
//	}
	c=window.location.hash.substring(2);
//	window.status=a;
	$("#pane_products").load(theme_dir+"inc/load-products.php");
	if(!c){
		a='home';
		$('#nav_home').toggleClass("on");
	}else{
		pageLoad(c,'/');
	}
	$("#pane_blog").load(theme_dir+"inc/load-blog.php",{},function(){ $('a.blog').fancybox({'titleShow':false}); });
	$(".previous").hide('slow');
}
$(window).load(function(){
	$(".roll").hover(
		function(){
			this.src=this.src.replace("-off","-on");
		},
		function(){
			this.src=this.src.replace("-on","-off");
		}
	);		
	$('a.search').live("click", function(){
		$.fancybox.showActivity();
		atts=$(this).attr('rel');
		atts="products:::"+atts;
		pageLoad(atts,':::');
//		window.location.hash="/products/"+s+"/"+p;
	});
	$('img.preload').each(function () {
		this.src=this.src.replace('blank.gif',$(this).attr('rel'));
	});
	var urls=[ 'slide_home-02','slide_home-03','slide_home-04','slide_home-05','slide_home-06','slide_home-07','slide_home-08' ];
	$.preload( urls, {
		base:theme_dir+'img/',
		ext:'.jpg',
		onComplete:function( data ){
			var img=new Image();
			img.src=data.image;
			$('#home_show').append(img);
		},
		onFinish:function(){
			var cycleMe=setInterval( slide_home, 9000 );
			//slide_home();
		}
	});
	$("body").append('<span class="sr_outer"><span class="sr_con"><span class="search_results"></span></span></span>');
	$(".search_results").hide();
	$("#pane_directions").html('<iframe width="880" height="510" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?oe=utf-8&amp;client=firefox-a&amp;ie=UTF8&amp;q=littleton+cyclery&amp;fb=1&amp;gl=us&amp;hq=cyclery&amp;hnear=littleton&amp;ei=W28-S8CJKJPSMMOl7coH&amp;ved=0CBoQpQY&amp;hl=en&amp;view=map&amp;cid=5695570226846447693&amp;ll=39.62365619840741,-105.01743078231812&amp;spn=0.006295,0.006295&amp;iwloc=A&amp;z=15&amp;output=embed"></iframe>');
	
	// Add the keypress handler
	// Using keyup because keypress doesn't recognize the backspace key
	// and that's kind of important.
	$("input[name='s']").keyup(liveResults);
	
	// Prevent browsers from doing autocomplete on the search field
	$("input[name='s']").attr('autocomplete', 'off');
	
	// Hide the search results when the search box loses focus
	//$("*").click(LiveSearch.handleClicks);

//	$(".fancy").hover( 
//        function () { 
//            pid=$(this).attr('rel'); 
//       } 
//    ); 	
//	$(".slide_products").mouseout( 
//        function () { 
//            $('.'+pcid).mouseover();
//        } 
//    ); 	
	$("a.fancy").fancybox({ 
		'hideOnContentClick': false, 
		'padding': 10, 
		'autoDimensions': false,
		'width': 800, 
		'height': 500, 
		'scrolling':'auto',
		'transitionIn':'none',
		'transitionOut':'none',
		'centerOnScroll': true, 
		'onClosed': function() { 
			$('body').css({overflow: 'auto'});
			$("#fancy_content").empty();
			if(old_hash) window.location.hash=old_hash;
		},
		'onStart': function() {
			$('body').css({overflow: 'hidden'});
			if(old_hash) var is_prod=old_hash.split('/');
//			if(!loaded) old_hash=window.location.hash;
//			else loaded=0;
			if(pid) window.location.hash=window.location.hash+"/"+pid;
			$(function() {
				$(".zoom").miniZoomPan({
						sW: 500,
						sH: 293,
						lW: 2000,
						lH: 1171
					})
			});
	} 
    });
	$('a.slide').click(function(){
		$('#nav_'+a).toggleClass("on");
		$.scrollTo( this.hash, 3000, { easing:'easeInOutExpo' } );
		a=this.hash;
		a=a.replace('#','');
		window.location.hash="/"+a;
		$('#nav_'+a).toggleClass("on");
		return false;
	});
//	$('a.slide_products').click(function(){
//		$('#pane').scrollTo( this.hash, 1300 );
//		b=this.hash;
//		b=b.replace('#','');
//		pcid=b;
//		window.location.hash="/products/"+b;
//		return false;
//	});
	// product category loader
	$('a.procat').live("click", function(){
		var base="/products/";
		var rel=$(this).attr('rel');
		var ttl=$(this).attr('title');
		var tid=$(this).attr('id');
		var ttlmod=ttl.toLowerCase().replace(/ /g,"__");
		eval(rel+'="'+ttlmod+'"');
		if(p_old[rel]!='') $(p_old[rel]).removeClass('on');
		p_old[rel]="a#"+tid;
		$(p_old[rel]).addClass('on');
		if(rel=='category') window.location.hash=base+category;
		if(rel=='subcategory') window.location.hash=base+category+'/'+subcategory;
		if(rel=='brand') window.location.hash=base+category+'/'+subcategory+'/'+brand;
		if(rel=='title'){
			$.fancybox.showActivity();
			old_hash=base+category+'/'+subcategory+'/'+brand;
			window.location.hash=base+category+'/'+subcategory+'/'+brand+'/'+title;
		}else{
			$("#"+rel).html("<img src='"+theme_dir+"img/icon-loading.gif' />");
		}
		$.post(theme_dir+"inc/load-products.php", { send: rel+':::'+ttl },
			function(data){
				if(rel=='title'){
					$("#box_pop").html(data);
					$('#box_link').click();
					a2a_init("page");
				}else{
					$("#"+rel).html(data);
				}
			}
		);
	});
	$('a.subscribe').live("click", function(){
		$.fancybox.showActivity();
		var rel=$(this).attr('rel');
		$.post(theme_dir+"inc/load-about.php", { send: rel },
			function(data){
				$("#box_pop").html(data);
				$('#box_link').click();
			}
		);
	});
	$('a.aboutcat').live("click", function(){
		$.fancybox.showActivity();
		var base="/about/";
		var rel=$(this).attr('rel');
		old_hash="/about";
		window.location.hash=base+rel;
		$.post(theme_dir+"inc/load-about.php", { send: rel },
			function(data){
				$("#box_pop").html(data);
				$('#box_link').click();
			}
		);
	});
	$('a.blog_more').live("click", function(){
		$.fancybox.showActivity();
		var base="/blog/";
		var rel=$(this).attr('rel');
		old_hash="/blog";
		window.location.hash=base+rel;
		$.post(theme_dir+"inc/load-blog_more.php", { send: rel },
			function(data){
				$("#box_pop").html(data);
				$('#box_link').click();
				a2a_init("page");
			}
		);
	});
	$('a.servicescat').live("click", function(){
		$.fancybox.showActivity();
		var base="/services/";
		var rel=$(this).attr('rel');
		old_hash="/services";
		window.location.hash=base+rel;
		$.post(theme_dir+"inc/load-about.php", { send: rel },
			function(data){
				$("#box_pop").html(data);
				$('#box_link').click();
			}
		);
	});
	$('a.next').click(function(){
		$(".previous").show('slow');
		var tpos="#post_"+bpos;
		if($.inArray(bpos, bposts)<0){
			$.post(theme_dir+"inc/load-blog.php", { pos: bpos },
				function(data){
					if(data){
						$("#pane_blog").append(data).scrollTo( tpos, 1300 );
						$('a.blog').fancybox({'titleShow':false});
						epos=bpos;
					}else{
						$(".next").hide('slow');
						bpos--;
						epos=bpos;
					}
				}
			);
		}else{
			if(bpos<epos){
				$("#pane_blog").scrollTo( tpos, 1300 );
			}else{
				$(".next").hide('slow');
				bpos--;
			}
		}
		bposts.push(bpos);
		bpos++;
	});
	$('a.previous').click(function(){
		$(".next").show('slow');
		bpos--;
		bpos--;
		var tpos="#post_"+bpos;
		$("#pane_blog").scrollTo( tpos, 1300 );
		if(bpos<=0) $(".previous").hide('slow');
		bpos++;
	});
//	easing='Expo';
//	$('#nav_sub').lavaLamp({
//		fx: 'easeInOut'+easing,
//		speed: 1000,
//		autoReturn: false,
//		returnHome: false,
//		homeLeft: -1000,
//		homeTop: -25,
//		homeWidth:700,
//		homeHeight:10
//	});
});
function show(where,what){
	$(where).html($(what).html());
}
function liveResults(){
	$(".search_results").html("<img src='"+theme_dir+"img/icon-loading-white.gif' />");	
	$.post(theme_dir+"inc/load-search.php", { s: $('#s').val() },
		function(data){
			var next=data.split(':::');
			//if (next[0]=='error') $("#add_status").html(next[1]);
			$('.search_results').html(data);
//			if(data=='No results.'){
//				$(".search_results").hide('slow');
//			}
		}
	);
	$(".search_results").show('slow');
}
function closeSearch(){
	$('.search_results').hide('slow');
};

// ---------- bg fader
var s=0;
function slide_home() {
	if(s==7){
		$('#home .page_content').scrollTo( 0, 800, { easing:'easeInOutExpo' } );
		s=0;
	}else{
		$('#home .page_content').scrollTo( {top:'0', left:'+=950px'}, 800, { easing:'easeInOutExpo' } );
		s++;
	}
//	var $active=$('#slideshow img.active');
//    if ( $active.length == 0 ) $active=$('#slideshow img:last');
//    var $next= $active.next().length ? $active.next()
//        : $('#slideshow img:first');
//    $active.addClass('last-active');
//    $next.css({opacity: 0.0})
//        .addClass('active')
//        .animate({opacity: 1.0}, 2000, function() {
//            $active.removeClass('active last-active');
//        });
}
function sendApp(){
	$("body").find("#submit-app").attr("disabled", "disabled");
	$.post(theme_dir+"inc/load-application.php", $("body").find("#form-app").serialize(),
		function(data){
			var next=data.split(':::');
			$("body").find("#status-app").html(next[1]);
			if (next[0]=='error'){
				$("body").find("#submit-app").attr("disabled", "");
			}else{
				$("body").find("#form-app").hide('slow');
			}
		}
	);
};
function sendContact(){
	$.fancybox.showActivity();
	$("#submit").attr("disabled", "disabled");
	$.post(theme_dir+"inc/load-contact.php", $("#form-contact").serialize(),
		function(data){
			$.fancybox.hideActivity();
			var next=data.split(':::');
			$("#status").html(next[1]);
			if (next[0]=='error'){
				$("#submit").attr("disabled", "");
			}else{
				$("#form-contact").hide('slow');
			}
		}
	);
};
function pageLoad(atts,brk){
		att=atts.split(brk);
		$(".search_results").hide('slow');
		$('#s').val('');
		if(a) $('#nav_'+a).toggleClass("on");
		a=att[0];
		if(a) $('#nav_'+a).toggleClass("on");
		if(a=='blog'){
			$.scrollTo('#'+a, 3000, { onAfter:function(){
				var base="/"+a+"/";
				var rel=att[1];
				old_hash="/"+a;
				if(rel) $.post(theme_dir+"inc/load-blog_more.php", { send: rel },
					function(data){
						$("#box_pop").html(data);
						$("#box_link").click();
					}
				)
			}});
		}else if(a!='products'){
			$.scrollTo('#'+a, 3000, { onAfter:function(){
				var base="/"+a+"/";
				var rel=att[1];
				old_hash="/"+a;
				if(rel) $.post(theme_dir+"inc/load-about.php", { send: rel },
					function(data){
						$("#box_pop").html(data);
						$("#box_link").click();
					}
				)
			}});
		}
		if(a=='products') $.scrollTo('#'+att[0], 3000 ,
			{ 
				onAfter:function(){
					$.fancybox.hideActivity();
					if(!att[1]) return;
					rel="category";
					category=att[1].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
					hash="/products/"+att[1].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
					window.location.hash=hash;
					old_hash=hash;
					if(p_old[rel]!='') $(p_old[rel]).removeClass('on');
					p_old[rel]="a#p_category_"+att[1].toLowerCase().replace(/__/g,"").replace(/ /g,"").replace(/'/g,"").replace(/.qu./g,"");
					$(p_old[rel]).addClass('on');
					$("#"+rel).html("<img src='"+theme_dir+"img/icon-loading.gif' />");
					$.post(theme_dir+"inc/load-products.php", { send: rel+':::'+att[1].replace(/.qu./g,"'").replace(/__/g," ") },
						function(data){
							$("#"+rel).html(data);
							if(!att[2]) return;
							rel="subcategory";
							subcategory=att[2].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
							hash=old_hash+"/"+att[2].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
							window.location.hash=hash;
							old_hash=hash;
							if(p_old[rel]!='') $(p_old[rel]).removeClass('on');
							p_old[rel]="a#p_subcategory_"+att[2].toLowerCase().replace(/__/g,"").replace(/ /g,"").replace(/'/g,"").replace(/.qu./g,"");
							$(p_old[rel]).addClass('on');
							$("#"+rel).html("<img src='"+theme_dir+"img/icon-loading.gif' />");
							$.post(theme_dir+"inc/load-products.php", { send: rel+':::'+att[2].replace(/.qu./g,"'").replace(/__/g," ") },
								function(data){
									$("#"+rel).html(data);
									if(!att[3]) return;
									rel="brand";
									brand=att[3].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
									hash=old_hash+"/"+att[3].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
									window.location.hash=hash;
									old_hash=hash;
									if(p_old[rel]!='') $(p_old[rel]).removeClass('on');
									p_old[rel]="a#p_brand_"+att[3].toLowerCase().replace(/__/g,"").replace(/ /g,"").replace(/'/g,"").replace(/.qu./g,"");
									$(p_old[rel]).addClass('on');
									$("#"+rel).html("<img src='"+theme_dir+"img/icon-loading.gif' />");
									$.post(theme_dir+"inc/load-products.php", { send: rel+':::'+att[3].replace(/.qu./g,"'").replace(/__/g," ") },
										function(data){
											$("#"+rel).html(data);
											if(!att[4]) return;
											rel="title";
											title=att[4].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
											hash=old_hash+"/"+att[4].toLowerCase().replace(/ /g,"__").replace(/.qu./g,"'");
											window.location.hash=hash;
											if(p_old[rel]!='') $(p_old[rel]).removeClass('on');
											p_old[rel]="a#p_title_"+att[4].toLowerCase().replace(/__/g,"").replace(/ /g,"").replace(/'/g,"").replace(/.qu./g,"");
											$(p_old[rel]).addClass('on');
											$.post(theme_dir+"inc/load-products.php", { send: rel+':::'+att[4].replace(/.qu./g,"'").replace(/__/g," ") },
												function(data){
													$("#box_pop").html(data);
													$('#box_link').click();
												}
											);
										}
									);
								}
							);
						}
					);
				}
			});
}
