// UI initialization
$(document).ready(function(){
    // set device name
    var $device_name = $("#device_name");
    if($device_name) {
        $device_name.text($("#device_name_" + $device_name.find("input[name=device]").val()).val());
    }

    // Page Scroll Effect
    jQuery.easing.quart = function (x, t, b, c, d) {return -c * ((t=t/d-1)*t*t*t - 1) + b;};
    $(function () {$('.pageTop').click(function () {$('html,body').animate({ scrollTop: 0 }, 500, 'quart');return false;});});
    
    // Add class="on" on mouseover "<label>"
    $("label").hover(function () {$(this).addClass("on");},function () {$(this).removeClass("on");});
    
    // Add class="checked" to selected radio button
    $("input").change(function () {
        $("input").parent().removeClass("checked");
        $("input:checked").parent().addClass("checked");
    }).change();
    
    // Relocate when browser window resized
    $(window).resize(resize_dialog);
    
    // Close the dialog
    $(".window .btn.close").click(function (e) {e.preventDefault();$("#mask, .window").hide();});
    $("#mask").click(function () {$(this).hide();$(".window").hide();});
});

// Popup the dialog
function windowOpen(dialogId) {
    var id = dialogId;
    var maskHeight = $(document).height();
    var maskWidth = $(document).width();
    $("#mask").css({"width":maskWidth,"height":maskHeight});
    $("#mask").show();
    var winH = $(window).height();
    var winW = $(window).width();
    var org_x=0, org_y=0;
    org_x = $(document).scrollLeft();
    org_y = $(document).scrollTop();
    if(org_x+$(window).width()>$(document).width()) org_x = $(document).width()-$(window).width();
    if(org_y+$(window).height()>$(document).height()) org_y = $(document).height()-$(window).height();
    $(id).css("top",  org_y+winH/2-$(id).outerHeight()/2);
    $(id).css("left", org_x+winW/2-$(id).outerWidth()/2);
    $(id).fadeIn(250); 
}

// Resize the dialog
function resize_dialog() {
    if($("#mask").css('display')!='none') {
        var maskHeight = $(document).height();
        var maskWidth = $(document).width();
        $("#mask").css({"width":maskWidth,"height":maskHeight});
        var winH = $(window).height();
        var winW = $(window).width();
        var org_x=0, org_y=0;
        org_x = $(document).scrollLeft();
        org_y = $(document).scrollTop();
        if(org_x+$(window).width()>$(document).width()) org_x = $(document).width()-$(window).width();
        if(org_y+$(window).height()>$(document).height()) org_y = $(document).height()-$(window).height();
        $(".window").each(function() {
            if($(this).css('display')!='none') {
                $(this).css("top",  org_y+winH/2-$(this).height()/2);
                $(this).css("left", org_x+winW/2-$(this).width()/2);
            }
            return true;
        });
    }
}

// Popup error dialog
function popError(error_id) {
    $("#errorDialog li").hide();
    var msg = $("#errorDialog li." + error_id);
    if(!msg.size()) {
        if(error_id.indexOf('error_10102')==0) msg = $("#errorDialog li.error_10102");
        if(!msg.size()) msg = $("#errorDialog li.error_default");
    }
    msg.show();
    windowOpen("#errorDialog");
}

// change page language
function lang_change(lang) {
    var tokens = window.location.search.split('&');
    var params = {};
    if(tokens.length>0) {
        tokens[0] = tokens[0].substring(1);
        for(var i=0; i<tokens.length; i++) {
            var splits = tokens[i].split('=');
            if(splits.length==2) {
                // assume that keys and values are already url-encoded.
                params[splits[0]] = splits[1];
            }
        }
    }
    params['lang'] = encodeURIComponent(lang);
    var path = window.location.protocol + "//" + window.location.host + window.location.pathname;
    var args = "";
    for(key in params) {
        args += (args=="" ? "?" : "&") + key + "=" + params[key];
    }
    window.location.href = path + args + window.location.hash;
}


