/* antispam */
function SendEMail( domain, mailbox )
{
var sLink = 'mailto:' ;
sLink += mailbox ;
sLink += '@' ;
sLink += domain ;

window.location.href = sLink ;

return false ;
}
/* end antispam */


$(document).ready(function() {
/* tooltips init */
    $('.tooltip').tooltip();

    $('.advtooltip').tooltip({ 
        track: true, 
        delay: 0, 
        showURL: false, 
        showBody: " - ", 
        fade: 250 
    });
    

    
/* end tooltips init */
    
/* gallery init */
    $('a[href$=.jpg], a[href$=.gif], a[href$=.png]').lightbox();
/* end gallery init */

/* user management */

    /*
    $("#usrmgt_remindpass").click(UsrmgtRemindPass);
    $("#usrmgt_signin").click(UsrmgtSignIn);
    $("#usrmgt_register").click(UsrmgtRegister);
    */
    
    $("a[href$=usrmgt_remindpass.htm]").live("click", UsrmgtRemindPass);
    $("a[href$=usrmgt_signin.htm]").live("click", UsrmgtSignIn);
    $("a[href$=usrmgt_register.htm]").live("click", UsrmgtRegister);
    $("a[href$=usrmgt_usermenu.htm]").live("click", UsrmgtUserMenu);
    
/* end user management */

/* shop scripts */
    $("a[href*=shop_addtocart_itemid_]").click(ShopAddToCart);
    $("a[href$=shop_cart.htm]").click(ShopViewCart);
    $("a[href*=shop_orders]").live("click", ShopOrders);
    $("a[href*=shop_orderdetails]").live("click", ShopOrderDetails);
/* end shop scripts */

});
function ShopOrderDetails(){
    var url = $(this).attr("href");
    $.ajax({
        url         :   url,
        async       :   false,
        dataType    :   "html",
        type        :   "GET",
        success     :   function(data){
                            $("body").append("<div id='shop-orderdetails'>" + data + "</div>");
                            $("#shop-orderdetails").dialog({
                                modal   :   true,
                                width   :   900,
                                minWidth:   900,
                                maxWidth:   900,
                                height  :   500,
                                minHeight:  500,
                                resizable:  true,
                                autoOpen:   true,
                                show    :   "blind",
                                hide    :   "blind",
                                open    :   function(){
                                                title = $("h1", this).html();
                                                $("h1", this).remove();
                                                $(this).dialog("option", "title", title);
                                            },
                                close   :   function(){
                                                $("#shop-orderdetails").dialog('destroy');
                                                $("#shop-orderdetails").remove();
                                            }
                            });
                        }
        });
    return false;
}
function ShopOrders(){
    var url = $(this).attr("href");
    $.ajax({
        url         :   url,
        async       :   false,
        dataType    :   "html",
        type        :   "GET",
        success     :   function(data){
                            if($("#shop-orderlist-container").length == 1){
                                $("#shop-orderlist-container").dialog('destroy');
                                $("#shop-orderlist-container").remove();
                            }
                            
                            $("body").append("<div id='shop-orderlist-container'>" + data + "</div>");
                            $("#shop-orderlist-container").dialog({
                                modal   :   true,
                                width   :   900,
                                height  :   400,
                                resizable:  false,
                                autoOpen:   true,
                                show    :   "blind",
                                hide    :   "blind",
                                open    :   function(){
                                                title = $("h1", this).html();
                                                $("h1", this).remove();
                                                $(this).dialog("option", "title", title);
                                                
                                                $("#shop-orders-filter").buttonset();
                                                $("#shop-orders-filter input").live('click', ShopSwitchOrdersMode);
                                                ShopSwitchOrders();
                                                
                                             },
                                close   :   function(){
                                                $("#shop-orderlist-container").dialog('destroy');
                                                $("#shop-orderlist-container").remove();
                                            }
                            });
                        }
    });
    return false
}
function ShopSwitchOrdersMode(){
    $("#shop-order-items").attr("mode", $(this).val());
    ShopSwitchOrders(0);
}
function ShopSwitchOrders(page){
    var mode = $("#shop-order-items").attr("mode");
    var uid = $("#shop-order-items").attr("uid");
    if(mode == "") mode = 0;
    $.get("/index.php?module=shop&action=orderitems&status=" + mode + "&uid=" + uid + "&page=" + page,
        function(data){
            $("#shop-order-items").html(data);
            $(".shop-orders-item").mouseenter(function(){ $(this).addClass("ui-state-hover"); }).mouseleave(function(){ $(this).removeClass("ui-state-hover"); });
        });
    return false;
}
function UpdateMiniCart(){
    $.getJSON("/index.php",
        { module : "shop", action : "minicart"},
        function(data){
            $("#shop-minicart .totalval").html(data.val);
            $("#shop-minicart .totalsum").html(data.sum);
        });
}
function ShopViewCart(){
    $.ajax({
        url         :   "/shop_cart.htm",
        async       :   false,
        dataType    :   "html",
        type        :   "GET",
        success     :   function(data){
                            $("body").append("<div id='shop-cart-container'>" + data + "</div>");
                            $("#shop-cart-container").dialog({
                                modal   :   true,
                                width   :   900,
                                minWidth:   900,
                                maxWidth:   900,
                                height  :   400,
                                minHeight:   400,
                                autoOpen:   true,
                                show    :   "blind",
                                hide    :   "blind",
                                open    :   function(){
                                                title = $("h1", this).html();
                                                $("h1", this).remove();
                                                $(this).dialog("option", "title", title);
                                                
                                                if($("input[type=button]", this).length != 0){
                                                   buttontxt = $("input[type=button]:eq(1)", this).val();
                                                   $("input[type=button]", this).remove();
                                                
                                                   $(this).dialog("option" , "buttons" , [{
                                                        text    :   buttontxt,
                                                        click   :   function(){
                                                            if($(".shop-cart-item").length != 0){
                                                              $("#shop-cart-form input[name=action]").val("checkout");
                                                              $.post("index.php",
                                                                    $("#shop-cart-form").serialize(),
                                                                    function(data){
                                                                        $("body").append("<div id='shop-cart-checkout'>" + data + "</div>");
                                                                        $("#shop-cart-checkout").dialog({
                                                                            modal   :   true,
                                                                            width   :   900,
                                                                            height  :   400,
                                                                            resizable:  false,
                                                                            autoOpen:   true,
                                                                            show    :   "blind",
                                                                            hide    :   "blind",
                                                                            open    :   function(){
                                                                                            title = $("h1", this).html();
                                                                                            $("h1", this).remove();
                                                                                            $(this).dialog("option", "title", title);
                                                                                            buttontxt = $("input[type=submit]:eq(0)", this).val();
                                                                                            $("input[type=submit]", this).remove();
                                                                                            $(this).dialog("option" , "buttons" , [{
                                                                                                text    :   buttontxt,
                                                                                                click   :   function(){
                                                                                                    if(ShopCheckoutValid()){
                                                                                                        $.post("index.php",
                                                                                                            $("#checkoutform").serialize(),
                                                                                                            function(data){
                                                                                                                $("#shop-cart-checkout").html(data);
                                                                                                                $("#shop-cart-checkout").dialog("option" , "buttons" , []);
                                                                                                            });
                                                                                                    }
                                                                                                }}]);
                                                                                        },
                                                                            close   :   function(){
                                                                                            UpdateMiniCart();
                                                                                            $("#shop-cart-checkout").dialog('destroy');
                                                                                            $("#shop-cart-checkout").remove();
                                                                                        }
                                                                        });
                                                                        $("#shop-cart-container").dialog('close');
                                                                    });
                                                            }
                                                        }}]);
                                                }
                                            },
                                close   :   function(){
                                                UpdateMiniCart();
                                                $("#shop-cart-container").dialog('destroy');
                                                $("#shop-cart-container").remove();
                                            }
                            });
                        }
    });
    return false;
}
function ShopAddToCart(){
    var url = $(this).attr("href");
    var linkSender = $(this);
    $.ajax({
        url         :   url,
        async       :   false,
        dataType    :   "json",
        type        :   "GET",
        success     :   function(data){
                            $(linkSender).effect("transfer", { to: "#shop-minicart", className: "ui-state-highlight" }, 500);
                            setTimeout(function(dta){
                                $("#shop-minicart .totalval").html(data.val);
                                $("#shop-minicart .totalsum").html(data.sum);
                            }, 500);
                        }
    });
    return false;
}


