﻿//Javascript init
var jsSize = 1;
switch(websiteSize)
{
    case 0:
        jsSize = 0.64;
    break;
    case 1:
        jsSize = 0.8;
    break;
    case 2:
        jsSize = 1;
    break;
}//switch

function g(elementId) {
    return document.getElementById(elementId);
}

function calcValue(intValue) {
    return parseInt(intValue * jsSize);
} //calcValue

function resValue(int1, int2, int3) {

    switch (websiteSize) {
        case 0:
            return int1;
            break;
        case 1:
            return int2;
            break;
        case 2:
            return int3;
            break;
    } //switch
} //resValue

//When dom is ready.
$(document).ready(function() {
    
    InitElements();
});

function InitElements(){

    //init popup scrollbar
    InitScrollBar("div-popup-root", "div-popup-container", "div-popup-document", "popupScrollbarContainer", false)

    //Check pagetype        
    switch(pagetype) {

        case 1:
            if ($(".imgActiveImagelistImageCompanyArticle").length > 0) {
                InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainer", false)
            }//if
            else {
                InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainerWide", false)
            }//else
            break;
        case 3:
            SetupImageList(m_imageListSources, "imgCompanyNewsImageList", "imgPreviousNewsImage", "imgNextNewsImage", "divImageListStatus");

            InitScrollBar("companyNewsRoot", "companyNewsContainer", "companyNewsDocument", "default-scrollbar-container", false);
            break;
        case 4:
            InitScrollBar("companyContactRoot", "companyContactContainer", "companyContactDocument", "default-scrollbar-container", false);
            break;
        case 5:
            switch (m_state) {

                case 0:
                    //Load search by map flash
                    setTimeout("LoadLargeFlash()", 200);

                    //Start image fader
                    startFrontpageFade();
                    break;
                case 1:
                    UpdateAreaDescription(m_area);

                    //Load search by map flash
                    setTimeout("LoadSmallFlashMap()", 200);
                    setTimeout("LoadFlashMapDepartment()", 200);
                    break;
                case 2:
                    break;
                case 3:
                    SetupImageList(m_imageListSources, "imgSelectedApartmentImageList", "imgPreviousApartmentImage", "imgNextApartmentImage", "divApartmentImageListStatus");
                    InitScrollBar("ApartmentPlaceHolder", "selectedApartmentContainer", "selectedApartmentDocument", "apartmentInfoScrollbarContainer", false);
                    break;
            } //switch

            //Load
            setTimeout("loadFrontpageBackgroundImageFlash()", 100);
            break;
        case 6:
            InitScrollBar("divTextFieldRoot", "divTextFieldContainerWide", "divTextFieldDocumentWide", "residentScrollbarContainerWide", false);
            InitScrollBar("divTextFieldRoot", "divTextFieldContainer", "divTextFieldDocument", "residentScrollbarContainer", false);

            SetupImageList(m_imageListSources, "imgDepartmentImageList", "imgPreviousDepartmentImage", "imgNextDepartmentImage", "divDepartmentImageListStatus");
            //initMenuScroll("divDepartmentMenuContainer", "tblDepartmentMenu", "imgDepartmentMenuUpArrow", "imgDepartmentMenuDownArrow", m_menuScrollPosition);

            menuOffset = getParameterByName('msp');
            if (menuOffset.length == 0)
                menuOffset = 0;

            if ($(".menu4Td1.mainrow .Boligkontoret-selectedhl").length > 0) {
                var selectedTop = parseInt($(".menu4Td1.mainrow .Boligkontoret-selectedhl").position().top);
                if (selectedTop < menuOffset * -1)
                    menuOffset = selectedTop * -1;
            } //if

            if ($("#tblDepartmentMenu").height() > $("#divDepartmentMenuContainer").height())
                $("#tblDepartmentMenu").css({ "top": menuOffset + "px" });

            break;
        case 7:
            if ($(".imgActiveImagelistImageCompanyArticle").length > 0) {
                InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainer", false)
            } //if
            else {
                InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainerWide", false)
            } //else
            break;
        case 8:
            InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainerMedium", false)

            $(".tr-employee-row").mouseover(function() {
                $(".tr-employee-row.selected").removeClass("selected");
                $(this).addClass("selected");
                $(".tbl-employee-hover.show").addClass("hide").removeClass("show");
                $(".tbl-employee-hover.tbl-employee-hover-" + $(this).attr("data") + "").addClass("show").removeClass("hide");
            });
            break;
        case 9:
            if ($(".imgActiveImagelistImageCompanyArticle").length > 0) {
                InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainer", false)
            } //if
            else {
                InitScrollBar("div-site-center", "companyArticleContainer", "companyArticleDocument", "articleScrollbarContainerWide", false)
            } //else
            break;
        case 99:
            InitScrollBar("divTextFieldRoot", "divTextFieldContainerWide", "divTextFieldDocumentWide", "residentScrollbarContainerWide", false);
            InitScrollBar("divTextFieldRoot", "divTextFieldContainer", "divTextFieldDocument", "residentScrollbarContainer", false);

            //Setup eventhandlers
            $(".title-resident-data-table-1").click(function() {

                $(".resident-data-table-2").addClass("hidden");
                $(".resident-data-table-1").removeClass("hidden");

                $(".title-resident-data-table-1").addClass("SelectedCompanyColor");
                $(".title-resident-data-table-2").removeClass("SelectedCompanyColor");
            });

            $(".title-resident-data-table-2").click(function() {

                $(".resident-data-table-1").addClass("hidden");
                $(".resident-data-table-2").removeClass("hidden");

                $(".title-resident-data-table-2").addClass("SelectedCompanyColor");
                $(".title-resident-data-table-1").removeClass("SelectedCompanyColor");
            });
            break;
        default:
            break;
    }//switch
} //InitElements

