﻿/// <reference path="jquery-1.4.1.min.js" />
var pl = '<img src="/images/preload.gif" />';

function maskInput() {
    var key_code = window.event.keyCode;
    var oElement = window.event.srcElement;
    if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
        if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
            if (key_code > 95) key_code -= (95 - 47); oElement.value = oElement.value;
        }
        else if (key_code == 8) {
            oElement.value = oElement.value;
        } else if (key_code != 9) {
            event.returnValue = false;
        }
    }
}

function populateDropdown(select, data) {
    var sel = "";
    $.each(data, function (id, option) {
        sel = sel + '<option value="' + option.Id + '">' /*((option.Id != '0') ? '">' : '" style="font-weight:bold;">')*/ + option.Name + '</option>';
    });
    select.html(sel);
}

function populateGroupDropList(select, data) {
    var sel = "";
    var groups = uniqueArray(JSONtoArray(data, 'GroupName'));
    for (var i = 0; i < groups.length; i++) {
        if (groups[i].length > 0)
            sel = sel + '<optgroup label="' + groups[i] + '">';
        $.each(data, function (id, option) {
            if (option.GroupName == groups[i]) {
                sel = sel + '<option value="' + option.Id + '">' /*((option.Id != '0') ? '">' : '" style="font-weight:bold;">')*/ + option.Name + '</option>';
            }
        });
        if (groups[i].length > 0)
            sel = sel + '</optgroup>';
    }
    select.html(sel);
}


function JSONtoArray(data, column) {
    var arr = [];
    $.each(data,
    function (index, obj) {
        arr[index] = obj[column];
    });
    return arr;
}

function uniqueArray(arr) {
    var res = [];
    var exs = false;
    for (var i=0; i < arr.length; i++) {
        for (var j = 0; j < res.length; j++) {
            if (res[j] == arr[i]) {
                exs = true;
                break;
            }
        }
        if (exs == false)
            res[res.length] = arr[i];
        exs = false;
    }
    return res;
}


function lang() {
    if ($(document).context.location.pathname == "/")
        return "ru";
    else 
        return $(document).context.location.pathname.substring(1, 3);
}


function authorize() {
    $.post("/function/authorize/", { language: lang(), referrer: document.referrer }, function (data) {
        if (data.Logged == true) {
            $("#auth_lof").show();
            $("#auth_acc").show();
            $("#auth_acc span").html(data.UserName);
            $("#menu_ry").removeClass("first");
            $("#menu_my").show();
        }
        else {
            $("#menu_my").removeClass("first");
            $("#auth_lon").show();
            $("#auth_reg").show();
        }
        $("#country_name").html(data.CountryName);
    });
}


function menu() {
    var $menu = $('#ldd_menu');

    $menu.children('li').each(function () {
        var $this = $(this);
        var $span = $this.children('a');
        $span.data('width', $span.width());

        $this.bind('mouseenter', function () {
            $(this).addClass("active");
            $menu.find('.ldd_submenu').stop(true, true).hide();
            $span.stop().animate(300, function () {
                $this.find('.ldd_submenu').slideDown(300);
            });
        }).bind('mouseleave', function () {
            $this.find('.ldd_submenu').stop(true, true).hide();
            $span.stop().animate(300);
            $(this).removeClass("active");
        });
    });
}


$(document).ready(function () {
    menu();
    authorize();


    $('ul#jcloud-tags').jcloud({
        radius: 120,
        size: 20,
        step: 2,
        speed: 50,
        flats: 2,
        clock: 10,
        areal: 100,
        splitX: 100,
        splitY: 100,
        colors: ['#000000', '#DD2222', '#2267DD', '#2A872B', '#872A7B', '#CAC641']
    });


    $("a[rel='loc']").live('click', function (event) {
        $("#d_country").dialog("open");
    });


    $("#d_country").dialog({
        autoOpen: false,
        width: 300,
        height: 150,
        modal: true,
        resizable: false,
        position: 'center',
        draggable: false,
        buttons: {
            "Выбрать": function () {
                $(this).dialog("close");
            },
            "Отказаться": function () {
                $(this).dialog("close");
            }
        }
    });
});