function UsrmgtUserMenu()
{
    $("body").append("<div id=\"usrmgt_usermenu_dialog\" style=\"display: none;\"></div>");
    $("#usrmgt_usermenu_dialog").load("usrmgt_usermenu.htm",
                                        function(){
                                            var ancLocation = $("a[href$=usrmgt_usermenu.htm]").position();
                                            var position = new Array(ancLocation.left, ancLocation.top);
                                            var dialogtitle = $("#usrmgt_usermenu_dialog h1:first").html();
                                            $("#usrmgt_usermenu_dialog h1:first").remove();
                                            $("#usrmgt_usermenu_dialog").dialog({
                                                    title: dialogtitle,
                                                    width: 200,
                                                    minWidth: 200,
                                                    height: 100,
                                                    minHeight: 100,
                                                    position: position,
                                                    modal: false,
                                                    close: function() { $(this).remove(); }
                                                });
                                        }
        );
    
    return false;
}

/* ****************************************************************** */
/* ************************ PASSWORD REMINDER *********************** */
/* ****************************************************************** */
function UsrmgtRemindPass()
{
    $("body").append("<div id=\"usrmgt_remindpass_dialog\" style=\"display: none;\"></div>");
    $("#usrmgt_remindpass_dialog").load("usrmgt_remindpass.htm",
                                        function(){
                                            $("#usrmgt_remindpass_dialog form :submit").hide();
                                            $("#usrmgt_remindpass_dialog form").submit(UsrmgtDoRemind);
                                            var dialogtitle = $("#usrmgt_remindpass_dialog h1:first").html();
                                            $("#usrmgt_remindpass_dialog h1:first").remove();
                                            $("#usrmgt_remindpass_dialog").dialog({
                                                    title: dialogtitle,
                                                    width: 500,
                                                    modal: true,
                                                    zIndex: 1010,
                                                    buttons: {
                                                            "OK": UsrmgtDoRemind,
                                                            "Cancel": function() { $(this).dialog("close"); }
                                                            },
                                                    close: function() { $(this).remove(); }
                                                });
                                        }
        );
    
    return false;
}