function startFrontpageFade() {

    if ($(".companyBackgroundImage").children().length > 1) {
        setTimeout("fadeToNext()", 5000);
    } //if
}

function fadeToNext() {

    var $current = $(".companyBackgroundImage .show");
    var $next = $current.next().length > 0 ? $current.next() : $(".companyBackgroundImage").children().first();

    $current.css({"z-index" : "0"});
    $next.css({ "z-index": "1", "opacity": "0" }).removeClass("hidden");

    //Show next
    $next.animate({ "opacity": "1" }, 1000, "", function() {

        //Animation done -> change current
        $(".companyBackgroundImage .show").removeClass("show").addClass("hidden").css({ "opacity": "0" });
        $(this).addClass("show");

        setTimeout("fadeToNext()", 5000);
    });
}//

function LoadLargeFlash() {

    if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
        alert("This page requires AC_RunActiveContent.js.");
    } else {
        var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
        if (hasRightVersion) {  // if we've detected an acceptable version
            // embed the flash movie
            document.getElementById("divLargeFlashContainer").innerHTML = AC_FL_RunContent(
                'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0',
                'width', '100%',
                'height', '100%',
                'src', 'searchmenu',
                'quality', 'high',
                'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
                'align', 'middle',
                'play', 'true',
                'loop', 'true',
                'scale', 'exactfit',
                'wmode', 'transparent',
                'devicefont', 'false',
                'id', 'searchmenu',
                'bgcolor', '#003300',
                'name', 'searchmenu',
                'menu', 'true',
                'allowScriptAccess', 'sameDomain',
                'allowFullScreen', 'false',
                'movie', 'searchmenu',
                'FlashVars', 'area=0',
                'salign', ''
                ); //end AC code
        } else {  // flash is too old or we can't detect the plugin
            var alternateContent = 'This content requires the Adobe Flash Player.<br><br><a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
            document.getElementById("divLargeFlashContainer").innerHTML = alternateContent;  // insert non-flash content
        }
    }
}

function loadFrontpageBackgroundImageFlash() {

    return;

    if (document.getElementById("companyBackgroundImage")) {
        document.getElementById("companyBackgroundImage").innerHTML = AC_FL_RunContent(
		                'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0',
		                'width', calcValue(782) + 'px',
		                'height', calcValue(304) + 'px',
		                'src', 'slide_2009',
		                'quality', 'high',
		                'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		                'align', 'top',
		                'play', 'true',
		                'loop', 'true',
		                'scale', 'noscale',
		                'wmode', 'transparent',
		                'devicefont', 'false',
		                'id', 'largeTopFlash',
		                'bgcolor', '',
		                'name', 'largeTopFlash',
		                'menu', 'false',
		                'allowScriptAccess', 'sameDomain',
		                'allowFullScreen', 'false',
		                'movie', 'slide_2009',
		                'FlashVars', "test=false&intern=false&lib=img_Company/&imgs=" + strFrontpageBackgroundImages + "&txts=&display_sec=3&fade_sec=1&stage_width=" + calcValue(782) + "&stage_height=" + calcValue(304) + "&textbox_height=0&slideFontSize=0&slideFontColor=FFFFFF&slideFontTopMargin=0",
		                'salign', 'lt'
		                ); //end AC code 
    } //if
} //





