﻿$(document).ready(function () {
    jQuery('a.minibutton, a.iconbutton').bind({
        mousedown: function () {
            jQuery(this).addClass('mousedown');
        },
        blur: function () {
            jQuery(this).removeClass('mousedown');
        },
        mouseup: function () {
            jQuery(this).removeClass('mousedown');
        }
    });
    $('.thumbnail').tipsy({ gravity: 'sw' });
});

function mhCountry() {
    country = $("#MHCountry").val();
    switch (country) {
        case 'au': url = "http://www.campertravel.com.au"; break;
        case 'nz': url = "http://www.campertravel.co.nz"; break;
        case 'us': url = "http://www.campertravelusa.com"; break;
        case 'ca': url = "http://www.campertravelcanada.com"; break;


    }
    if (country != "ca" || country != "") {
        window.location.replace(url);
        return false;
    }


}
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function VehiclePopup(url, width, height) {
    width = typeof (width) != 'undefined' ? width : 940;
    height = typeof (height) != 'undefined' ? height : 700;

    popupWindow = window.open(
       url + "&popup=true", '_blank', 'height=' + height + ',width=' + width + ',left=0,top=10,resizable=no,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=no')
}


function BindCookies() {
    var b, lp, ld, lps, lds, dp, dd, dlt, at, sh;

    //GET

    //berth
    //see search form
    b = (getParameterByName('b')) ? getParameterByName('b') : $.cookie("b");
    if (b) {
        if ($("input[name='Berth']").is(":radio")) {
            $("input[name='Berth'][value='" + b + "']").attr("checked", true);
        } else {
            $("#Berth").val(b);
        }
        $.cookie("b", b, { expires: 1, path: '/' });
    } else {
        $.cookie("b", 2, { expires: 1, path: '/' });
    }
    //pickup
    lp = (getParameterByName('lp')) ? getParameterByName('lp') : $.cookie("lp");
    if (lp) {
        $("#ddlDepotsPickup").val(lp);
        $.cookie("lp", lp, { expires: 1, path: '/' });
    } else {
        $.cookie("lp", $("#ddlDepotsPickup").val(), { expires: 1, path: '/' });

    }
    //dropoff
    ld = (getParameterByName('ld')) ? getParameterByName('ld') : $.cookie("ld");
    if (ld) {
        $("#ddlDepotsDropOff").val(ld);
        $.cookie("ld", ld, { expires: 1, path: '/' });
    } else {
        $.cookie("ld", $("#ddlDepotsDropOff").val(), { expires: 1, path: '/' });
    }
    //datepickup
    dp = (getParameterByName('dp')) ? getParameterByName('dp') : $.cookie("dp");
    if (dp) {
        $("#txtPickupDate").val(dp);
        $.cookie("dp", dp, { expires: 1, path: '/' });
    } else {
        $.cookie("dp", $("#txtPickupDate").val(), { expires: 1, path: '/' });
    }
    //datedropoff
    dd = (getParameterByName('dd')) ? getParameterByName('dd') : $.cookie("dd");
    if (dd) {
        $("#txtDropOffDate").val(dd);
        $.cookie("dd", dd, { expires: 1, path: '/' });
    } else {
        $.cookie("dd", $("#txtDropOffDate").val(), { expires: 1, path: '/' });
    }
    //license
    dlt = (getParameterByName('dlt')) ? getParameterByName('dlt') : $.cookie("dlt");
    if (dlt) {
        $("#lic-type").val(dlt)
        $.cookie("dlt", dlt, { expires: 1, path: '/' });
    } else {
        $.cookie("dlt", $("#lic-type").val(), { expires: 1, path: '/' });
    }

    //vehicle type
    vt = (getParameterByName('vt')) ? getParameterByName('vt') : $.cookie("vt");
    if (vt) {
        $("input[name='w-type'][value='" + vt + "']").attr("checked", true);
        $.cookie("vt", vt, { expires: 1, path: '/' });
    } else {
        $.cookie("vt", 0, { expires: 1, path: '/' });
    }

    //Automatic
    at = (getParameterByName('at') != "") ? getParameterByName('at') : $.cookie("at");
    if (eval(at)) {
        $("#cbAutomatic").attr('checked', 'checked');
        $.cookie("at", true, { expires: 1, path: '/' });
    } else {
        $.cookie("at", false, { expires: 1, path: '/' });
    }
    //Shower
    sh = (getParameterByName('sh') != "") ? getParameterByName('sh') : $.cookie("sh");
    if (eval(sh)) {
        $("#cbShower").attr('checked', 'checked');
        $.cookie("sh", true, { expires: 1, path: '/' });
    } else {
        $.cookie("sh", false, { expires: 1, path: '/' });
    }

    //SET

    //berth
    if ($("input[name='Berth']").is(":radio")) {
        $("input[name='Berth']").click(function () {
            b = $(this).val();
            $.cookie("b", b, { expires: 1, path: '/' });
        })
    } else {
        $("#Berth").change(function () {
            b = $(this).val();
            $.cookie("b", b, { expires: 1, path: '/' });
        })
    }
    //pickup
    $("#ddlDepotsPickup").change(function () {
        lp = $(this).val();
        $.cookie("lp", lp, { expires: 1, path: '/' });
    })
    $("#ddlStatesPickup").change(function () {
        lps = $(this).val();
        $.cookie("lps", lps, { expires: 1, path: '/' });
        GetPickupCities();
    })
    //dropoff
    $("#ddlDepotsDropOff").change(function () {
        ld = $(this).val();
        $.cookie("ld", ld, { expires: 1, path: '/' });
    })
    $("#ddlStatesDropOff").change(function () {
        lds = $(this).val();
        $.cookie("lds", lds, { expires: 1, path: '/' });
        GetDropOffCities();

    })
    //datepickup
    $("#txtPickupDate").change(function () {
        dp = $(this).val();
        $.cookie("dp", dp, { expires: 1, path: '/' });
    })
    //datedropoff
    $("#txtDropOffDate").change(function () {
        dd = $(this).val();
        $.cookie("dd", dd, { expires: 1, path: '/' });
    })

    //driver's license
    $("#lic-type").click(function () {
        dlt = $(this).val();
        $.cookie("dlt", dlt, { expires: 1, path: '/' });
    })

    //vehicle type
    $("input[name='w-type']").click(function () {
        vt = $(this).val();
        $.cookie("vt", vt, { expires: 1, path: '/' });
    })

    //automatic
    $("#cbAutomatic").click(function () {
        at = $(this).is(":checked");
        $.cookie("at", at, { expires: 1, path: '/' });
    })
    //shower
    $("#cbShower").click(function () {
        sh = $(this).is(":checked");
        $.cookie("sh", sh, { expires: 1, path: '/' });
    })

    var c, l, a, s, h, k, n;
    c = (getParameterByName('c')) ? getParameterByName('c') : $.cookie("c");
    if (c) { $.cookie("c", c, { expires: 30 }); }
    l = (getParameterByName('l')) ? getParameterByName('l') : $.cookie("l");
    if (l) { $.cookie("l", l, { expires: 30 }); }
    a = (getParameterByName('a')) ? getParameterByName('a') : $.cookie("a");
    if (a) { $.cookie("a", a, { expires: 30 }); }
    s = (getParameterByName('s')) ? getParameterByName('s') : $.cookie("s");
    if (s) { $.cookie("s", s, { expires: 30 }); }
    h = (getParameterByName('h')) ? getParameterByName('h') : $.cookie("h");
    if (h) { $.cookie("h", h, { expires: 30 }); }
    k = (getParameterByName('k')) ? getParameterByName('k') : $.cookie("k");
    if (k) { $.cookie("k", k, { expires: 30 }); }
    n = (getParameterByName('n')) ? getParameterByName('n') : $.cookie("n");
    if (n) { $.cookie("n", n, { expires: 30 }); }

}