function UsrmgtDoRemind()
{
    $("#usrmgt_remindpass_dialog form").append("<input type='hidden' name='ajaxsubmit' value='true' \>");
    var formdata = $("#usrmgt_remindpass_dialog form").serialize();

    $.getJSON("/?" + formdata,
             function(data)
                 {
                     var msg = data.message;
                     var result = data.passed;
                     
                     $("#usrmgt_remindpass_dialog").append("<div id=\"usrmgt_remindpass_msg\" style=\"display: none;\"></div>");
                     $("#usrmgt_remindpass_msg").html(msg);
                     $("#usrmgt_remindpass_msg").dialog({
                                                width: 500,
                                                modal: true,
                                                zIndex: 1011,
                                                buttons: { "OK": function() { $(this).dialog("close"); if(result) { $("#usrmgt_remindpass_dialog").dialog("close"); } } },
                                                close: function() { $(this).remove(); }
                                            });
                 }
             );

    return false;
}
/* ****************************************************************** */
/* ********************** END PASSWORD REMINDER ********************* */
/* ****************************************************************** */


/* ****************************************************************** */
/* ************************** USER SIGN IN ************************** */
/* ****************************************************************** */
function UsrmgtSignIn()
{
    $("body").append("<div id=\"usrmgt_signin_dialog\" style=\"display: none;\"></div>");
    $("#usrmgt_signin_dialog").load("usrmgt_signin.htm",
                                        function(){
                                            $("#usrmgt_signin_dialog form :submit").hide();
                                            $("#usrmgt_signin_dialog form").submit(UsrmgtDoSignIn);
                                            var dialogtitle = $("#usrmgt_signin_dialog h1:first").html();
                                            $("#usrmgt_signin_dialog h1:first").remove();
                                            $("#usrmgt_signin_dialog").dialog({
                                                    title: dialogtitle,
                                                    width: 300,
                                                    modal: true,
                                                    zIndex: 998,
                                                    resizable: false,
                                                    buttons: {
                                                            "OK": UsrmgtDoSignIn,
                                                            "Cancel": function() { $(this).dialog("close"); }
                                                            },
                                                    close: function() { $(this).remove(); }
                                                });
                                        }
        );
    
    return false;
}

