﻿// JScript File

// consts - server side controls client id

var FocusControls = {};
var redirectAfterNicknameSet = "";
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";
FocusControls.SetNickname  = "SysWinView_SetNickname_TextBox1";


function OpenLoginWin() 
{
   openSystemDiv("SysWinLogin","Login", 230, 380,0,0,FocusControls.Login);
}
function OpenNewUserWin() 
{  
   // call the page
   openSystemDiv("SysWinSignUp","Sign Up", 230, 380,0,0,FocusControls.SignUp);
}
function OpenAddRssWin() 
{
   openSystemDiv( "SysWinAddRSS","Add Rss", 305, 300,0,0,FocusControls.Rss);
    fillCatsAndItemsDDLs('ddlCategoriesRss',serverVars.Objects.categoriesRss,'ddlRss',serverVars.Objects.citemsRss);
}
function OpenAddWidgetWin()
{
   openSystemDiv("SysWinAddWidget", "Add Widget", 330, 360, 0, 0,FocusControls.Widget);
    fillCatsAndItemsDDLs('ddlCategoriesWidgets',serverVars.Objects.categoriesWidgets,'ddlWidgets',serverVars.Objects.citemsWidgets);
}
function OpenAddSiteshotWin()
{
   openSystemDiv("SysWinAddSiteshot", "Add Siteshot", 200, 360, 0, 0,FocusControls.Widget);
    fillCatsAndItemsDDLs('ddlCategoriesSiteshots',serverVars.Objects.categoriesSiteshots,'ddlSiteshots',serverVars.Objects.citemsSiteshots);
}
function AddNewTab() 
{
   openSystemDiv("SysWinAddTab","Add a new tab",80,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", 270, 400, elID, typeID, FocusControls.Send);
}

function bugReportWin(frameID) 
{
   openSystemDiv("SysWinReportBug","Report a bug",390,380,frameID,3,FocusControls.Bug);
   document.getElementById('SysWinView_ReportBug_tbEmail').value=userEmail; 
}
function OpenRenameFrame(frameID,Title)
{
   openSystemDiv("SysWinRenameFrame","Rename frame",95,250,frameID,3,FocusControls.Rename);
    //alert(Title);
    document.getElementById('SysWinView_RenameFrame_tbFrameName').value = Title;
    setTimeout("renameFrameSuspended();",1000);
}
function renameFrameSuspended()
{
    document.getElementById('SysWinView_RenameFrame_tbFrameName').focus();
    document.getElementById('SysWinView_RenameFrame_tbFrameName').select();

}
function OpenSetBackground()
{
   openSystemDiv("SysWinSetBackground","Set Background",395,360,0,0,'');
   jscolor.init();
      document.getElementById('SysWinView_SetBackground_TextBoxColor').value='Click here to select color...';
   document.getElementById('SysWinView_SetBackground_TextBoxColor').style.color='Gray';

   
}
function OpenShareDialog()
{
   openSystemDiv("SysWinShareDialog","Share Tab",195,500,0,0,'');
   document.getElementById('SysWinView_ShareDialog_TextBoxURL').value=friendlyURL;
    setTimeout("OpenShareDialog2();",500);
}
function OpenShareDialog2()
{
   document.getElementById('SysWinView_ShareDialog_TextBoxURL').focus();
   document.getElementById('SysWinView_ShareDialog_TextBoxURL').select();
}
function OpenGetURL()
{
   openSystemDiv("SysWinGetTabURL","Get Tab URL",100,500,0,0,"");
   document.getElementById('SysWinView_GetTabURL_TextBoxURL').value=friendlyURL;
    setTimeout("OpenGetURL2();",500);
}
function OpenGetURL2()
{
   document.getElementById('SysWinView_GetTabURL_TextBoxURL').focus();
   document.getElementById('SysWinView_GetTabURL_TextBoxURL').select();

}
function OpenSetNickname(redirect)
{
   redirectAfterNicknameSet = redirect;
   openSystemDiv("SysWinSetNickname","Set Nickname",120,290,0,3,FocusControls.SetNickname);
   document.getElementById('SysWinView_SetNickname_TextBox1').value = suggestedNickname;
}
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();
}
function loginCloseNoFade()
{
    if (!$("#disableBG").hasClass("hidden")) {
        $("#disableBG").fadeOut(10, 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();
}

function openFloatingDiv(divID)
{
    var divStyle = document.getElementById(divID).style;
    divStyle.top = tempY+15+"px";
    divStyle.left = tempX+15+"px";
    divStyle.display="inline";
}
function closeFloatingDiv(divID)
{
    var divStyle = document.getElementById(divID).style;
    divStyle.display="none";
}

// 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+80);
            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 openSystemDivMOUSEOVER(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").css("display","");
            $("#disableBG").removeClass("hidden");
    
            var top = tempY+10;
            var left = tempX+20;
            
            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()
{
    loginCloseNoFade();
    //OpenLoginWin();
    setTimeout("OpenLoginWin();",100);
}
 
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)
{
//alert('OpenMessageDialog h:'+height+" w:"+width);
    // 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, true);
}

function OpenMessageDialogNoAutoClose(frameTitle, innerTitle, htmlContent, height, width, focusElementID)
{
//alert('OpenMessageDialog h:'+height+" w:"+width);
    // 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, false);
}

function openSystemDialog(contentDivId, frameTitle, innerTitle, htmlContent, focusElementID, height, width, autoClose)
{
    //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+6);
            
            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);
            
            if (autoClose) 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();
}

////////////////////
// add siteshot
////////////////////
function onSiteshotSelected(){
    document.getElementById(tbAddSiteshotTitle).value = getListText('ddlSiteshots');
    document.getElementById(hfddlSiteshotsID).value = getListValue('ddlSiteshots');
}


function AddSiteshotClickGo()
{
    var theEvent = window.event || arguments.callee.caller.arguments[0];
    return GeneralClickGo(lbAddSiteshotsID,theEvent);
}

function clearDDL3()
{
    var ddlOptions = document.getElementById('ddlSiteshots').options;
    var i =0;
    for(i = 0; i<ddlOptions.length; i++){
            ddlOptions[i].selected = false;
    }
}

//////////////////////////