﻿$.each(jQuery.browser, function (i, val) {
    if (i == 'msie' && val == true && $.browser.version.substr(0, 1) <= 7) {
        $("#container", "li[id^='text']", ".icon").css("filter", "alpha(opacity=0)");
    }
});

function popup(url, width, height, sb) {
    y = (screen.width - width) / 2;
    x = (screen.height - height) / 2;
    win = window.open(url, "", "width=" + width + ",height=" + height + ",left=" + y + ",top=" + x + ",scrollbars=" + sb + ", menubar=no,location=no, directories=no, toolbar=no,status=no");
    win.focus();
    return false;
}

function HideInfo(element) {
    element = $(element);
    $('#rxnInfo').hide();
    element.css('z-index', '50000');
    element.toggleClass('white');
}

function ShowInfo(element, direction) {
    var fixedwidth = 400;
    element = $(element);

    var x = element.offset().left;
    var y = element.offset().top;

    if ($('#rxnInfo').length == 0)
        $('body').append('<div class="shadow" id="rxnInfo" style="position:absolute;top:0px;left:0px;z-index:50001;background:#262828;"></div>');

    if (direction == 'left') {
        x -= fixedwidth;
        x -= 40;
    } else {
        x += element.find('span').first().outerWidth();
    }

    $('#rxnInfo').css('left', x + 'px');
    $('#rxnInfo').css('top', y + 'px');

    $('#rxnInfo').css('width', fixedwidth + 'px');
    $('#rxnInfo').css('padding', '30px 20px 10px 20px');

    var info = '';
    if (element.find('.info') != null) info = element.find('.info').html();

    element.css('z-index', '50002');
    element.toggleClass('white');

    $('#rxnInfo').html(info);

    $('#rxnInfo').show();
}

InputTitleCheckOut = function () {
    try {
        var preset = $(this).attr('title');
        var textvalue = $(this).val();
        //textvalue = textvalue.trim();

        if (textvalue == '') $(this).val(preset);
    } catch (e) {
        this.value = this.title;
    }
}

InputTitleCheckIn = function () {
    try {
        var preset = $(this).attr('title');
        var textvalue = $(this).val();
        //textvalue = textvalue.trim();

        if (textvalue == preset) $(this).val('');
    } catch (e) { }
}

function ValidateEnable(url) {
    $('input[type="text"]').blur(function () { Validate(this.name, url); });
    $('input[type="checkbox"]').click(function () { Validate(this.name, url); });
    $('input[type="radio"]').click(function () { Validate(this.name, url); });
    $('textarea').blur(function () { Validate(this.name, url); });
    $('select').change(function () { Validate(this.name, url); });
}

function Validate(name, url) {
    $('input[title!=""],textarea[title!=""]').each(InputTitleCheckIn);

    var values = $('form').serialize();
    $.ajax({
        url: url,
        type: 'POST',
        data: values,
        dataType: 'json',
        success: function (data, textStatus) { ValidateSuccess(data, name); }
    });

    $('input[title!=""],textarea[title!=""]').each(InputTitleCheckOut);
}

function ValidateSuccess(data, name) {
    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        var error = $('#' + item.Key.replace(/\./g, "_") + '_Error');
        var field = $('[name="' + item.Key + '"]');
        if (field.hasClass('ignore-error-handler')) { continue; }
        if (item.Value == null) { error.children('span').remove(); field.removeClass('input-validation-error'); continue; }
        if (name != item.Key) { continue; }
        field.addClass('input-validation-error');
        error.children('span').remove();
        error.append('<span class="field-validation-error">' + item.Value + '</span>');
    }
}

jQuery(document).ready(function () {
    $(function () {
        var zIndexNumber = 1000;
        $('#content div,#content ul,#content a,#content img').each(function () {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
        $('#background').css('zIndex', '1');
        $('#content a').css('zIndex', '1500');
        $('#content img').css('zIndex', '1500');
        $('#content h3').css('zIndex', '1600');
        $('#decoration_prepaid').css('zIndex', '1600');
        $('#decoration_postpaid').css('zIndex', '1600');
        $('#content .inner').css('zIndex', '1750');

        $('#navigation').css('zIndex', '10000');
    });

    /* navigation */

    $.each(
        $('#navigation .flyout'),
        function () {
            var self = $(this);
            var label = $(this).parent().find('a').first().text();
            self.attr('title', label);
        }
    );

    $.each(
        $('#navigation a'),
        function () {
            var self = $(this);
            var label = $(this).text();
            self.attr('title', label);
        }
    );

    // define mouseover/-out handlers for navigation
    $('#navigation a[title!=""]').parent().mouseover(NaviOn);
    $('#navigation a[title!=""]').parent().mouseout(NaviOff);

    // same for flyouts
    $('#navigation .flyout[title!=""]').mouseover(NaviOn);
    $('#navigation .flyout[title!=""]').mouseout(NaviOff);

    $('#navigation a[title^="' + NAVI_CURRENT_CONTROLLER + '"]').parent().addClass('active');

    // dead link for login
    $('#navigation li.last a').first().attr('href', '#');

    //add shadows
    //$('#navigation li ul').addClass('shadow');

    //as invisible by default, here we show the translucent background after correcting the height for lame browsers
    $('#background').show();
});

NaviOn = function () {
    // make object jqueryable
    var self = ($(this).context.tagName == 'ul') ? $(this) : $(this).children('a').first();
    // get controller name from tite attribute
    var title = self.attr('title');

    // clear current timeout (if existing)
    window.clearTimeout(NAVI_TIMEOUT);
    // close all flyouts
    $('#navigation .flyout').hide();
    // just show up by controller name in title attribute
    $('#navigation .flyout[title="' + title + '"]').show();

    $('#navigation a[title!="' + NAVI_CURRENT_CONTROLLER + '"]').parent().removeClass('active');
    $('#navigation a[title="' + title + '"]').parent().addClass('active');
}

NaviOff = function () {
    // make object jqueryable
    var self = ($(this).context.tagName == 'ul') ? $(this) : $(this).children('a').first();
    // get controller name from tite attribute
    var title = self.attr('title');
    // create timeout to prevent closing while moving over to associated flyout
    NAVI_TIMEOUT = window.setTimeout('$(\'#navigation .flyout[title="' + title + '"]\').hide();' + ((NAVI_CURRENT_CONTROLLER != title) ? '$(\'a[title="' + title + '"]\').parent().removeClass(\'active\')' : '') + ';', 300);
}

