﻿var mActiveLink = "";
var mActiveDir = "";
var mReqId = 0;
var strDefAtmo = "<span id=\"txtAtmo\" class=\"main\" onmouseover=\"hoverLink(this);\" onmouseout=\"unHoverLink(this);\" onclick=\"goInfo(this);\">Atmosfär</span><br /><br />";
window.addEvent('domready', function () {
		goInfo($('txtAtmo'));
	});
function sendAjaxRequest(tmpUrl, completeFx, failureFx, sendinfo)
{
    new Request.JSON({url: tmpUrl, onComplete:completeFx, onFailure:failureFx}).send( JSON.encode({strReq: sendinfo}) );
}
function ajaxFailureMain(transport)
{
    //alert("Error: " + transport.responseText);
}
function goExInfo(whatToSee) {
    win = window.open("dircut.aspx?dre=" + whatToSee.id, "_blank", "toolbar=0, menubar=0, location=0, directories=0, status=0, scrollbars=0, resizable=no, width=565, height=650"); 
    win.focus();
}
function goExInfoPic(whatToSee) {
    win = window.open("dircutpic.aspx?dre=" + whatToSee.id, "_blank", "toolbar=0, menubar=0, location=0, directories=0, status=0, scrollbars=1, resizable=no, width=1000, height=800");
    win.focus();
}
    function goLoadInfo()
    {
        var win = window.open("newupdo.aspx" , "_blank", "toolbar=0, menubar=0, location=0, directories=0, status=0, scrollbars=0, resizable=no, width=620, height=200"); 
        win.focus();
    }

function goInfo(whatToSee)
{
    mActiveLink = whatToSee.id;
    hoverLink(whatToSee);

    if(whatToSee.id == "btnDir")
    {
        mActiveDir = "";
        clearVideos();
    }
    
    sendAjaxRequest("Informator.asmx?mn=giveItToMe" , OnRequestComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString());
}
function goInfoSpec(whatToSee) {
    var strWhat = whatToSee.id.split("_");
    mActiveLink = "";
    hoverLink(whatToSee);

    if (strWhat[0] == "btnDirSpec") {
        mActiveDir = "theVideos11";
        clearVideos();
    }

    sendAjaxRequest("Informator.asmx?mn=giveItToMe", OnRequestComplete, ajaxFailureMain, strWhat[0] + strWhat[1] + "#h4tag#" + (++mReqId).toString());
}
function hoverLink(oToHover)
{
    $("btnDir").className = "main";
    $("btnProd").className = "main";
    $("btnClient").className = "main";

    if(mActiveLink != "")
        $(mActiveLink).className = "hover";
    oToHover.className = "hover";
}
function unHoverLink(oToHover)
{
    if(oToHover.id != mActiveLink)
        oToHover.className = "main";
}
function goInfoDir(whatToSee, dirvalue) {
    mActiveDir = whatToSee.id;
    hoverLinkDir(whatToSee);
    clearVideos();
    sendAjaxRequest("Informator.asmx?mn=giveItToMe", OnRequestComplete, ajaxFailureMain, "btnVideos" + dirvalue + "#h4tag#" + (++mReqId).toString());
}
function hoverLinkDir(oToHover)
{
    for(var i=0; i<$("txtInfoTbl").getChildren().length; i++)
    {
        $("txtInfoTbl").getChildren()[i].className = "main";
    }

    if(mActiveDir != "")
        $(mActiveDir).className = "hover";
    oToHover.className = "hover";
}
function unHoverLinkDir(oToHover)
{
    if(oToHover.id != mActiveDir)
        oToHover.className = "main";
}

function clearVideos()
{
    for( var i=1; i<10; i++)
    {
        $("picTbl" + i.toString()).set('html', "");
        $("txtTbl" + i.toString()).set('html', "");
    }
}

