// Clear the field
function clear_field(id){
	document.getElementById(id).value = "";
}

// Rollover Image
function rollover_image(id,image){
	document.getElementById(id).src = image;
}

// Rollout Image
function rollout_image(id,image){
	document.getElementById(id).src = image;
}

// jQuery
$(document).ready(function(){
	// Load in the main menu.
	$("#submenu").load("/wp-content/themes/back-to-school/header-menu.php");
	
	// Slight differences for Windows.
	if($.client.os == "Windows"){
		$("body").css('font-family',"Courier New");
		$("#mainmenu li a").css('font-weight',"bold");
		$("h2").css("font-weight","bold");
		$("h3").css("font-weight","bold");
		$("h4").css("font-weight","bold");
		$("#title-bar").css("font-weight","bold");
		
		// Opera on Windows
		if($.client.browser == "Opera"){
			$("#footer li a").css("background-color","#eeeeee");
			$("#footer li a").css("line-height","20px");
		}
    }
    
	// Remove from the Wishlist page.
	$(".remove-from-wishlist-page").click(function(){
		// Get the product ID.
		product_id = $(this).attr("name");
		
		// Remove the wishlist item.
		$("#wishlist-response").load("/wp-content/plugins/wishlists/api/remove.php?product_id="+product_id);
		
		// Hide the whole row.
		$(".wishlist_"+product_id).hide();	
	});
	
	// Add to Wishlist
	if(document.getElementById("add-to-wishlist")){
		// Hide the add button if the product is already added.
		$(".hide").hide();
		
		// Dynamically check and load based on the user.
		post_id = $('#post_id').val();
		$('#add-to-wishlist').load(
			"/wp-content/plugins/wishlists/api/add-to-wishlist.php?post_id="+post_id
			,function(){
				// Add to Wishlist
				$("#add-to-wishlist-button").click(function(index){
					// Get the product ID.
					product_id = $("#add-to-wishlist-button").attr("name");
					
					// Save the wishlist item.
					$("#add-to-wishlist-response").load("/wp-content/plugins/wishlists/api/add.php?product_id="+product_id);
					
					// Toggle the buttons.
					$("#add-to-wishlist-button").hide();
					$("#remove-from-wishlist-button").show();
				});
			
				// Remove a product from the wishlist.
				$("#remove-from-wishlist-button").click(function(index){
					// Get the product ID.
					product_id = $("#remove-from-wishlist-button").attr("name");
					
					// Remove the wishlist item.
					$("#add-to-wishlist-response").load("/wp-content/plugins/wishlists/api/remove.php?product_id="+product_id);
					
					// Toggle the buttons.
					$("#add-to-wishlist-button").show();
					$("#remove-from-wishlist-button").hide();
				});
			}
		);
	}
	
	// Load More Products
	if(document.getElementById("load-more-products")){
		var block = 0;
		
		$("#load-more-products").click(function(index){
			// Tally a block.
			block = (block+1);
			
			// Get the query details.
			load_more = $("#load-more-"+block).val();
			
			// Place a loading GIF in the placeholder.
			$("#more-products-placeholder-"+block).html('<img border="0" height="19" src="/wp-content/themes/back-to-school/images/ajax-loader.gif" width="220">');
			
			// Load more products, passing along the query and block.
			$("#more-products-placeholder-"+block).load("/wp-content/plugins/collections/api/more-products.php?load_more="+load_more+"&block="+block,function(){
				// IE8: #wrapper wasn't resizing to the new height.
				$("#wrapper").css("height","");
				$("#wrapper").css("height","auto");
			});
		});
	}
	
	// Product Page Ordering
	if(document.getElementById("product-sort-direction")){
		$("#product-sort-direction").change(function(){
			if($("#product-sort-direction").val() != ""){
				document.product_sort.submit();
			}
		});
	};
	
	// Product Details Page Thumbnails
	if(document.getElementById("large-detail-image")){
		$('.thumbnail-detail-page').click(function(index){
			// Figure out which image is currently the large image.
			small_image = $("#large-detail-image").attr('src').replace('.jpg','-127x169.jpg');
			
			// Get the large version of the image.
			large_image = $(this).attr('src').replace('-127x169','');
			
			// Place the small version of the image into the thumbnail placeholder.
			$(this).attr('src',small_image);
			
			// Place the large version of the selected thumbnail into the large image placeholder.
			$("#large-detail-image").attr('src',large_image);
		});
	};
	
	// Reveal Brands
	if(document.getElementById("reveal-brands")){
		// Hide the brands by default.
		//$("#brand-breakdown").css("display","none");
		
		// Toggle the brands on click.
		/*
		$("#reveal-brands").click(function(){
			brand_displayed = $("#brand-breakdown").css("display");
			
			if(brand_displayed.toString() == "none"){
				$("#brand-breakdown").css("display","");
			}else{
				$("#brand-breakdown").css("display","none");
			}
		});
		*/
		
		// Hide the brands by default.
		$("#brand-breakdown").hide();
		
		// Toggle the brands on click.
		$("#reveal-brands").click(function(){
			if($("#brand-breakdown").is(":hidden")){
				$("#brand-breakdown").slideDown(1000);
			}else{
				$("#brand-breakdown").slideUp(1000);
			}
		});
	};
	
	// Save the current product page so the product can be closed this page can be returned to.
	if(document.getElementById("product_return_url")){
		// Get the product_return_url
		product_return_url = $('#product_return_url').val();
		
		// 
		$("#product_return_url_message").load("/wp-content/themes/back-to-school/save-product-session.php?product_return_url="+product_return_url);
	}
	
	// Now check where the close button should link back to.
	if(document.getElementById("product_close_button")){
		// Load the close button.
		$("#product_close_button").load("/wp-content/themes/back-to-school/product-close-button.php");
	}
});
