// JavaScript Document

$(document).ready(function(){
	$('#footer_nav li a:first').css('border-left','none');
	$('#footer_nav li a:last').css('margin-right','0px');
	$('#main_nav ul li ul li:first-child').css('border-top','1px solid #CCB79B');
	
	if ($('#product_grid').length > 0) {
		productPanels();
	}
	
	if ($('.ngg-gallery-thumbnail').length > 0) {
		galleryPanels();
	}
	
	if ($('.projects .project').length > 0) {
		projectsPanels();
	}
	
	if ($('#slider').length > 0) {
		$("#slider").easySlider({
			auto: true, 
			continuous: true,
			controlsBefore:	'<p id="controls">',
			controlsAfter:	'</p>',
			speed: 1000,
			pause: 4000
		});
	}
	
	if ($('#assembly').length > 0) {
		$("#assembly").easySlider({
			auto: true, 
			continuous: true,
			controlsBefore:	'<p id="controls-assembly">',
			controlsAfter:	'</p>',
			speed: 1000,
			pause: 4000
		});
	}
	if ($('form').length > 0) {
		validateMe();
	}
});

function productPanels() {
	// Set up some variables for later
	var total_width = $('#lower_content').width();
	var padding = $('#lower_content').css('padding-left');
	padding = parseInt(padding) * 2;
	var avail_width = total_width - padding;
	//alert (avail_width);
	$('#product_grid .entry > ul').addClass('products_list');
	$('ul.products_list > li').addClass('product_panel');
	$('ul.products_list ul').addClass('product_details');
	
	if (!($.browser.msie  && parseInt($.browser.version) == 7)) {
		// There is a bug in TinyMCE in Wordpress 3.2.1 (min). It doesn't nest indented
		// lists properly. For each nested ul on product details, they need to be pulled back 
		// into the li above them, making them a child not a sibling, for correct html and 
		// to put a list inside the product panel properly.
		// The EXCEPTION is IE7, which ignores the problem. It must be handled separately until
		// the bug is fixed (promised in V 3.3.
		$('ul.product_details').each(function(index) {
			$(this).appendTo($(this).prev());
			$('li:first', this).addClass('product_panel_title');
		});
	} else {
		$('ul.product_details').each(function(index) {
			$('li:first', this).addClass('product_panel_title');
		});
	}

	//Make all product panels the same height
	var height = 0;
	$('li.product_panel').each(function(index) {
		var h = $(this).height();
		if (h > height) {
			height = h;
		}
	});
	$('#product_grid li.product_panel').height(height);
	
	$('#product_grid li.product_panel img').each(function(index) {
		if ($(this).parent('a').length > 0) {
			$(this).hover(function () {
    			$(this).closest('li.product_panel').addClass('panel_over');
  			}, 
			function () {
    			$(this).closest('li.product_panel').removeClass('panel_over');
			});
		}
	});
}

function galleryPanels() {
	$('.ngg-gallery-thumbnail img').each(function(index) {
		if ($(this).parent('a').length > 0) {
			$(this).hover(function () {
    			$(this).closest('div.ngg-gallery-thumbnail').addClass('panel_over');
  			}, 
			function () {
    			$(this).closest('div.ngg-gallery-thumbnail').removeClass('panel_over');
			});
		}
		var div_h = $(this).closest('div.ngg-gallery-thumbnail').height();
		var img_w = $(this).width();
		var img_h = $(this).height();
		var top = Math.round(((div_h - img_h) / 2) - 10);
			top = top + 'px';
		if (img_w > img_h) {
			$(this).css(
				{'position': 'relative', 'top': top}
			)
		}
	});
}

function projectsPanels() {
	var height = 0;
	$('.projects .project').each(function(index) {
		var h = $(this).height();
		if (h > height) {
			height = h;
		}
	});
	$('.projects .project').height(height);
}

function showpic(whichpic) {
 if (document.getElementById) {
  document.getElementById('placeholder1').src = whichpic.href;
  document.getElementById('placeholder2').childNodes[0].nodeValue = whichpic.title;
  return false;
 } else {
  return true;
 }
}

function validateMe() {
	var test = $('#samplereq').attr('checked');
	if (!test){
		$("#sample_details").hide();
	}
	$("#samplereq").click(function () {
		$("#sample_details").toggle();
	});
	$("#arch_form_form").validate();
}
