﻿// JScript File

// consts - server side controls client id

var FocusControls = {};
FocusControls.Login  = "SysWinView_login_tbEmail";
FocusControls.SignUp  = "SysWinView_SignUp1_tbEmail";
FocusControls.Rss  = "SysWinView_AddRss_tbTitle";
FocusControls.Widget  = "SysWinView_AddWidget_tbTitle";
FocusControls.Tab  = "SysWinView_AddTab_tbTabName";
FocusControls.Send  = "SysWinView_SendToFriend_tbEmail";
FocusControls.Bug  = "SysWinView_ReportBug_tbTitle";
FocusControls.Rename  = "SysWinView_RenameFrame_tbFrameName";


function OpenLoginWin() 
{
   openSystemDiv("SysWinLogin","Login", 300, 380,0,0,FocusControls.Login);
}
function OpenNewUserWin() 
{  
   // call the page
   openSystemDiv("SysWinSignUp","Sign Up", 300, 380,0,0,FocusControls.SignUp);
}
function OpenAddRssWin() 
{
   openSystemDiv( "SysWinAddRSS","Add Rss", 305, 360,0,0,FocusControls.Rss);
}
function OpenAddWidgetWin()
{
   openSystemDiv("SysWinAddWidget", "Add Widget", 330, 380, 0, 0,FocusControls.Widget);
}
function AddNewTab() 
{
   openSystemDiv("SysWinAddTab","Add a new tab",160,250,0,0,FocusControls.Tab);
}
function OpenHelpWin()
{
    openSystemDiv("SysWinHelp", "Help", 400, 440, 0, 0, "SysWin_Help_Wrapper");
}
function OpenFlashDemoWin()
{
    openSystemDiv("SysWinFlashDemo", "Live Alefo Demo", 469, 691, 0, 0, "SysWin_FlashDemo_Wrapper");
}
 
//typeID :1 - user, 2-tab 3-frame 0- none
function OpenSendToFriend(elID, typeID)
{
   openSystemDiv("SysWinSendToFriend", "Send to Friend", 360, 400, elID, typeID, FocusControls.Send);
}

function bugReportWin(frameID) 
{
   openSystemDiv("SysWinReportBug","Report a bug",390,380,frameID,3,FocusControls.Bug);
}
function OpenRenameFrame(frameID)
{
   openSystemDiv("SysWinRenameFrame","Rename frame",160,250,frameID,3,FocusControls.Rename);
}
function OpenSetBackground()
{
   openSystemDiv("SysWinSetBackground","Set Background",160,250,0,3,FocusControls.Rename);
}
function loginClose()
{
    if (!$("#disableBG").hasClass("hidden")) {
        $("#disableBG").fadeOut(DialogFadeTime, function() {
            $("#disableBG").addClass("hidden");
            $("#disableBG").css("display","");
        });
    }
    $("#SysWinContent").children(":visible").toggleClass("SysWinHidden");

    // remove text validators
    $("#valEmailExists").css("visibility","");
    $("#valLoginError").text("");
    $("#valLoginError").css("visibility","");
    
    $("#tbUrl").focus();
}

// typeID :1-user, 2-tab, 3-frame, 0-none
function openSystemDiv(contentDivId, Name, height, width, ElID, typeID, focusElementID)
{
    //loginClose();
    // initialize the dialog
    ClearSystemWindowValidators(contentDivId);
    ClearSystemWindowInputs(contentDivId);

    var h = GetWindowHeight();
    var w = GetWindowWidth();
    $("#disableBG").height(h-3);
    $("#disableBG").width(w-3);
    if ($("#disableBG").hasClass("hidden"))
    {
        $("#disableBG").fadeIn(DialogFadeTime, function() {
            $("#disableBG").css("display","");
            $("#disableBG").removeClass("hidden");
    
            var top = h/2 - height/2 < 10 ? 10 : h/2 - height/2;
            var left = w/2 - width/2 < 10 ? 10 : w/2 - width/2;
            
            var win = $("#SysWinDiv");
            var lbl = $("#SysWinTitle");
            lbl.text(Name);
            win.css("position","absolute");
            win[0].style.top = top + 'px';
            win[0].style.left = left + 'px';
            win.css("z-index", 2147483647);
            win.height(height+14);
            win.width(width+14);
            
            var roundEdgeTable = win.find(".WindowRoundEdge");
            if (roundEdgeTable.length) {
                //roundEdgeTable.height(sheet.height());
                //roundEdgeTable.width();
                var roundEdgeTop = roundEdgeTable.find(".t");
                var roundEdgeBottom = roundEdgeTable.find(".b");
                var roundEdgeLeft = roundEdgeTable.find(".l");
                var roundEdgeRight = roundEdgeTable.find(".r");
                var roundEdgeCenter = roundEdgeTable.find(".frame");
                roundEdgeTop.width(width);
                roundEdgeBottom.width(width);
                roundEdgeLeft.height(height);
                roundEdgeRight.height(height);
                //roundEdgeCenter.height(rowHeight);
            }
            
            
            var contentDiv = $("#"+contentDivId);
            contentDiv.removeClass("SysWinHidden");
            
            try
            {
                // not good, should be placed in each of the calls
                // to open a SysWin, and to be specific to the desired input.
                //$("#" + contentDivId + " input:first")[0].focus();
                
                // instead try to set the focus to a designated element
                $("#" + contentDivId).find("#" + focusElementID).focus();
            }
            catch(e) { }
            
            // bind the esc click to close System Wins
            $("*", $("#disableBG")).keypress(function(e) {
                var theEvent = e; // window.event || arguments.callee.caller.arguments[0];
                if(theEvent.which || theEvent.keyCode)
                {
                    if ((theEvent.which == 27) || (theEvent.keyCode == 27))
                    {
                        if (!$("#disableBG").hasClass("hidden")) {
                            loginClose();
                        }
                    }
                }
            });
            
            if(typeID != 0)
            {
                var hdnID = contentDiv.children("input[elID]");
                var hdnType = contentDiv.children("input[typeID]");
                if(hdnID[0])
                    hdnID[0].value = ElID;
                if(hdnType[0])
                    hdnType[0].value = typeID;
            }
        });
    }
    return false;
}

