Wpjb = {

    State: null,

    LogoImg: null,

    Lang: {},

    Listing: [],

    ListingId: null,

    Discount: null,

    AjaxRequest: null,



    calculate: function() {

        var listing = null;

        var id = Wpjb.ListingId;

        for(var i in Wpjb.Listing) {

            if(Wpjb.Listing[i].id == id) {

                listing = Wpjb.Listing[i];

                break;

            }

        }



        var discount = "0.00";

        var total = listing.price;

        if(Wpjb.Discount) {

            if(Wpjb.Discount.type == 2) {

                if(Wpjb.Discount.currency != listing.currency) {

                    alert(Wpjb.Lang.CurrencyMismatch);

                } else {

                    discount = Wpjb.Discount.discount;

                    total -= discount;

                }

            }

        }



        if(total < 0) {

            total = 0;

        }



        var symbol = listing.symbol;

        var price = new Number(listing.price);

        jQuery("#wpjb_listing_cost").html(symbol+price.toFixed(2));

        discount = new Number(discount);

        jQuery("#wpjb_listing_discount").html(symbol+discount.toFixed(2));

        total = new Number(total);

        jQuery("#wpjb_listing_total").html(symbol+total.toFixed(2));

    }

}



jQuery(function() {



    // Change State: select or input[type=text]

    var altState = jQuery('<input type="text" />')

        .hide()

        .attr("id", "job_state_text")

        .attr("value", Wpjb.State)

        .attr("name", "wpjb_temp_state");



    jQuery("#job_state").after(altState);

    jQuery("#wpjb-input-job_state").hide();

    

    jQuery("#job_country").bind("change", function() {

    	//console.log('testing..');

        var stateSelect = jQuery("#job_state");

        var stateInput = jQuery("#job_state_text");

		var stateSelectCanada = jQuery("#job_state_canada");

		var exceptCanada = jQuery("#wpjb-input-job_state");	

		var stateCanadaContainer = jQuery("#wpjb-input-job_state_canada");		

		

        if(jQuery(this).val() == 840) {

        	

        	exceptCanada.show();

        	stateCanadaContainer.hide();

		} else if( jQuery(this).val() == 124 ){

			stateCanadaContainer.show();

			exceptCanada.hide();

            	

        }

    });



    jQuery("#job_country").trigger("change");



    // Insert image

    if(Wpjb.LogoImg != null) {

        var logoWrap = jQuery("<div></div>");

        var logoImg = jQuery("<img />");

        logoImg.attr("alt", "");

        logoImg.attr("src", Wpjb.LogoImg);

        logoWrap.append(logoImg);

        jQuery("#wpjb-input-company_logo").append(logoWrap);

    }



    // Coupon handling

    var listingDiv = jQuery("#wpjb-input-listing");

    var table = jQuery("<table></table>");

    table.attr("id", "wpjb_pricing");

    var tbody = jQuery("<tbody></tbody>");



    var tr1 = jQuery("<tr></tr>");

    var td11 = jQuery("<td></td>").append(Wpjb.Lang.ListingCost+":");

    var span1 = jQuery("<span></span>")

        .attr("id", "wpjb_listing_cost")

        .append(Wpjb.Lang.SelectListingType);

    var td12 = jQuery("<td></td>").append(span1);



    var tr2 = jQuery("<tr></tr>");

    var td21 = jQuery("<td></td>").append(Wpjb.Lang.Discount+":");

    var span2 = jQuery("<span></span>")

        .attr("id", "wpjb_listing_discount")

        .append(Wpjb.Lang.SelectListingType);

    var td22 = jQuery("<td></td>").append(span2);



    var tr3 = jQuery("<tr></tr>").attr("id", "wpjb_table_total");

    var td31 = jQuery("<td></td>").append(Wpjb.Lang.Total+":");

    var span3 = jQuery("<span></span>")

        .attr("id", "wpjb_listing_total")

        .append(Wpjb.Lang.SelectListingType);

    var td32 = jQuery("<td></td>").append(span3);



    tr1.append(td11).append(td12);

    tr2.append(td21).append(td22);

    tr3.append(td31).append(td32);

    tbody.append(tr1).append(tr2).append(tr3);

    table.append(tbody);

    listingDiv.append(table);



    var check = jQuery('<input type="button" />');

    check.attr("value", Wpjb.Lang.Check);

    check.attr("id", "wpjb_button_check");

    check.bind("click", function() {

        var data = {"code": jQuery("#coupon").val()};

        jQuery.ajax({

            url: Wpjb.AjaxRequest,

            dataType: 'json',

            data: data,

            type: "POST",

            success: function(data) {

                if(data.isError) {

                    alert(data.error);

                    return;

                }

                Wpjb.Discount = data;

                jQuery("#coupon").attr("readonly", "readonly");

                jQuery("#wpjb_button_remove").css("display", "inline");

                jQuery("#wpjb_button_check").hide();

                Wpjb.calculate();

            }

        });



    });

    var remove = jQuery('<input type="button" />');

    remove.hide();

    remove.attr("value", Wpjb.Lang.Remove);

    remove.attr("id", "wpjb_button_remove");

    remove.bind("click", function() {

        Wpjb.Discount = null;

        jQuery(this).hide();

        jQuery("#coupon").removeAttr("readonly").val("");

        jQuery("#wpjb_button_check").css("display", "inline");

    })

    jQuery("#coupon").css("width", "70%").after(check).after(remove);

    if(jQuery("#coupon").val() && jQuery("#coupon").val().length > 0) {

        check.trigger("click");

    }



    jQuery("#wpjb-input-listing input[type=radio]").bind("click", function() {

        Wpjb.ListingId = jQuery(this).val();

        Wpjb.calculate();



    });

    jQuery("#wpjb-input-listing input[type=radio]:checked").trigger("click");

    

});
