var offset =0;
$(function(){
	
	if (!($.browser.msie  && (parseInt($.browser.version) == 8 || parseInt($.browser.version) == 7))) {
		$(".tabHeader:not(:first)").insertAfter($(".tabHeader:first"));
		$(".tabHeader:first").addClass("active");
		$(".tabContent:not(:first)").hide();
		$(".tabHeader").click(function(){
			$(".tabContent").hide();
			$("#"+$(this).attr("tab")).show();
			$(".tabHeader").removeClass("active");
			$(this).addClass("active");
		});
	}
	$(".vidPop").click(function(){
		if($(this).is(".disabled"))
			return false;
			
		var youtubeId = $(this).attr("vid");
		$.modal("<div><iframe width='560' height='345' src='http://www.youtube.com/embed/"+youtubeId+"?rel=0&autoplay=1' frameborder='0' allowfullscreen></iframe></div>",{close:true,overlayClose:true});
			try{ _gaq.push(['_trackEvent', 'Videos', 'Play', $("h1").eq(0).text()]); } catch(err) {}
	}); 
	
	$("#prodThumbs img").click(function(){
									$("#mainImg").attr("src",$(this).attr("lrg"));
								});
								
	$(".photometry").live('click',function(){
									$.modal('<div id="photometryPop">Our Photometry Sheets are not available online, but can be made available <a href="'+site_url+'/contact/">on&nbsp;request</a>.</div>',{close:true,overlayClose:true});
								});
								
	$("#productBar a").live('click', function(event){
									event.preventDefault();
									$("#prodBlock,  #renderVid, #btmLft").append('<div class="ajaxLoading"></div>');
									$.ajax({
									  url: $(this).attr("href")+"?ajax",
									  dataType: 'json',
									  success: function( data){
										ajaxLoadProd(data);
									  }
									});
									
								});
								
	$("#prodCatNav a").click(function(event){
									event.preventDefault();
									$("#prodCatNav a").removeClass('active');
									$("#prodCatNav").css("position","relative").append('<div class="ajaxLoading"></div>');
									$("#prodBlock, #renderVid, #btmLft").append('<div class="ajaxLoading"></div>');
									$(this).addClass('active');
									urlToLoad = $(this).attr("href")+"?ajax";
									flyOut($("#productBar a:last"));				
									
									
								});
								
	$('#hpSlider').cycle({
			fx: 'scrollLeft',
			pause: true,
			pager:  '#slideNav',
			speed: 1500,
			timeout:5000			
		});
	$("#expandedFooter").hide();
	$("#moreFooter").click(function(){
							if($("#expandedFooter").is(":visible")){
								$("#expandedFooter").slideUp()
								$(this).text("More Footer");
							} else {
								$("#expandedFooter").slideDown();
								$('html, body').animate({
													scrollTop: $("#expandedFooter").offset().top
												}, 1000);
								
								$(this).text("Hide Footer");
							}
					});
});

function flyIn(elm)
{
	var position = elm.position();
	elm.animate({'left': offset},{
	duration: 850, 
	easing: 'easeOutBounce', 
	complete: function(){
											offset += 150;
											if($(this).next().length != 0)
												flyIn($(this).next());

										}});

}
function flyOut(elm)
{
	var position = elm.position();
	elm.css({'position':'absolute','left':position.left});
	elm.animate({'left': +1000}, 400,'swing', function(){
											if($(this).prev().length != 0)
												flyOut($(this).prev());
											else
												$.ajax({
												  url: urlToLoad,
												  dataType: 'json',
												  success: function( data){
													ajaxLoadcat(data);
												  }
												});	
										});
	
}


function ajaxLoadProd(data){
	$("#prodBlock h2").text(data.name);
	$("#prodBlock p").text(data.excerpt);
	$("a.viewMore").attr("href", data.permalink);
	if(data.video != "")
		$(".vidPop").attr("vid", data.video).removeClass("disabled");
	else
		$(".vidPop").addClass("disabled");
	$("#btmLft p").text(data.bottom_left);
	$("#btmLft img").attr("src",data.bottom_left_img[0]);
	$("#temperature p").text(data.temperature);
	$(".prodBlockImg").attr("src",data.images.cat_main_img);
	
	if($("#prodAttachments").length != 0)
	{
		// Prod attachments for support page
		attachments = '';
		for(x in data.attachments){
			attachments+= '<li><a target="_blank" href="'+data.attachments[x].location+'"><img src="'+data.attachments[x].icon+'" alt="'+data.attachments[x].caption+'" />'+data.attachments[x].caption+'</a></li>';
		}
		attachments+= '<li class="photometry"><img src="'+template_url+'/images/file-icons/photometry.gif" alt="photometry">Photometry</li>';

		$("#prodAttachments ul").html(attachments);
	}
	$(".ajaxLoading").remove();
}

function ajaxLoadcat(data){
	var newBar = '';
	for(x in data.products){
		
		newBar += '<a href="'+data.products[x].permalink+'"><img src="'+data.products[x].thumbnail+'" /><span>'+data.products[x].name+'</span></a>';
	}
	$("#productBar").html(newBar);
	offset = 0;
	$("#productBar a").css({"position":"absolute","left":1000});
	flyIn($("#productBar a:first"));

	
	
	$.ajax({
	  url: $("#productBar a:first").attr("href")+"?ajax",
	  dataType: 'json',
	  success: function( data){
		ajaxLoadProd(data);
	  }
	});

}



















