/* Freemap Slovakia  "freemap" layout driver scripts
*/
Site['InitGO']     	  = false;
Site['InitMap']       = false;
Site['InitCMS']       = false;

var Theme = new Array();
Theme['FullScreen'] = false;
Theme['IconSearch']   = "./themes/nevada/icon_search.png";
Theme['IconPoint']    = "./themes/nevada/icon_point.png";
Theme['IconPoint2']   = "./themes/nevada/icon_point2.png";
Theme['IconClear']    = "./themes/nevada/icon_clear.png";
Theme['Languages']    = false;
var HelpSearchCounter = 0;
var HelpLayerCounter  = 0;

var AreaOpened        = new Array;
AreaOpened['left']    = true;
AreaOpened['middle']  = false;
AreaOpened['info']    = false;

// DOKUMENT READY
$j(document).ready(function(){
	$j('#Screen').css("display", "block");
	
	window.onresize();
	if (Site['LoadLayout']) { 
		ShowGOArea(Site['LoadLayoutURL']);
	} else {    
		if (Site['u'] == "c") ShowCMS(); else
		if (Site['u'] == "g") ShowGO(); else 
		if (Site['u'] == "m") ShowMap();
		if (typeof Site['u'] == "undefined") 
			ShowMap();
	}

	if (Theme_StartFullScreen == 1) {
		$('LeftAreaContainer').style.display     = "none";
		$('SearchArea').style.display     = "none";
		$('SearchAreaClose').style.display     = "none";
		$('SearchAreaSwitcher').style.display     = "none";
		AreaOpened['left']    = false;
		FullScreenEnable();
	}  
    window.onresize();

	// aktivujeme jazyky
	$j("#Language").click(function() {
			if (!Theme['Languages']) {
				$j('#Languages').load('?c=nevada.languages'); 
				$('Languages').style.display = "block";
			} else {
				$('Languages').style.display = "none";
			}
			Theme['Languages'] = !Theme['Languages'];
		});

	// napojime layer switcher
	$j("#LayerA").click(function() { SetMapLayer("A"); });
	$j("#LayerT").click(function() { SetMapLayer("T"); });
	$j("#LayerC").click(function() { SetMapLayer("C"); });
	$j("#LayerO").click(function() { SetMapLayer("O"); });
	$j("#LayerX").bind("mouseover", function() { LayersCustomShow(); });
	$j("#LayerX").bind("mouseout", function() { LayersCustomHide(); });
	$j("#LayersCustom").bind("mouseover", function() { LayersCustomShow(); });
	$j("#LayersCustom").bind("mouseout", function() { LayersCustomHide(); });
	$j("#MapLayer").buttonset();
	$j("#LayerX").button();

	Site['Loading'] = false;
});
	 
// DOKUMENT RESIZE
window.onresize = function() {
    // height
    var theHeight = 0;
    if (window.innerHeight) {
        theHeight=window.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            theHeight=document.documentElement.clientHeight;
        } else {
            if (document.body) {
                theHeight=document.body.clientHeight;
            }
        }
    }
    WindowHeight = theHeight;

    // width
    var theWidth = 0;
    if (window.innerWidth) {
        theWidth=window.innerWidth;
    } else {
        if (document.documentElement && document.documentElement.clientWidth) {
            theWidth=document.documentElement.clientWidth;
        } else {
            if (document.body) {
                theWidth=document.body.clientWidth;
            }
        }
    }
    WindowWidth = theWidth;

	if (Site['InitMap']) {
    	var mapelement            = document.getElementById('map');
	    mapelement.style.left     = (((AreaOpened['left']) && (!Theme['FullScreen']))? 301:12) + "px";
    	mapelement.style.width    = (WindowWidth - (((AreaOpened['left']) && (!Theme['FullScreen']))? 301:12) ) + "px";
	    mapelement.style.height   = (WindowHeight - ((Theme['FullScreen'])? 44 : 44)) + "px";
	    if (map)  map.updateSize();	
    }
    var left                  = document.getElementById('LeftArea');
    var login                 = document.getElementById('login_form');
   	var CMSMenu               = document.getElementById('CMSMenu');
    var CMSMiddleArea         = document.getElementById('CMSMiddleArea');
   	var GOMiddleArea          = document.getElementById('GOMiddleArea');
   	CMSMenu.style.left        = Math.round((WindowWidth - 800)/2) + "px";
    CMSMiddleArea.style.left  = Math.round((WindowWidth - 800)/2) + "px";
   	GOMiddleArea.style.left   = Math.round((WindowWidth - 800)/2) + "px";
	$j('#MiddleAreaShadow').css("width", WindowWidth + 'px');
}