/********************************************/
/******** IMAGELIST FUNCTIONS START *********/
/********************************************/
var imageListSourcesArray = null;
var imageListArrayPointer = 0;
var imageListImageElement = null;
var imagelistStatusElement = null;
function SetupImageList(imageslisteSources, imageElementId, imagePreviousId, imageNextId, imagelistStatusId) {

    //Init
    imageListImageElement = g(imageElementId);
    imagelistStatusElement = g(imagelistStatusId);

    //Split sources list to array
    imageListSourcesArray = imageslisteSources.split('*');

    //Check if no images
    if (imageslisteSources.length == 0) {
        //Hide all elements
        if (g(imageElementId)) {
            g(imageElementId).style.display = "none";
            g(imagePreviousId).style.display = "none";
            g(imageNextId).style.display = "none";
            g(imagelistStatusId).style.display = "none";
        }//if
    } //if
    //Check if only one image
    else if (imageListSourcesArray.length == 1) {
        //Hide prev / next arrows
        g(imagePreviousId).style.display = "none";
        g(imageNextId).style.display = "none";
        g(imagelistStatusId).style.display = "none";
        UpdateImageListSrc();
    } //else if
    else {
        //Setup arrows events
        if(g(imagePreviousId))
            g(imagePreviousId).onclick = function() { PreviousImage(); };
        
        if (g(imageNextId))
            g(imageNextId).onclick = function() { NextImage(); };
        
        UpdateImageListSrc();
    } //else
}

function NextImage() {

    if (imageListArrayPointer < imageListSourcesArray.length - 1) {
        imageListArrayPointer++;
        UpdateImageListSrc();
    }
} //

function PreviousImage() {

    if (imageListArrayPointer > 0) {
        imageListArrayPointer--;
        UpdateImageListSrc();
    } //if
} //

function UpdateImageListSrc() {
    if (imageListImageElement) {
        imageListImageElement.src = imageListSourcesArray[imageListArrayPointer];
    } //if
    if (imagelistStatusElement) {
        imagelistStatusElement.innerHTML = "<span style='color:#FF0000;'>" + (imageListArrayPointer + 1) + "</span>&nbsp;<span style='color:#FFFFFF'>/&nbsp;" + (imageListSourcesArray.length) + "</span>";
    } //if
} //
/********************************************/
/******** IMAGELIST FUNCTIONS END ***********/
/********************************************/


/********************************************/
/******* MENU SCROLL FUNCTIONS START ********/
/********************************************/

var menuOffset = 0;
var menuScrollTimer;

function initMenuScroll(containerId, documentId, upArrowId, downArrowId, startOffset) {

    if (g(containerId) && g(documentId) && g(documentId) && g(downArrowId)) {

        if (g(containerId).offsetHeight > g(documentId).offsetHeight) {

            //Hide scroll
            g(upArrowId).style.display = "none";
            g(downArrowId).style.display = "none";
            menuOffset = 0;
        } //if
        else {
            g(documentId).style.top = startOffset + "px";
            menuOffset = startOffset;
        } //else
    } //if
} //initMenuScroll

function StartMenuScroll(direction, container, document) {

    scrollSpeed = 1;
    menuScrollTimer = setInterval("DoMenuScroll(" + direction + ", '" + container + "', '" + document + "')", 10);
}

function StopMenuScroll() {

    clearInterval(menuScrollTimer);
    scrollSpeed = 1;
}

function DoMenuScroll(direction, container, document) {

    //Move document
    if (direction == 1) {
        if (g(document).offsetTop - scrollSpeed > -1 * (g(document).offsetHeight - g(container).offsetHeight)) {
            g(document).style.top = g(document).offsetTop - scrollSpeed + "px";
        }
        else if (g(document).offsetTop > -1 * (g(document).offsetHeight - g(container).offsetHeight)) {
            g(document).style.top = -1 * (g(document).offsetHeight - g(container).offsetHeight) + "px";
        } //else
        else {
            StopMenuScroll();
        }  //else
    } //if
    else if (direction == -1) {
        if (g(document).offsetTop + scrollSpeed < 0) {
            g(document).style.top = g(document).offsetTop + scrollSpeed + "px";
        } //if
        else if (g(document).offsetTop < 0) {
            g(document).style.top = 0 + "px";
        } //else if
        else {
            g(document).style.top = 0 + "px";
            StopMenuScroll();
        } //else
    } //else

    //Update menuOffset
    menuOffset = g(document).offsetTop;
} //DoScroll

