﻿
$( document ).ready(function (e) {

    $(".charitySelect").change(function (e) {
       //if ($( this ).val() == "other") {
       //     $( this ).hide();
       //     $( this ).siblings(".charityOther").show().focus();
       // } else {

            //get element by id
            var obj = document.getElementById("ToolTipList");
            
            var Tooltips = obj.value.split("||");
            var strToolTip = "";

            //cycle through tooltips to find the right one for the charity selected.
            for(var i = 0; i < Tooltips.length; i++) {
	            var Tooltip = Tooltips[i].split(";;");
	            
	            if ($( this ).val() == Tooltip[0]) {
	                strToolTip = Tooltip[1];
	                break;
	            }
	            
            }
            
            //if no tooltip found then the charity is not a branch one!
            if (strToolTip != "") {
                $( this ).hide();
                $( this ).siblings(".charityAllowBranch").attr("title", strToolTip);
                $( this ).siblings(".charityAllowBranch").tipsy({gravity:'s'});                
                $( this ).siblings(".charityAllowBranch").show().focus();
            }
        //}
    });
    
    $(".buttonUpdateWine").hide();
    $(".selectBottleQty").live("change", function () { 
    
        $(this).parent().submit();
    });
    
    $(".wine .more").show();
    
    handleAcceptTermsAndConditions($("#summary"));
    
    $("#cart-background.hidden").fadeTo(0,0);
    $("#cart").fadeTo(0,0.9);

});

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}



$(".formBuyWine").live("submit", function (e) {

    e.preventDefault();

    var formBuyWine = $(this);
    
    //alert(formBuyWine.find('.charitySelect').length + " " + formBuyWine.find('.charityAllowBranch').length + " '" + formBuyWine.find('.charityAllowBranch').val() + "'");
    
    if ((! formBuyWine.find('.charitySelect').attr('type') == 'hidden') & formBuyWine.find('.charityAllowBranch').is(':visible') & formBuyWine.find('.charityAllowBranch').val() == '') {
        jQuery.facebox('Please enter your selection where you would like the donation to be made to.');
        
    } else if (formBuyWine.find('.charitySelect').attr('type') == 'select-one' & formBuyWine.find('.charitySelect').val() == '') {
        jQuery.facebox('Please select a charity from the drop down list before continuing.');
    
    } else if (formBuyWine.find('.charitySelect').length > 0 & formBuyWine.find('.charityAllowBranch').is(':visible') & formBuyWine.find('.charityAllowBranch').val() == '') { 
        jQuery.facebox('Please enter your selection where you would like the donation to be made to.');
                    
    } else {
    
        
        var loader = formBuyWine.find(".loading");
        
        loader.fadeIn();

        $.ajax({
            type: "POST",
            url: formBuyWine.attr("action"),
            dataType: "html",
            data: formBuyWine.serialize() + "&call=ajax",
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
                alert(textStatus);
                alert(errorThrown);

            } , 
            success: function(result)
            {
                loader.clearQueue();       
                loader.fadeOut();

                $("#cart-background").css("visibility", "visible");
                $("#cart-background").fadeTo("normal",1);
                $.scrollTo("#bottleInsertionPoint", 800, { onAfter: function () { 
                    var bottle = $(result);
                    bottle.hide();
                    $("#bottles").append(bottle);
                    bottle.find(".buttonUpdateWine").hide();
                    bottle.fadeIn();
                } });
                
                $("#paymentDetails").fadeOut();  

            }
        });
        
    }
    
});


$(".formUpdateWine").live("submit", function (e) {

    e.preventDefault();
    
    var formUpdateWine = $(this);
    var loader = formUpdateWine.find(".loading");
    
    loader.fadeIn();

    $.ajax({
        type: "POST",
        url: formUpdateWine.attr("action"),
        dataType: "html",
        data: formUpdateWine.serialize() + "&call=ajax",
        success: function(result)
        {
            loader.clearQueue();       
            loader.fadeOut();

            bottleUpdated = $(result);

            formUpdateWine.parent().parent().replaceWith(bottleUpdated)

            bottleUpdated.find(".buttonUpdateWine").hide();
            bottleUpdated.find(".price").animate({ backgroundColor: "#4b0704" }, 300);
            bottleUpdated.find(".price").animate({ backgroundColor: "#ffffff" }, 300);
            
            $("#paymentDetails").fadeOut();  

        }
    });
    
});


$(".formRemoveWine").live("submit", function (e) {

    e.preventDefault();
    
    var formRemoveWine = $(this);
    var loader = formRemoveWine.find(".loading");
    
    loader.fadeIn();

    $.ajax({
        type: "POST",
        url: formRemoveWine.attr("action"),
        dataType: "html",
        data: formRemoveWine.serialize() + "&call=ajax",
        success: function(result)
        {
            loader.clearQueue();       
            loader.fadeOut();

            formRemoveWine.parent().parent().fadeOut(function () { formRemoveWine.parent().parent().remove() });
            $("#paymentDetails").fadeOut();  

        }
    });
    
});



$(".formUpdateDelivery").live("submit", function (e) {

    e.preventDefault();
    
    var formUpdateDelivery = $(this);
    var loader = formUpdateDelivery.find(".loading");
    
    loader.fadeIn();

    $.ajax({
        type: "POST",
        url: formUpdateDelivery.attr("action"),
        dataType: "html",
        data: formUpdateDelivery.serialize() + "&call=ajax",
        success: function(result)
        {
            loader.clearQueue();       
            loader.fadeOut();


            $("#summary").replaceWith(result);
            handleAcceptTermsAndConditions($("#summary"));
            Cufon.refresh("h5");           
        }
    });
    
});



function handleAcceptTermsAndConditions(summary) {

    var checkAcceptTermsAndConditions = summary.find("#checkAcceptTermsAndConditions");
    summary.find("#buttonAcceptTermsAndConditions").hide();
    


    //checkAcceptTermsAndConditions.change(processTermsAndConditions);
    checkAcceptTermsAndConditions.click(processTermsAndConditions);
    
}


function processTermsAndConditions () {

        var summary = $("#summary");
        
        var formAcceptTermsAndConditions = summary.find("#formAcceptTermsAndConditions");
        var loader = formAcceptTermsAndConditions.find(".loading");
        
        loader.fadeIn();

        $.ajax({
            type: "POST",
            url: formAcceptTermsAndConditions.attr("action"),
            dataType: "html",
            data: formAcceptTermsAndConditions.serialize() + "&call=ajax",
            success: function(result)
            {
                loader.clearQueue();       
                loader.fadeOut();

                formAcceptTermsAndConditions.fadeOut("normal", function () {
                     summary.find("#formPayPal").fadeIn();
                });
                
            }
        });

}