function activePage_Open(CSSClass) {
    $("div." + CSSClass).addClass(CSSClass + "-h");
}
function styleCheckboxes() {
    var checkboxHeight = "25";
    var radioHeight = "25";
    var selectWidth = "190";
    var Custom = {
        init: function () {
            var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
            for (a = 0; a < inputs.length; a++) {
                if ((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
                    span[a] = document.createElement("span");
                    span[a].className = inputs[a].type;

                    if (inputs[a].checked == true) {
                        if (inputs[a].type == "checkbox") {
                            position = "0 -" + (checkboxHeight * 2) + "px";
                            span[a].style.backgroundPosition = position;
                        } else {
                            position = "0 -" + (radioHeight * 2) + "px";
                            span[a].style.backgroundPosition = position;
                        }
                    }
                    inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                    inputs[a].onchange = Custom.clear;
                    if (!inputs[a].getAttribute("disabled")) {
                        span[a].onmousedown = Custom.pushed;
                        span[a].onmouseup = Custom.check;
                    } else {
                        span[a].className = span[a].className += " disabled";
                    }
                }
            }
            inputs = document.getElementsByTagName("select");
            for (a = 0; a < inputs.length; a++) {
                if (inputs[a].className == "styled") {
                    option = inputs[a].getElementsByTagName("option");
                    active = option[0].childNodes[0].nodeValue;
                    textnode = document.createTextNode(active);
                    for (b = 0; b < option.length; b++) {
                        if (option[b].selected == true) {
                            textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
                        }
                    }
                    span[a] = document.createElement("span");
                    span[a].className = "select";
                    span[a].id = "select" + inputs[a].name;
                    span[a].appendChild(textnode);
                    inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                    if (!inputs[a].getAttribute("disabled")) {
                        inputs[a].onchange = Custom.choose;
                    } else {
                        inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
                    }
                }
            }
            document.onmouseup = Custom.clear;
        },
        pushed: function () {
            element = this.nextSibling;
            if (element.checked == true && element.type == "checkbox") {
                this.style.backgroundPosition = "0 -" + checkboxHeight * 3 + "px";
            } else if (element.checked == true && element.type == "radio") {
                this.style.backgroundPosition = "0 -" + radioHeight * 3 + "px";
            } else if (element.checked != true && element.type == "checkbox") {
                this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
            } else {
                this.style.backgroundPosition = "0 -" + radioHeight + "px";
            }
            //Event Tracking for Search form
            if (element.id == "cbAutomatic") {
                if (typeof(_gaq) != "undefined"){
                    _gaq.push(['_trackEvent', 'Transmission', 'Checkbox', '/Transmission/Checked']);
                }
            }
            if (element.id == "cbShower") {
                if (typeof (_gaq) != "undefined") {
                    _gaq.push(['_trackEvent', 'Shower', 'Checkbox', '/Shower/Checked']);
                }
            }
        },
        check: function () {
            element = this.nextSibling;
            if (element.checked == true && element.type == "checkbox") {
                this.style.backgroundPosition = "0 0";
                element.checked = false;
            } else {
                if (element.type == "checkbox") {
                    this.style.backgroundPosition = "0 -" + checkboxHeight * 2 + "px";
                } else {
                    this.style.backgroundPosition = "0 -" + radioHeight * 2 + "px";
                    group = this.nextSibling.name;
                    inputs = document.getElementsByTagName("input");
                    for (a = 0; a < inputs.length; a++) {
                        if (inputs[a].name == group && inputs[a] != this.nextSibling) {
                            inputs[a].previousSibling.style.backgroundPosition = "0 0";
                        }
                    }
                }
                element.checked = true;
            }
        },
        clear: function () {
            inputs = document.getElementsByTagName("input");
            for (var b = 0; b < inputs.length; b++) {
                if (inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
                    inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight * 2 + "px";
                } else if (inputs[b].type == "checkbox" && inputs[b].className == "styled") {
                    inputs[b].previousSibling.style.backgroundPosition = "0 0";
                } else if (inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
                    inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight * 2 + "px";
                } else if (inputs[b].type == "radio" && inputs[b].className == "styled") {
                    inputs[b].previousSibling.style.backgroundPosition = "0 0";
                }
            }
        },
        choose: function () {
            option = this.getElementsByTagName("option");
            for (d = 0; d < option.length; d++) {
                if (option[d].selected == true) {
                    document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
                }
            }
        }
    }
    window.onload = Custom.init;

}
function GetPickupCities() {
    $("#ddlDepotsPickup option").remove();
    $.post('/SearchForm/GetCitiesByState', { State: $("#ddlStatesPickup").val() }, function (args) {
        var aCities = args;
        $(aCities).each(function (i, s) {
            $("#ddlDepotsPickup").append("<option value='" + s + "'>" + s + "</option>");
        })
    })
}
function GetDropOffCities() {
    $("#ddlDepotsDropOff option").remove();
    $.post('/SearchForm/GetCitiesByState', { State: $("#ddlStatesDropOff").val() }, function (args) {
        var aCities = args;
        $(aCities).each(function (i, s) {
            $("#ddlDepotsDropOff").append("<option value='" + s + "'>" + s + "</option>");
        })
    })
}