function ShowMap() {
	SetLocParam('u', 'm');
    $j('#LayoutCMS').fadeOut("slow");
    $j('#LayoutGO').fadeOut("slow");
    $j('#LayoutMap').fadeIn("fast");
    $j('#MyLayers').css("display", "block");
    $j('#CopyrightArea').css("display", "block");
    $j('#OZArea').css("display", "none");

	LoadLibrary("core.map");
	// niekedy ked je default go a prepnem na mapu je cista
	if (!Site['InitMap']) {
		RotateCopyright();
		MapInit();
		CheckBrowser();
		FinderChangeType(4);
		ThemeMapLayerChanged("", false);
		map.updateSize();
	}
	Site['InitMap'] = true;	
	if (!Site['Loading']) {
		window.onresize();
	}
}

function ShowCMSArea(URL) {
	SetLocParam('u', 'c');
    $j('#LayoutMap').fadeOut("slow");
    $j('#LayoutGO').fadeOut("slow");
    $j('#MyLayers').css("display", "none");
    $j('#CopyrightArea').css("display", "none");
    $j('#OZArea').css("display", "block");
    $j('#LayoutCMS').fadeIn("fast");
    if (URL){
        LoadContent(URL);
    }
}

function ShowGOArea(URL) {
	SetLocParam('u', 'g');
    $j('#LayoutMap').fadeOut("slow");
    $j('#LayoutCMS').fadeOut("slow");
    $j('#MyLayers').css("display", "none");
    $j('#CopyrightArea').css("display", "none");
    $j('#OZArea').css("display", "block");
    $j('#LayoutGO').fadeIn("fast");
    if (URL){
    	Ajax(URL, {
    	    TargetId: "GOContent"
	    } );
    }
}

function ShowCMS() {
    ShowCMSArea("?c=content.view.3");
}

function ShowGO() {
    ShowGOArea("?c=core.go");
}

function ShowForum() {
    ShowCMSArea("?c=forum.Show");
}

function ThemeMapLayerChanged(MapLayer, Hover) {	
	path = "/themes/nevada/";
	if (Map['CurrentMapLayer'] == "A") 
		$j("#LayerA").click();
	if (Map['CurrentMapLayer'] == "T") 
		$j("#LayerT").click();
	if (Map['CurrentMapLayer'] == "C") 
		$j("#LayerC").click();
	if (Map['CurrentMapLayer'] == "O") 
		$j("#LayerO").click();
}

// Vytvori popup
function ThemeRenderPopUpWindow(DivName) {
	var div  = document.getElementById(DivName);
	if (div) {
		div.innerHTML = "<div id='"+DivName+"_top'></div><div id='"+DivName+"_content'></div><div id='"+DivName+"_bottom'></div>";
	} else {
		alert('Div ' + DivName + ' doesn\'t exists');
	}
}

// FULLSCREEN MODE FUNCTIONS
function ToggleFullScreen() {
	if (Theme['FullScreen'])
		FullScreenDisable();
	else
		FullScreenEnable();
}

function FullScreenEnable() {
	Theme['FullScreen'] = true;
	AreaClose('left');
	window.onresize();
	if (typeof BtnToggleFullScreen != "undefined")
		BtnToggleFullScreen.activate();
	if (typeof Map != "undefined")
		Map['Permalink'].updateLink();
}

function FullScreenDisable() {
	Theme['FullScreen'] = false;
	AreaOpen('left');
	window.onresize();
	BtnToggleFullScreen.deactivate();
	Map['Permalink'].updateLink();
}

