﻿// Installs hooks to add/remove placeholder text on given input element when focused/blurred
jQuery.fn.placeholder = function(text) {
    var waitingClass = 'waiting-for-input';
    var waitingTextKey = 'waiting-text';

    $(this).each(function() {
        var placeholder = text || $(this).val();
        $(this).data(waitingTextKey, placeholder);
        $(this).val(placeholder);
        $(this).addClass(waitingClass);
    });

    $(this).focus(function() {
        if ($(this).hasClass(waitingClass)) {
            $(this).removeClass(waitingClass);
            $(this).val('');
        }
    });

    $(this).blur(function() {
        if ($(this).val() == '') {
            $(this).addClass(waitingClass);
            $(this).val($(this).data(waitingTextKey));
        }
    });
}

function filterByCountry() {
    window.location.href = jQuery.param.querystring(
        window.location.href, 'page=1&countryId=' + jQuery("#countryId").val()
    );
}

function filterByYear() {
    window.location.href = jQuery.param.querystring(
        window.location.href, 'page=1&year=' + jQuery("#year").val()
    );
}

function filterByCountryAndLocationType() {
    window.location.href = jQuery.param.querystring(
        window.location.href, 'page=1&locationTypeId=' + jQuery("#locationTypeId").val() + '&countryId=' + jQuery("#countryId").val()
    );
}

function filterByCountryAndJobArea() {
    window.location.href = jQuery.param.querystring(
        window.location.href, 'page=1&jobAreaId=' + jQuery("#jobAreaId").val() + '&countryId=' + jQuery("#countryId").val()
    );
}

function filterByCountryAndYear() {
    window.location.href = jQuery.param.querystring(
        window.location.href, 'page=1&year=' + jQuery("#year").val() + '&countryId=' + jQuery("#countryId").val()
    );
}

function preload(image) {
    if (document.images) {
        var imageObj = new Image();
        imageObj.src = image;
    }
}


$(document).ready(function () {
    createDropDown();

    $(".dropdown dt a").click(function () {
        $(".dropdown dd ul").toggle();
    });

    $(document).bind('click', function (e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
    });

    $(".dropdown dd ul li a").click(function () {
        var text = $(this).html();
        $(".dropdown dt a").html(text);
        $(".dropdown dd ul").hide();

        var source = $("#source");
        source.val($(this).find("span.value").html())
        selectLanguageSubmit($(this).find("span.value").html());
    });
});

function createDropDown() {
    var source = $("#source");
    var selected = source.find("option[selected]");
    var options = $("option", source);

    $("#language-select").append('<dl id="target" class="dropdown"></dl>')
    $("#target").append('<dt><a href="#">' + selected.text() +
                '<span class="value">' + selected.val() +
                '</span></a></dt>')
    $("#target").append('<dd><ul></ul></dd>')

    options.each(function() {
        $("#target dd ul").append('<li><a href="#">' +
                    $(this).text() + '<span class="value">' +
                    $(this).val() + '</span></a></li>');
    });
}

function selectLanguageSubmit(redirectUrl) {
    window.location = redirectUrl;
}