function goToDepartmentPage(linkUrl) {

    if (menuOffset < 0) {

        if (linkUrl.indexOf('?') > 0)
            linkUrl += "&msp=" + menuOffset;
        else
            linkUrl += "?msp=" + menuOffset;
    } //if

    window.location.href = linkUrl;
} //getMenuScrollOffsetParam

/********************************************/
/******** MENU SCROLL FUNCTIONS END *********/
/********************************************/

function LoadSmallFlashMap(){

    if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
        alert("This page requires AC_RunActiveContent.js.");
    } else {
        var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
        if(hasRightVersion) {  // if we've detected an acceptable version
            // embed the flash movie
           document.getElementById("divSmallFlashMapContainer").innerHTML = getFlashContent('100%','100%','area=' + m_area + '');
           
        } else {  // flash is too old or we can't detect the plugin
            var alternateContent = 'This content requires the Adobe Flash Player.<a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
            document.getElementById("divSmallFlashMapContainer").innerHTML = alternateContent;  // insert non-flash content
        }
    }
}

function LoadFlashMapDepartment() {
    if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
        alert("This page requires AC_RunActiveContent.js.");
    } else {
        var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
        if (hasRightVersion) {  // if we've detected an acceptable version
            // embed the flash movie
            document.getElementById("searchByMapDepartmentContainer").innerHTML = getFlashContent2('100%', '100%', 'area=' + m_area + '&companycolor=ff0000&SitePath=' + m_SitePath + '');

        } else {  // flash is too old or we can't detect the plugin
            var alternateContent = 'This content requires the Adobe Flash Player.<a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
            document.getElementById("searchByMapDepartmentContainer").innerHTML = alternateContent;  // insert non-flash content
        }
    }
}


function UpdateAreaDescription(areaPK) {
    ret = ScriptService.GetAreaDescription(areaPK, UpdateDescriptionOnComplete, OnTimeOut, OnError);
}

function UpdateDescriptionOnComplete(args) {

    var desctionArray = args.split("#");
    UpdateSearchByMapText(desctionArray[0], desctionArray[1])
}

function UpdateDepartmentDescription(departmentPK) {
    ret = ScriptService.GetDepartmentDescription(departmentPK, UpdateDescriptionOnComplete, OnTimeOut, OnError);
}

function OnTimeOut(args) {
    document.title = "Service call timed out.";

}

function OnError(args) {
    document.title = "Error calling service method.";
}

function getFlashContent(w,h,v){

			return AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0',
				'width',  w ,
				'height', h,
				'src', 'searchmenu',
				'quality', 'high',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'true',
				'scale', 'exactfit',
				'wmode', 'transparent',
				'devicefont', 'false',
				'id', 'searchmenu',
				'bgcolor', '#003300',
				'name', 'searchmenu',
				'menu', 'true',
				'allowScriptAccess','sameDomain',
				'allowFullScreen','false',
				'movie', 'searchmenu',
				'FlashVars', v,
				'salign', ''
				); //end AC code

}

function getFlashContent2(w,h,v){

		return AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,24,0',
			'width', w,
			'height', h,
			'src', 'searchcontent',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'exactfit',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', 'searchcontent',
			'bgcolor', '#ffffff',
			'name', 'searchcontent',
			'menu', 'true',
			'allowScriptAccess','sameDomain',
			'allowFullScreen','false',
			'movie', 'searchcontent',
			'FlashVars', v,
			'salign', ''
			); //end AC code

}


function UpdateSearchByMapText(title, description) {
    
    document.getElementById('divAreaDepartmentTitle').innerHTML = title;
    document.getElementById('divAreaDepartmentText').innerHTML = description;
    document.getElementById('divAreaDepartmentText').style.top = "0px";

    if ($("#divAreaDepartmentText").height() > $("#divAreaDepartmentTextContainer").height()) {
        $(".imgAreaDepartmentTextScrollUp").css({ "visibility": "visible" });
        $(".imgAreaDepartmentTextScrollDown").css({ "visibility": "visible" });
    }
    else {
        $(".imgAreaDepartmentTextScrollUp").css({ "visibility": "hidden" });
        $(".imgAreaDepartmentTextScrollDown").css({ "visibility": "hidden" });
    }
}


