/*
 * Ajax functions to make the product detail page extra snazzy.
 */
$(function() {
    
    // Hide the help text
	$("#options_form div.help").css("display", "none");

	// Make the options do ajax calls
    $("#options_form select").change(updatePostageTable);
    
    // Make the help links work
    $("a.help").show("slow").click(function() {
        help_text = $("#" + this.id.substring(5)).animate({opacity: 'toggle'});
        return false;
    })
	
	activatePostageTable();
    updatePostageTable();
    
}); 


/*
 * Updates the postage table.
 */
function updatePostageTable() {
    // Creates the request action
    query_string = $("#options_form").formSerialize();
    request_uri = new String(window.location).split("?")[0];
    action = request_uri + "?" + query_string;
    // Get the new content
    deactivatePostageTable();
    $("img.loader").fadeIn();
    $("#content-secondary-ajax").fadeTo("normal", 0.25);
    $("#postage-options").fadeTo("normal", 0.25);
    $.ajax({
        dataType: "text",
        url: action,
        success: function(data) {
            result = $(data);
            $("#content-secondary-ajax").html($("#content-secondary-ajax", result).html());
            $("#postage-options").html($("#postage-options", result).html());
            $("#content-secondary-ajax").fadeTo("normal", 1);
            $("#postage-options").fadeTo("normal", 1, function() {
                activatePostageTable();
            })
            $("img.loader").fadeOut();
        },
        error: function() {
            // alert("The price information could not be retrieved.");
            $("img.loader").fadeOut();
        }
    });
}

    
/*
 * Makes the highlight work on the postage table.
 */
function activatePostageTable() {

	if (!$("table.postage-options").hasClass("disabled")) {

		// Make the table highlight work
		$("td.postage-left").hover(function() {
			if (!($("button", this).attr("disabled"))) {
				$(this).css("background", "#f3f8fb").css("text-decoration", "none");
				$("#postage-right" + this.id.substring(12)).css("background", "#f3f8fb").css("text-decoration", "none");
			}
		}, function() {
			$(this).css("background", "#bed7ec").css("text-decoration", "none");
			$("#postage-right" + this.id.substring(12)).css("background", "#d6e6f3").css("text-decoration", "none");
		})
		
		// Make the table highlight work
		$("td.postage-right").hover(function() {
			if (!($("button", this).attr("disabled"))) {
				$(this).css("background", "#f3f8fb").css("text-decoration", "none");
				$("#postage-left" + this.id.substring(13)).css("background", "#f3f8fb").css("text-decoration", "none");
			}
		}, function() {
			$(this).css("background", "#d6e6f3").css("text-decoration", "none");
			$("#postage-left" + this.id.substring(13)).css("background", "#bed7ec").css("text-decoration", "none");
		})
	
		// Make the postage hover work.
		$("#postage-options button").hover(function() {
			postage = $("#postage_description_" + this.className.substring(5));
			this.oldHtml = postage.html();
			postage.html(this.title + "&nbsp");
		}, function() {
			$("#postage_description_" + this.className.substring(5)).html(this.oldHtml);
		})
		
		jQuery.each(jQuery.browser, function(i, val) {
            if(i=="msie" && jQuery.browser.version.substr(0,1)=="8") {
                $("table.postage-options button").click(function() {
                    var value = $(this).prev().attr("value");
                    deactivatePostageTable();
                    $.ajax({
                       url: "/checkout/place-order/",
                       type: "post",
                       data: {price: value},
                       success: function() {
                            window.location = "/checkout/";
                       }
                    });
                    return false;
                });
            }
        });
		
	}
	
}

/*
 * Makes the postage table temporarily non-functional.
 */
function deactivatePostageTable() {
	$("#postage-options button").css("cursor", "default").click(function() {
		return false;
	})
	$("td.postage-left").css("cursor", "default").hover(function() {
		return false;
	}, function() {
		return false;
	})
	$("td.postage-right").css("cursor", "default").hover(function() {
		return false;
	}, function() {
		return false;
	})
}

