﻿/// <reference path="lib/jquery-1.3.2.min-vsdoc.js" />

var serifgothic = {
    src: '/scripts/fonts/serifgothic.swf'
};

sIFR.activate(serifgothic);

sIFR.replace(serifgothic, {
    selector: '.serifgothic.green',
    forceSingleLine: true,
    wmode: 'transparent',
    fitExactly: true,
    offsetLeft: 0,
    offsetTop: 0,
    css: ['.sIFR-root {color:#008b92;font-weight:bold}',
    '.sIFR-root a {color:#008b92;text-decoration: none; }',
    '.sIFR-root a:hover {color: #008b92; }',
    '.sIFR-root a:visited {color:#008b92; }']
});

sIFR.replace(serifgothic, {
    selector: '.serifgothic.orange',
    forceSingleLine: true,
    wmode: 'transparent',
    fitExactly: true,
    offsetLeft: 0,
    offsetTop: 0,
    css: ['.sIFR-root {color:#fb7c11;font-weight:bold}',
    '.sIFR-root a {color:#fb7c11;text-decoration: none; }',
    '.sIFR-root a:hover {color: #fb7c11; }',
    '.sIFR-root a:visited {color:#fb7c11; }']
});

sIFR.replace(serifgothic, {
    selector: '.serifgothic',
    forceSingleLine: false,
    wmode: 'transparent',
    fitExactly: true,
    offsetLeft: 0,
    offsetTop: 0,
    css: ['.sIFR-root {color:#666;font-weight:bold}',
    '.sIFR-root a {color:#666;text-decoration: none; }',
    '.sIFR-root a:hover {color: #666; }',
    '.sIFR-root a:visited {color:#666; }']
});

sIFR.replace(serifgothic, {
    selector: '.serifgothic',
    forceSingleLine: true,
    wmode: 'transparent',
    fitExactly: true,
    offsetLeft: 0,
    offsetTop: 0,
    css: ['.sIFR-root {color:#666;font-weight:bold}',
    '.sIFR-root a {color:#666;text-decoration: none; }',
    '.sIFR-root a:hover {color: #666; }',
    '.sIFR-root a:visited {color:#666; }']
});

function runPcCheck() {

    var pc = $("#zip_postcode.pc-lookup").val();
    var number = $("#house_number.pc-lookup").val();
    $.ajax({
        type: "GET",
        url: "/_ws/get_postcode",
        contentType: "application/json; charset=utf-8",
        dataType: "text",
        data: "postcode=" + pc + "&house_number=" + number,
        beforeSend: function() {
            $("#activity").addClass("ajax_activity");
        },
        complete: function() {
            $("#activity").removeClass("ajax_activity");
        },
        success: function(json) {
            appendAddress(json);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        }
    });

}

function appendAddress(json) {

    var j = eval('(' + json + ')');
    if (j.City && j.StreetName) {
        $("#street_name.pc-lookup").val(j.StreetName);
        $("#city.pc-lookup").val(j.City);
    }

    $("#street_name.pc-lookup").attr("readonly", null);
    $("#city.pc-lookup").attr("readonly", null);

}

$(function() {

    // postode lookup
    $("#street_name.pc-lookup").attr("readonly", true);
    $("#city.pc-lookup").attr("readonly", true);
    $("#house_number.pc-lookup").blur(function() {
        runPcCheck();
    });

    // IE6 fix against content bleeding
    $(".level2").bgiframe();

    // Menu code
    $("ul.level1 li").mouseover(function() {
        // hide all menu's
        $("#menu .level2").stopTime("auto-hide-menu");
        $("#menu ul.level2").hide();

        $(this).find("ul.level2").show();


        $(this).mouseout(function() {

            $(this).find("ul.level2").oneTime(500, "auto-hide-menu", function() {
                $(this).hide();
            });
        });

    });

    // auto postback
    $(".auto_postback").change(function() {
        $(this).parents('form:first').submit();
    });

    //$(".btn_bestellen").click(function() {
    $("#popup").jqm({ ajax: '@href', trigger: '.btn_bestellen', closeClass: 'btn_close' });
    //});


    initAccountNumber();

    $("#betaal_methodes input:radio[@name='payment_method']").change(function() {
        initAccountNumber();
    });

    $("dt").click(function() {
        if ($(this).hasClass("active")) {
            $("dd").hide();
            $("dt").removeClass("active");
        } else {
            $("dd").hide();
            $("dt").removeClass("active");
            $(this).addClass("active");
            $(this).next("dd").show();
        }
    });

    // product filters
    $(".product_filters a").click(function() {

        if ($(this).hasClass("checked")) {
            // uncheck all
            $(".product_filters a").removeClass("checked");
            filterProducts("");
        } else {
            // uncheck all except this one
            $(".product_filters a").removeClass("checked");
            $(this).addClass("checked");
            filterProducts($(this).attr("href").replace("#", ""));
        }
        return false;
    });

    // form field transform

    // .initials
    $("input.initials").blur(function() {
        this.value = this.value.toUpperCase();
        return mask(this.value, this, '1,3,5,7,9,11,13,15,17,19', '.');
    });
    // .uCamelCase
    $("input.uCamelCase").blur(function() {
        $(this).val(camelcase($(this).val()));
    });
    // .uCase
    $("input.uCase").blur(function() {
        this.value = this.value.toUpperCase();
    });
    // .lCase
    $("input.lCase").blur(function() {
        this.value = this.value.toLowerCase();
    });

});

var jsonFilters = "";

function filterProducts(filter) {

    if (jsonFilters == "") {
        $.get("/_ws/get_filters", function(data) {
            jsonFilters = eval(data);
            applyFilters(filter);
        });

    } else {
        applyFilters(filter);
    }
}

function applyFilters(filter) {

    if (filter == "") {
        $("div.product_block").show();
        return;
    }

    $("div.product_block").hide();

    var products = new Array();

    for (var i = 0; i < jsonFilters.length; ++i) {
        if (jsonFilters[i].Filter == filter) {
            products = jsonFilters[i].Products;
            break;
        }
    }

    for (var i = 0; i < products.length; ++i) {
        $("#prod_" + products[i]).show();
    }

}



function initAccountNumber() {
    if ($("#betaal_methodes input:radio[@name='payment_method']:checked").val() == "pm_1") {
        $("#betaal_methodes #account").show();
    } else {
        $("#betaal_methodes #account").hide();
    }
}

/* text formatting functions */

function changeCase() {



}