function UsrmgtDoSignIn()
{
    $("#usrmgt_signin_dialog form").append("<input type='hidden' name='ajaxsubmit' value='true' \>");
    var formdata = $("#usrmgt_signin_dialog form").serialize();

    $.getJSON("/?" + formdata,
             function(data)
                 {
                     var msg = data.message;
                     var result = data.passed;
                     
                     if(result)
                     {
                         window.location.reload();
                     }
                     else
                     {
                         $("#usrmgt_signin_dialog").append("<div id=\"usrmgt_signin_msg\" style=\"display: none;\"></div>");
                         $("#usrmgt_signin_msg").html(msg);
                         $("#usrmgt_signin_msg").dialog({
                                                    width: 500,
                                                    modal: true,
                                                    zIndex: 999,
                                                    resizable: false,
                                                    buttons: { "OK": function() { $(this).dialog("close"); } },
                                                    close: function() { $(this).remove(); }
                                                });
                     }
                 }
             );

    return false;
}

/* ****************************************************************** */
/* ************************ END USER SIGN IN ************************ */
/* ****************************************************************** */

/* ****************************************************************** */
/* ************************ USER REGISTRATION *********************** */
/* ****************************************************************** */
function UsrmgtRegister()
{
    $("body").append("<div id=\"usrmgt_register_dialog\" style=\"display: none;\"></div>");
    $("#usrmgt_register_dialog").load("usrmgt_register.htm",
                                        function(){
                                            $("#usrmgt_register_dialog form :submit").hide();
                                            $("#usrmgt_register_dialog form").submit(UsrmgtDoRegister);
                                            var dialogtitle = $("#usrmgt_register_dialog h1:first").html();
                                            $("#usrmgt_register_dialog h1:first").remove();
                                            $("#usrmgt_register_dialog").dialog({
                                                    title: dialogtitle,
                                                    width: 500,
                                                    modal: true,
                                                    zIndex: 1000,
                                                    resizable: false,
                                                    buttons: {
                                                            "OK": UsrmgtDoRegister,
                                                            "Cancel": function() { $(this).dialog("close"); }
                                                            },
                                                    close: function() { $(this).remove(); }
                                                });
                                        }
        );
    
    return false;
}

function UsrmgtDoRegister()
{
    $("#usrmgt_register_dialog form").append("<input type='hidden' name='ajaxsubmit' value='true' \>");
    var formdata = $("#usrmgt_register_dialog form").serialize();

    $.getJSON("/?" + formdata,
             function(data)
                 {
                     var msg = data.message;
                     var result = data.passed;
                     $("#usrmgt_register_dialog").append("<div id=\"usrmgt_register_msg\" style=\"display: none;\"></div>");
                     $("#usrmgt_register_msg").html(msg);

                     $("#usrmgt_register_msg").dialog({
                                                    width: 500,
                                                    modal: true,
                                                    zIndex: 1001,
                                                    resizable: false,
                                                    buttons: { "OK": function() { $(this).dialog("close"); if(result) { $("#usrmgt_register_dialog").dialog("close"); } } },
                                                    close: function() { $(this).remove(); }
                                                });
                 }
             );

    return false;
}
/* ****************************************************************** */
/* ******************** END USER REGISTRATION *********************** */
/* ****************************************************************** */