function ClearSystemWindowValidators(contentDivId)
{
    $("#" + contentDivId).find(".Validator").css("visibility","");
}

function ClearSystemWindowInputs(contentDivId)
{
    $("#" + contentDivId).find("input:text").val("");
    $("#" + contentDivId).find("input:password").val("");
    $("#" + contentDivId).find("textarea").val("");
    $("#" + contentDivId).find("select").attr("selectedIndex","0");
}

/*********************************/
/* Misc Error and Click Handlers */
/*********************************/
 
function switchToLogin()
{
    loginClose();
    OpenLoginWin();
}
 
function ShowEmailExistsError()
{
    $("#valEmailExists").text("The Email Address Exists!");
    $("#valEmailExists").css("visibility","visible");
}
 
function LoginErrorMsg(txt)
{
    $("#valLoginError").text(txt);
    $("#valLoginError").css("visibility","visible");
}

function AddTabErrorMsg(txt)
{
    $("#valAddTabError").text(txt);
    $("#valAddTabError").css("visibility","visible");
}


/******************/
/* Message Dialog */
/******************/

var _messageDialogDefaultHeight = 150;
var _messageDialogDefaultWidth = 300;

function OpenMessageDialog(frameTitle, innerTitle, htmlContent, height, width, focusElementID)
{
    // set default value for empty params
    var _contentDivId = "SysWinMessageDialog";
    
    var _frameTitle     = frameTitle.length     > 0 ? frameTitle     : "Message Dialog";
    var _innerTitle     = innerTitle.length     > 0 ? innerTitle     : "";
    var _htmlContent    = htmlContent.length    > 0 ? htmlContent    : "";
    var _focusElementID = focusElementID.length > 0 ? focusElementID : "SysWinMessageDialog";
    
    var _height = height == 0 ? _messageDialogDefaultHeight : height;
    var _width  = width  == 0 ? _messageDialogDefaultWidth : width;
    
    openSystemDialog(_contentDivId, _frameTitle, _innerTitle, _htmlContent, _focusElementID, _height, _width);
}

function openSystemDialog(contentDivId, frameTitle, innerTitle, htmlContent, focusElementID, height, width)
{
    //loginClose();
    
    var h = GetWindowHeight();
    var w = GetWindowWidth();
    $("#disableBG").height(h-3);
    $("#disableBG").width(w-3);
    if ($("#disableBG").hasClass("hidden"))
    {
        $("#disableBG").fadeIn(DialogFadeTime, function() {
            $("#disableBG").css("display","");
            $("#disableBG").removeClass("hidden");
    
            var top = h/2 - height/2 < 10 ? 10 : h/2 - height/2;
            var left = w/2 - width/2 < 10 ? 10 : w/2 - width/2;
            
            var win = $("#SysWinDiv");
            var lbl = $("#SysWinTitle");
            lbl.text(frameTitle);
            win.css("position","absolute");
            win[0].style.top = top + 'px';
            win[0].style.left = left + 'px';
            win.css("z-index", 2147483647);
            win.height(height);
            win.width(width);
            
            var contentDiv = $("#"+contentDivId);
            contentDiv.removeClass("SysWinHidden");
            
            try
            {
                $("#" + contentDivId).find("#" + focusElementID).focus();
            }
            catch(e) { }
            
            // bind the esc click to close System Wins
            $("*", $("#disableBG")).keypress(function(e) {
                var theEvent = e;
                if(theEvent.which || theEvent.keyCode)
                {
                    if ((theEvent.which == 27) || (theEvent.keyCode == 27))
                    {
                        if (!$("#disableBG").hasClass("hidden")) {
                            closeMessageDialog();
                        }
                    }
                }
            });


            var wrapper = $("#" + contentDivId).find(".SysWin_MessageDialog_Wrapper");
            wrapper.height(height - 35);
            wrapper.width(width - 22);
            wrapper.css("padding","12px 0 0 22px");
            
            $("#" + contentDivId).find("#innerTitle").text(innerTitle);
            $("#" + contentDivId).find("#msgDialogContent").html(htmlContent);
            
            setTimeout(delegate(this, closeMessageDialog),DialogShowTime); // DialogShowTime is a global var, defined in the AlefoDev.aspx.cs
        });
    }
}

function closeMessageDialog()
{
    if (!$("#disableBG").hasClass("hidden")) {
        $("#disableBG").fadeOut(DialogFadeTime, function(){
            $("#disableBG").addClass("hidden");
            $("#disableBG").css("display","");
        });
    }
    
    $("#SysWinContent").children(":visible").toggleClass("SysWinHidden");

    $("#tbUrl").focus();
}