function OnRequestComplete(result)
{
    var strRez = result.split("#h4tag#");
    if(strRez[0] != mReqId)
        return;
        
    if(strRez[1] == "errRet")
    {
        alert("Atmosfar.se has encountered an access error. Please try again.");
        return;
    }
        
    if(strRez[1] == "btnAbout")
    {
        $("tableClient").setStyle('display', 'none');
        $("imgLogo").src = "imgres/logo0.gif";
        $("tableInfo").setStyle('display', 'none'); 
        var RsltElem = $("txtInfo");
        RsltElem.setStyle('display', 'block');
        RsltElem.set('html', strDefAtmo + strRez[2]);
    }
    else if(strRez[1] == "txtAtmo")
    {
        $("tableClient").setStyle('display', 'none');
        $("imgLogo").src = "imgres/logo1.gif";
        $("tableInfo").setStyle('display', 'none');
        var RsltElem = $("txtInfo");
        RsltElem.setStyle('display', 'block');
        var tmpDefAtmo = "<span id=\"txtAtmo\" class=\"main\" style='float:left; width:200px;' onmouseover=\"hoverLink(this);\" onmouseout=\"unHoverLink(this);\" onclick=\"goInfo(this);\">Atmosfär</span>";
        RsltElem.set('html', tmpDefAtmo + "<div style='float:right; width:570px;'>" + strRez[2] + "</div>");
    }
    else if(strRez[1] == "btnProd")
    {
        $("tableClient").setStyle('display', 'none');
        $("imgLogo").src = "imgres/logo3.gif";
        $("tableInfo").setStyle('display', 'none');
        var RsltElem = $("txtInfo");
        RsltElem.setStyle('display', 'block');
        RsltElem.set('html', strDefAtmo + strRez[2]);
    }
    else if(strRez[1] == "btnDir")
    {
        $("tableClient").setStyle('display', 'none');
        $("imgLogo").src = "imgres/logo2.gif";
        $("txtInfo").setStyle('display', 'none');
        $("tableInfo").setStyle('display', 'block');
        $("txtInfoTbl").set('html', strDefAtmo + strRez[2]);
        //goInfoDir($('theVideos11'), 'Jörgen Lööf');
    }
    else if (strRez[1] == "btnDirSpec") {
        $("tableClient").setStyle('display', 'none');
        $("imgLogo").src = "imgres/logo2.gif";
        $("txtInfo").setStyle('display', 'none');
        $("tableInfo").setStyle('display', 'block');
        $("txtInfoTbl").set('html', strDefAtmo + strRez[2]);
        goInfoDir($('theVideos11'), $('theVideos11').get('text'));
    }
    else if (strRez[1] == "btnVideosBio") {
        $("txtInfoClient").set('html', $("txtInfoTbl").get('html'));
        $("tdBody").set('html', strRez[2]);
        $("tableInfo").setStyle('display', 'none');
        $("tableClient").setStyle('display', 'block');
    }
    else if(strRez[1] == "btnVideos")
    {
        $("tableClient").setStyle('display', 'none');
        $("tableInfo").setStyle('display', 'block');
        var objectId=1;
        for( var i=2; i<strRez.length-1; i+=2)
        {
            $("picTbl" + objectId.toString()).set('html', strRez[i]);
            $("txtTbl" + objectId.toString()).set('html', strRez[i+1]);
            objectId++;
        }              
    }
}


