/*jslint browser: true */
/*global jQuery: true */

var prBudgie = 0,
    seedingBudgie = 0;

function display_total_budget() {
    var mediaamount = jQuery('#slider-range-min').slider('value');
    jQuery('#totalamount').val( (mediaamount + prBudgie));
    jQuery('#pramount').val( seedingBudgie);
}

function estimator_mailme() {
    jQuery('#mailme').hide();
    var postdata = jQuery('#estimator-results-mail-form').serialize();
    jQuery.post("please_mail_me",postdata,function (data) {
            jQuery('#mailme-ret').text(data);
            if (data.slice(0,5) === "Somet") {
                jQuery('#mailme').show();
            }
    },"text");

    var s = new Image(1,1);
    s.src = "http://www.googleadservices.com/pagead/conversion/1070116300/?value=1&amp;label=Q4gKCOjSXxDM26L-Aw&amp;guid=ON&amp;script=0";
}

function set_prBudgie() {
    var seeding;
    jQuery("#yyseeding").find("input[type='radio']").each(function () {
            if (this.checked) {
                seeding = this.id;
            }
    });
    seedingBudgie = {threehalves:900,fivehalves:1500,eighthalves:2400}[seeding];
    prBudgie = 600 + seedingBudgie;
    display_total_budget();
}

function estimator_panel (kind) {
    try{
        // Google Analytics
        pageTracker._trackPageview("/show_est_"+kind);
    } catch(e) {}
    jQuery('#bespoke-panel').hide();
    jQuery('#estimator-panel').show();
    if (kind === 'deluxe') {
        jQuery('#yyseeding').show();
        jQuery('#reportcost-div').show();
        jQuery('#pramount-div').show();
        jQuery('#dlx-est-copy').show();
        jQuery('#std-est-copy').hide();
        jQuery('#estimator-legend').text('Deluxe Estimator');
        document.getElementById('yystdordx').value = 'deluxe';
        set_prBudgie();
    } else {
        jQuery('#yyseeding').hide();
        jQuery('#reportcost-div').hide();
        jQuery('#pramount-div').hide();
        jQuery('#dlx-est-copy').hide();
        jQuery('#std-est-copy').show();
        jQuery('#estimator-legend').text('Standard Estimator');
        document.getElementById('yystdordx').value = 'standard';
        prBudgie = 0;
        display_total_budget();
    }
}
function bespoke_panel () {
    // Google Analytics
    try {
        pageTracker._trackPageview("/show_bespoke");
    } catch(e) {}
    jQuery('#estimator-panel').hide();
    jQuery('#bespoke-panel').show();
}
jQuery(function() {

var jqAmount = jQuery('#mediaamount'),
    jqTotala = jQuery('#totalamount'),
    jqSlider = jQuery('#slider-range-min');

jqSlider.slider({
    range: 'min',
    value: 2500,
    min: 500,
    max: 10000,
    step: 500,
    position: top,
    closeOnEscape: true,
    slide: function(event, ui) {
        jqAmount.val( ui.value);
        jQuery('#totalamount').val((ui.value + prBudgie));
    }
});

jqAmount.val( jqSlider.slider('value'));
display_total_budget();

jQuery('#estimator-results').show().dialog({
    autoOpen: false,
    modal: true,
    title: 'Estimator',
    width: '900px',
    close: function (even, ui) {
        /* Damn IE6. Select boxes show right through the dialog box. */
        jQuery('#estimator-panel').find('select').show();
    }
});

});

function printEstimator() {
    window.print();
}

function shiny_dialog(data) {
    var jqPoppy = jQuery('#estimator-results');
    jQuery('#busy').hide();
    jqPoppy[0].innerHTML = data;
    jqPoppy.dialog('open');
    /* Damn IE6. Select boxes show right through the dialog box. */
    jQuery('#estimator-panel').find('select').hide();
    shiny_going = false;
}

var shiny_going = false;
function shinybutton() {
    var interest, postdata = { yystdordx:1 }, thing;

    if (shiny_going)
        return;

    for (thing in postdata) {
        postdata[thing] = document.getElementById(thing).value;
    }

    for (thing in {interest1:1,interest2:1,interest3:1,geotarget:1,cttype:1}) {
        select = document.getElementById(thing);
        try {
            postdata[thing] = select.options[select.selectedIndex].value;
            if ((postdata.interest1 === "") &&
                (postdata.interest2 === "") &&
                (postdata.interest3 === "")) {
                throw "first interest is blank";
            }
        } catch (error) {
            jQuery('#est-error').text("Please pick at least one interest to target.");
            return;
        }
    }

    if (postdata.yystdordx === 'deluxe') {
        jQuery("#yyseeding").find("input[type='radio']").each(function () {
                if (this.checked) {
                    postdata.seeding = this.value;
                }
        });
    }

    postdata.amount = document.getElementById('totalamount').value;
    postdata.media_amount = jQuery('#slider-range-min').slider('value');   

    shiny_going = true;
    jQuery('#busy').show();
    jQuery('est-error').text("");
    jQuery.post('estimator_query',postdata,shiny_dialog,'html');
}

jQuery(function () {
    if (window.location.hash == '#estimator-panel-deluxe') {
        estimator_panel('deluxe');
    }
    if (window.location.hash == '#estimator-panel-standard') {
        estimator_panel('standard');
    }
    if (window.location.hash == '#bespoke-panel') {
        bespoke_panel();
    }
});