/********************************************/
/********** Flash/form FUNCTIONS START **********/
/********************************************/

var arrAreaIdDic = new Array(49, 50, 51, 52, 53, 54, 55, 56);

function updateForm(id, fieldtype) {

    //window.status = (">> Hello... " + fieldtype + ": " + id);
    if (m_state != '1') {
        if (fieldtype == "area") {
            if (m_basePageURL.indexOf("?") > 0)
                window.location = m_basePageURL + "&state=1&area=" + id;
            else
                window.location = m_basePageURL + "?state=1&area=" + id;
        } //          
    } else {
    if (fieldtype == "area") {
        
            //Update area title and description
            UpdateAreaDescription(id)

            id = arrAreaIdDic[id - 1];
            updateArea(id);
        }
        if (fieldtype == "department") {

            //Update department title and description
            UpdateDepartmentDescription(id);

            updateDepartment(id);
        }
    }
    return ">> Hi Flash.";
}

function updateArea(id) {
    
    //Change selected value in drop down list
    changeValue('ddlSearchCriteria5', id);

    //Update department dropdownlist
    updateDepartmentDropDownList(id);

    //Update large area / department flash - remove 01-08-2011 (jha)
    //updateFlashMenu($(".ddlSearchCriteria5")[0].options[$(".ddlSearchCriteria5")[0].selectedIndex].value);
    
    //setTimeout('__doPostBack(\'ddlSearchCriteria5\',\'\')', 0);
} //updateArea

function updateAreaFromCombo(selectelement) {

    //Update department dropdownlist
    updateDepartmentDropDownList(selectelement.value);

    //Update large area / department flash
    updateFlashMenu(selectelement);
}

function updateDepartment(id) {

    changeValue('ddlSearchCriteria6', id);
}

function UpdateSearchByMapText(title, description) {
    document.getElementById('divAreaDepartmentTitle').innerHTML = title;
    document.getElementById('divAreaDepartmentText').innerHTML = description;
    document.getElementById('divAreaDepartmentText').style.top = "0px";

    if ($("#divAreaDepartmentText").height() > $("#divAreaDepartmentTextContainer").height()) {
        $(".imgAreaDepartmentTextScrollUp").css({ "visibility": "visible" });
        $(".imgAreaDepartmentTextScrollDown").css({ "visibility": "visible" });
    }
    else {
        $(".imgAreaDepartmentTextScrollUp").css({ "visibility": "hidden" });
        $(".imgAreaDepartmentTextScrollDown").css({ "visibility": "hidden" });
    }
}

function changeValue(dllName, val) {

    for (i = 0; i < $("." +dllName)[0].length; i++) {
        if ($("." + dllName)[0].options[i].value == val) {
            $("." + dllName)[0].selectedIndex = i;
        }
    }
    
    /*
    for (i = 0; i < document.getElementById(dllName).length; i++) {
        if (document.getElementById(dllName).options[i].value == val) {
            document.getElementById(dllName).selectedIndex = i;
        }
    }
    */
}

function updateFlashMenu(selectelement) {

    var n = selectelement.selectedIndex;
    thisMovie("searchmenu").highliteArea(n);
}

function updateFlashContent(n) {

    if (m_state == '1') {
        thisMovie("searchcontent").highliteDepartment(n);

    }
}


function thisMovie(movieName) {
    /*
    if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName]
    }
    else {
    return document[movieName]
    }
    */
    return document[movieName];
}

function flashSearch() {
    $(".btnSearch").click();
}

function updateDepartmentDropDownList(area) {

    //ScriptService.GetDepartments(area, GetDepartmentsOnComplete, OnTimeOut, OnError); 
}

function GetDepartmentsOnComplete(args) {

    //Remove old values
    $('.ddlSearchCriteria6').empty();
    
    //Add new department values
    for (var i = 0; i < args.length; i++) {

        var val = args[i].split("#")[0];
        var text = args[i].split("#")[1];
        $('.ddlSearchCriteria6').append('<option value="' + val + '">' + text + '</option>');
    }//for
}
/********************************************/
/*********** Flash/form FUNCTIONS END ***********/
/********************************************/


$.urlParam = function(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}


function paymentRegistered() {

    alert("paymentRegistered called!");
}
