$(document).ready(function(){
	
	// Initilize Google search in the sub navigation
	if($('#cse_sub_nav').length && $('#cse_sub_nav').html() == 'Loading' && customSearchControl_sub_nav != undefined) customSearchControl_sub_nav.draw('cse_sub_nav');
	
	// Initilize Google search in the features
	if($('#cse_feature').length && $('#cse_feature').html() == 'Loading' && customSearchControl_feature != undefined) customSearchControl_feature.draw('cse_feature');
	
	// Add images to contanier
	init_images();
	
	// Initilize the expandable sections (option description and special offers)
	init_expandable();
	
});

function init_expandable(){
	
	// Hover
	$('.expandable').hover(function(){
		$(this).css('border','2px solid #ff5a00');
	},function(){
		$(this).css('border','2px solid #f0f0f0');
	});
	
	// Click
	$('.expandable a.expand').click(function(){
		if($(this).text() == $(this).attr('contracted')){
			$(this).text($(this).attr('expanded'));
			$(this).css({color:'black', padding:'3px', backgroundColor:'#f0f0f0'});
			$(this).parent().find('.expanded_content').show();
		}else{
			$(this).text($(this).attr('contracted'));
			$(this).css({color:'blue', padding:'0', backgroundColor:'transparent'});
			$(this).parent().find('.expanded_content').hide();
		}
	});
}

function update_total(){
	
	total = 0;
	
	quantity = jQuery.trim($('#price_table input:first').val());
	if(isInteger(quantity)){
		
		quantity = parseInt(quantity);
		
		$('#price_table input, #price_table select option:selected').each(function(){
			if($(this).attr('price') !== undefined){
				add = false;
				if($(this).attr('type') != 'checkbox'){
					add = true;
				}else if($(this).attr('checked') != ''){
					add = true;
				}
				if(add) total += quantity * parseFloat($(this).attr('price'));
			}
		});
		
		$('#configured_price').slideUp("fast", function(){
			$(this).text(formatCurrency(total));
			$(this).slideDown("fast");						
		});
		
		
		
	}else{
		alert('A non-number is in the quantity field');
	}
}

function init_images(){
	
	// Load in the first image
	if(arr_images.length){
		$("#image_display").css("backgroundImage", 'url(' + path + 'images/products/' + arr_images[0][0] + '_s2.jpg)').attr('title', arr_images[0][1]).click(function(){
			tb_show(arr_images[0][1], path + "images/products/" + arr_images[0][0] + "_s1.jpg", false);
		});
	}
	
	// Create thumbnails
	for(i=0;i<arr_images.length;i++){
		
		el = $(document.createElement('div'));
		el.addClass("image_thumb");
		
		if(!i) el.addClass("image_thumb_hover");
		
		el.css('background-image', 'url(' + path + 'images/products/' + arr_images[i][0] + '_s4.jpg)');
		el.attr('title', arr_images[i][1]);
		el.attr('zoom', arr_images[i][0]);
		
		el.hover(function(){
			
			$("#image_thumbs div").removeClass("image_thumb_hover");
			$(this).addClass("image_thumb_hover");
			$("#image_display").css('background-image', 'url(' + path + 'images/products/' + $(this).attr('zoom') + '_s2.jpg)');
			
		}, function(){
			
			// clear out
			$("#image_thumbs div").removeClass("image_thumb_hover");
			$("#image_thumbs div:first").addClass("image_thumb_hover");
			$("#image_display").css("background-image", 'url(' + path + 'images/products/' + arr_images[0][0] + '_s2.jpg)');
			
		});
		
		el.click(function(){
			tb_show($(this).attr('title'), path + "images/products/" + $(this).attr('zoom') + "_s1.jpg", false);
		});
		
		$("#image_thumbs").append(el);
	}
}