var debug = false;

/* DropDown menu */
function startList() 
{
    if (document.all && document.getElementById) {
        navRoot = document.getElementById("nav");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            
            if (node.nodeName=="LI") {
                node.onmouseover=function() {
                    this.className+=" over";
                    }
                node.onmouseout=function() {
              
                this.className=this.className.replace(" over", "");
                }
            }
        }
     }
}

//
//  getPageSize()
//
function getPageSize() {
        
    var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

/* History management */

function init()
{
    
    window.dhtmlHistory.create({
            toJSON: function(o) {
                    return Object.toJSON(o);
            }
            , fromJSON: function(s) {
                    return s.evalJSON();
            }
    });

    window.onload = function() {
        dhtmlHistory.initialize();
        startList();
        document.onmousemove = getPosition;
    };

    $('overlay').hide().observe('click', (function() { hidePopup(); }).bind(this));    

}

/* Event popup */
var popupwindow = 'freshPopup';
var loadinghtml = '<div style="width:100%;height:400px;background-position: center 50px;background-repeat: no-repeat;background-image: url(\'gfx/ajax/loading.gif\')"></div>';



function loading(height, width, marginleft, margintop, divclass)
{
    var arrayPageSize = getPageSize();
    $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
    
    $(popupwindow).style.width = width;
    $(popupwindow).style.height = height;
    $(popupwindow).style.marginLeft = marginleft;
    $(popupwindow).style.marginTop = margintop;
    $(popupwindow).className = divclass;
    
    $('popupcontent').innerHTML = loadinghtml;    
    new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.4 });
    $(popupwindow).show();    
}

function showEvent(id)
{
    if (debug) alert("henter event" + id);
    loading('302px', '435px', '-220px', '-200px', 'popup_event');
    //shopPopup();
    City2.Website.AjaxService.GetEventDetails(id, onSuccessGetEventDetails, onFailed, null);     
    
}

function hidePopup()
{
    if (debug) alert("lukker vindue");        
    $(popupwindow).hide();
    $('overlay').hide();
}

function showPopupFrame(url)
{
    loading('408px','606px', '-303px', '-204px', 'popup_shop');    
    content = '<div style="padding: 10px;"><iframe id=myEditor style="width: 580px; height: 370px" name=main marginwidth=0 framespacing=0 marginheight=0 src="'+url+'" scrolling="auto" frameborder="0"></iframe></div>';
        
    $('popupcontent').innerHTML = content;        
}

function shopPopup()
{
    // stretch overlay to fill page and fade in
    var arrayPageSize = getPageSize();
    $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
    new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.4 });
}

function showShop(id)
{
    if (document.getElementById('shopcatlist'))
        $('shopcatlist').hide();
    if (debug) alert("Henter butik" + id);
    loading('408px','606px', '-303px', '-204px', 'popup_shop');    
    City2.Website.AjaxService.GetShopDetails(id, onSuccessGetShopDetails, onFailed, null);
}

function onSuccessGetShopDetails(value, userContext, methodName)
{
     if (debug) alert("success: " + value);                    
    $('popupcontent').innerHTML = value;    
}

function onSuccessGetEventDetails(value, userContext, methodName) 
{
    if (debug) alert("success: " + value);

    //fix height
    $('popupcontent').innerHTML = value;

    //alert('DIV: ' + $('eventpopupcontent').offsetHeight);
    var res = ($('eventpopupcontent').offsetHeight < 240 ? '240' : $('eventpopupcontent').offsetHeight) + 'px';
    //alert(res);
    $('eventpopupcontent').style.height = res;
} 

//shared
function onFailed(ex, userContext, methodName) { 
    if (debug)
        alert(ex.get_exceptionType());                                                                         
    else
        alert("Vi beklager der skete en fejl, Tryk venligst F5 for at loade siden igen. "); // + ex.get_exceptionType());
} 

//Menu div show/hide

var menuTimer;
var menuShowed = "";

function showMenu(sender, div){
    if (menuShowed != "")
        hideMenu();
        
    menuShowed = div;
    if (sender != null && document.getElementById(sender) != null)
        pos = findPos(document.getElementById(sender));
    else
        pos = findPos(sender);
    
    $(div).style.top = pos[1] + 27 + "px";
    $(div).style.left = pos[0] + 0 + "px";    
    if (!$(div).visible())
        //Effect.Appear(div);
        Effect.BlindDown(div, { duration: 0.3 });
}

function onMenuOver(sender, div){

    //alert("menuShowed=" + menuShowed);
    if (menuShowed != "" && menuShowed != div)
    {        
        hideMenu();
        showMenu(sender, div);
    }    
    else if (menuShowed == "" || !$(div).visible()){
        //alert('viser menu');
        showMenu(sender, div);
    }
    else{ //alert('gør ingenting');
    }
        
    clearTimeout(menuTimer);
}

function onMenuOut(sender, div){
    menuTimer = setTimeout("hideMenu()", 600);
}

function hideMenu()
{
    if (menuShowed != "" && $(menuShowed).visible())
    {
        menuTimer = null;
        Effect.BlindUp(menuShowed, { duration: 0.2 });
        menuShowed = "";
    }
}

var mcursor;

function getPosition(e) {
    e = e || window.event;

    var cursor = { x: 0, y: 0 };

    if (e.pageX || e.pageY) {

        cursor.x = e.pageX;

        cursor.y = e.pageY;

    }

    else {

        cursor.x = e.clientX +

           (document.documentElement.scrollLeft ||

           document.body.scrollLeft) -

           document.documentElement.clientLeft;

        cursor.y = e.clientY +

           (document.documentElement.scrollTop ||

           document.body.scrollTop) -

           document.documentElement.clientTop;

    }

    mcursor = cursor;
    return cursor;

}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}


function openMusicPopup() {

    var clickstring = "window.open(this.href, 'player', 'width=320,height=240,toolbar=no,titlebar=no,status=no,scrollbars=no,resizable=no,menubar=no,location=no');hidePopup();return false;";
    var content = '<center><a href="musik/klip_1.html" onclick="' + clickstring + '"><img src="musik/player1.png" alt="musikafspiller" /><br /><br />Start musikafspilleren i et nyt vindue</a></center>';
    
    loading('258px','606px', '-303px', '-154px', 'popup_shop');    
    content = '<div style="padding: 10px;">'+content+'</div>';
        
    $('popupcontent').innerHTML = content;            
}