function goSaveVidRes(whatToSee) {
    sendAjaxRequest("Informator.asmx?mn=getClientData", OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString() + "#h4tag#" + $("txtVideoDirector").options[$("txtVideoDirector").selectedIndex].value + "#h4tag#" + document.getElementById("txtVideoTitle").value
     + "#h4tag#" + document.getElementById("txtVideoClient").value + "#h4tag#" + document.getElementById("txtVideoAgency").value + "#h4tag#" + "oldvideolength"
     + "#h4tag#" + document.getElementById("txtVideoDate").value + "#h4tag#" + document.getElementById("txtVideoExtra").value + "#h4tag#" + document.getElementById("txtVideoLinkPic").value
     + "#h4tag#" + document.getElementById("txtVideoMovie1").value + "#h4tag#" + document.getElementById("txtVideoTitle1").value + "#h4tag#" + document.getElementById("txtVideoLength1").value
     + "#h4tag#" + document.getElementById("txtVideoMovie2").value + "#h4tag#" + document.getElementById("txtVideoTitle2").value + "#h4tag#" + document.getElementById("txtVideoLength2").value
     + "#h4tag#" + document.getElementById("txtVideoMovie3").value + "#h4tag#" + document.getElementById("txtVideoTitle3").value + "#h4tag#" + document.getElementById("txtVideoLength3").value     
     );
}
function goAddExtras(whatToSee)
{
    sendAjaxRequest("Informator.asmx?mn=getClientData" , OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString() + "#h4tag#" + getSelectedItems($("listResExtra")).join("#h4tag#"));
}
function getSelectedItems(oSel) { 
    var selectedItems = new Array(); 
    for (var i = 0; i < oSel.length; i++) { 
        if (oSel.options[i].selected)
            selectedItems[selectedItems.length] = oSel.options[i].value; } 
    return selectedItems; 
} 
//---------------------------
function goClientInfo(whatToSee)
{
    if(whatToSee.id == "btnClient")
    {
        mActiveLink = whatToSee.id;
        hoverLink(whatToSee);
        sendAjaxRequest("Informator.asmx?mn=getClientData" , OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString());
    }
    else if (whatToSee.id == "btnSaveAbout" || whatToSee.id == "btnSaveProd")
    {
        sendAjaxRequest("Informator.asmx?mn=getClientData" , OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString() + "#h4tag#" + $("txtEditAbout").value);
    }
    else if (whatToSee.id == "btnSaveBio")
    {
        sendAjaxRequest("Informator.asmx?mn=getClientData", OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString() + "#h4tag#" + $("txtEditBio").value);
    }
    else if(whatToSee.id == "btnLogin")
    {
        if($("txtId").value == "")
        {
            alert("In order to process you have to enter a valid ID.");
            return;
        }
        sendAjaxRequest("Informator.asmx?mn=getClientData" , OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString() + "#h4tag#" + $("txtId").value);
    }
    else
    {
        sendAjaxRequest("Informator.asmx?mn=getClientData" , OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString());
    }
}
function OnClientInfoComplete(result)
{
    var strRez = result.split("#h4tag#");
    if(strRez[0] != mReqId)
        return;
        
    if(strRez[1] == "errRet")
    {
        alert("Atmosfar.se has encountered an access error. Please try again later.");
        return;
    }
    else if(strRez[1] == "errRetMsg")
    {
        alert(strRez[2]);
        return;
    }
        
    if(strRez[1] == "onLineState")
    {
        $("imgLogo").src = "imgres/logo4.gif";
        $("tableInfo").style.display = "none";
        $("txtInfo").style.display = "none";
        $("tableClient").style.display = "block";
        $("txtInfoClient").innerHTML = strDefAtmo + strRez[3];
        if(strRez[2] == "-1")
            alert(strRez[4]);
            
        if(strRez[2] == "1")
        {
            $("btnClient").innerHTML = "Log in";
            $("tdBody").innerHTML = strRez[4];
        }
        else if(strRez[2] == "-2")
        {
            $("btnClient").innerHTML = "Log in";
            $("tdBody").innerHTML = strRez[4];
            alert(strRez[5]);
        }
        else
        {
            $("btnClient").innerHTML = "Log in";
            $("tdBody").innerHTML = "";
        }
         
        if($("txtId") != null)
            $("txtId").focus();
        else if($("textUserName") != null)
            $("textUserName").focus();
    }
    else if(strRez[1] == "onSaveState")
    {
        $("txtInfo").innerHTML = strDefAtmo + strRez[2];
    }
    else if (strRez[1] == "onSaveStateBio") {
        $("tdBody").set('html', strRez[2]);
    }
}
function goClientSave(whatToSee)
{
    sendAjaxRequest("Informator.asmx?mn=getClientData" , OnClientInfoComplete, ajaxFailureMain, whatToSee.id + "#h4tag#" + (++mReqId).toString() + "#h4tag#" + $("textUserName").value + "#h4tag#" + $("textPwd").value + "#h4tag#" + $("boxActive").checked + "#h4tag#" + $("boxUpload").checked);
    $("tdBody").innerHTML = "Saving...";
}
//---------------------
function goSaveAtmo(whatToSee)
{
    sendAjaxRequest("Informator.asmx?mn=goSaveAtmo" , OnSaveAtmoComplete, ajaxFailureMain, (++mReqId).toString() + "#h4tag#" + $("theTextFrontPage").value + "#h4tag#" + $("theImgSelFrontpage").value + "#h4tag#" + $("theLinkSelFrontpage").value);
    whatToSee.innerHTML = "Saving...";
}
function OnSaveAtmoComplete(result)
{
    if(result != mReqId)
        return;
    
    $('theImgFrontPage').src = "dirsources/atmo_front_page_file.jpg?rnd=" + Math.random();
    $('btnSaveAtmo').set('html', 'Save');
}
//---------small funcs
function checkKey(who, pressed_key)
{ 
    if(who.id == "txtId" && pressed_key==13) 
        goClientInfo( $("btnLogin")); 
}