// AjaxRequest
function ThemeOpenArea(Area, URL, Options) {
    if (Area == "free_area") {
        if (
		  (URL.indexOf("%2FMapLayer") != -1) || 
		  (URL.indexOf("%2FLayer") != -1) 	  
		  )
            if (!AreaOpened['left'])
                return "LeftArea";
        if (URL.indexOf("c=finder.") != -1) {
            AreaOpen('left');
            return "LeftArea";
        } else
        if (URL.indexOf(".infobox") != -1) {
            AreaOpen("info");
            return "InfoBox";
        } else
        if (URL.indexOf("forum") != -1) {    
            return "CMSContent";
        } else
        if ((URL.indexOf("c=core.admin") != -1) ||
            (URL.indexOf("c=core.hiking.") != -1) ||
            (URL.indexOf("c=core.user.profile") != -1) ||
            (URL.indexOf("c=core.user.payments") != -1) ||
            (URL.indexOf("c=core.weather.") != -1) ||
            (URL.indexOf("c=fls.") != -1) ||
            (URL.indexOf("c=logvisualizer.") != -1) ||
            (URL.indexOf("c=weather.") != -1) ||
            (URL.indexOf("c=gallery.") != -1) ||
            (URL.indexOf("c=relations.") != -1) ||
            (URL.indexOf("c=user.profile") != -1) ||
            (URL.indexOf("c=user.login") != -1) ||
            (URL.indexOf("c=login") != -1) ||
            (URL.indexOf("c=drivelog") != -1) ||
            (URL.indexOf("cms/BrowserCompatibility") != -1) ||
            (URL.indexOf("cms/FileDownload") != -1) ||
            (URL.indexOf("c=admin") != -1) 
            ) {
            AreaOpen('middle');
            return "MiddleAreaContainer";
        } else {
            AreaOpen('left');
            return "LeftArea";
        }
    }
    return Area;
}    

// area = left, right
function AreaOpen(AreaName) {
    if (AreaName == "left")
        ReturnAreaName = "LeftArea";
    if (AreaName == "middle")
        ReturnAreaName = "MiddleAreaContainer";
    if (AreaName == "info")
        ReturnAreaName = "InfoBox";
    if (AreaOpened[AreaName])
        return ReturnAreaName;
    AreaOpened[AreaName]    = true;
    if (AreaName == "left") {
        $j('#LeftAreaContainer').fadeIn("fast");
        $('SearchArea').style.display     = "block";
        $('SearchAreaClose').style.display     = "block";
        $('SearchAreaSwitcher').style.display     = "block";
    }
    if (AreaName == "middle") {
        $('MiddleAreaContainer').style.display = "inline";
        $('MiddleAreaShadow').style.display = "inline";
        $('MiddleAreaClose').style.display = "inline";
        $('MiddleAreaCloseR').style.display = "inline";
        $('MiddleAreaHeader').style.display = "inline";
        $('MiddleAreaFooter').style.display = "inline";
    }
    if (AreaName == "info") {
        $j('#InfoBoxArea').fadeIn("fast");
        $j('#info_box_close2').fadeIn("slow");
    }
    window.onresize();
    return ReturnAreaName;
}

// area = left, right
function AreaClose(AreaName) {
	if (typeof Map != "undefined") 
		if (Map['CurrentControl']) {
			if (Map['CurrentControl'].className == "FinderControl")
				return "";
		}
    if (AjaxIsRunning) {
        return "";
    }
    if (!AreaOpened[AreaName])
        return "";
    AreaOpened[AreaName]    = false;
    if (AreaName == "left") {
        $j('#LeftAreaContainer').fadeOut("slow");
        $('SearchArea').style.display     = "none";
        $('SearchAreaClose').style.display     = "none";
        $('SearchAreaSwitcher').style.display     = "none";
    }
    if (AreaName == "middle") {
        $('MiddleAreaContainer').style.display = "none";
        $('MiddleAreaShadow').style.display = "none";
        $('MiddleAreaClose').style.display = "none";
        $('MiddleAreaCloseR').style.display = "none";
        $('MiddleAreaHeader').style.display = "none";
        $('MiddleAreaFooter').style.display = "none";
    }
    if (AreaName == "info") {
        $j('#info_box_close2').fadeOut("slow");
        $j('#InfoBoxArea').fadeOut("slow");
    }
    window.onresize();
    return "";
}

// delayed close
function AreaCloseDelayed(AreaName) {
    if (AreaOpened[AreaName]) {
        AreaOpened[AreaName] = false;
        var t = setTimeout("if (AreaOpened['"+AreaName+"'] === false) { AreaOpened['"+AreaName+"'] = true; AreaClose('"+AreaName+"'); } ",2000);
        return;
    }
}

function LoadContent(URL) {
    Ajax(URL, {
        TargetId: "CMSContent"
    } );
}
