// ================================= declare global mdv namespace =======================================================


mdv.beg = {
    menuItems: new Array('Auskunft', 'Haltestelleninfos', 'Fahrplanaenderungen', 'AllgemeineInfos', 'MeinFahrplan'),
    widthLeftColumn: 518,
    footerHeight: 68,
    heightHeader: 86,
    heightMenu: 68,
    heightImgHeader: 85,
    heightEfaMenu: 0,
    mapMinWidth: 450,
    mapMinHeight: 420,
    sideMargin: 70,
    autoupdate: null,
    dynVal: {},
    dynText: {},
    debug : false
};


// ================================= dimensions ==========================================================

// Set dimensions
mdv.beg.setDimensions = function() {
    var availDim = mdv.beg.getAvailDimensions();
    
    mdv.beg.setLayoutDim(availDim.windowWidth);
    mdv.beg.setLeftColDim(availDim.contentHeight, availDim.windowWidth);
    mdv.beg.setRightColDim(availDim.rightContentWidth, availDim.contentHeight);
    mdv.beg.setMapDim(availDim.rightContentWidth, availDim.contentHeight);
    mdv.beg.setOverlayDim(availDim.windowWidth, availDim.contentHeight);
}

// Get dimensions.
mdv.beg.getAvailDimensions = function() {
    var windowHeight, windowWidth, contentHeight, rightContentWidth;
    var scrollDelta = (mdv.common.getIEVersion() != -1) ? 0 : 16;
    var belowContent = mdvLib.$('belowContentRight')  ? true : false;
	
	windowHeight = mdv.common.getInnerBrowser().height;
	windowWidth = mdv.common.getInnerBrowser().width - mdv.beg.sideMargin;
    
    // full width map?
    if (mdvLib.$('leftColumn') && mdvLib.$('leftColumn').style.display === 'none') {
        rightContentWidth = windowWidth;
    }
    else {
        rightContentWidth = windowWidth - mdv.beg.widthLeftColumn;
    }
    contentHeight = windowHeight - mdv.beg.heightHeader - mdv.beg.footerHeight;
    
    if (jQuery('#portal-mainmenu').css('display') != 'none') {
        contentHeight = contentHeight - mdv.beg.heightMenu;
    }
    else {
        contentHeight += 3;
    }
    
	if (rightContentWidth < mdv.beg.mapMinWidth) {
		rightContentWidth = mdv.beg.mapMinWidth;
        windowWidth = mdv.beg.widthLeftColumn + mdv.beg.mapMinWidth; 
        contentHeight -= scrollDelta; // horizontal scrollbar
	}

    if(mdvLib.$('bilderleiste')) {
        contentHeight = contentHeight - mdv.beg.heightImgHeader + mdv.beg.footerHeight - 70;
    }
    
	if (contentHeight < mdv.beg.mapMinHeight) {
		contentHeight = mdv.beg.mapMinHeight; 
        // vertical scrollbar
        rightContentWidth -= scrollDelta; 
        windowWidth -= scrollDelta;
	}
    else if (belowContent) {
        // vertical scrollbar
        rightContentWidth -= scrollDelta;  
        windowWidth -= scrollDelta;
    }
    
    if (belowContent) {
       contentHeight += 37;
    }
    
    if(document.getElementById('cms-content')) {
        contentHeight += 3;
    }
    
    if(document.getElementById('cms-content')) {
        contentHeight -= 1;
    }
    
    windowWidth = ((mdv.common.getIEVersion()) === -1 && mdvLib.$('kupu-editor-text')) ? (windowWidth - 6) : windowWidth; 
    
    return { rightContentWidth: rightContentWidth, contentHeight: contentHeight, windowWidth: windowWidth}
}

// Set layout dimensions. 
mdv.beg.setLayoutDim = function(windowWidth) {    
    if (mdvLib.$('visual-portal-wrapper')) {
        mdvLib.$('visual-portal-wrapper').style.width = windowWidth + 'px';
    }
}

// Set dimensions of left colum.
mdv.beg.setLeftColDim = function(contentHeight, windowWidth) { 
    var elem = mdvLib.$('scrollContent');
    var infoLink = 0;
    
    // StationsDB (flash object)
    if (jQuery('object').length > 0) {
        return;
    };
    
    if (mdvLib.$('banner')) {
        infoLink = mdvLib.$('banner').clientHeight + 5; 
    }

    if (elem) {
        elem.style.height = contentHeight - infoLink + 'px';
    }
    
    elem = mdvLib.$('scrollInner');
    if (elem) {
        elem.style.height = contentHeight - infoLink + 'px';
    }

    elem = mdvLib.$('portal-content');
    if (elem) {
        elem.style.height = contentHeight + 'px';
        elem.style.width = windowWidth + 'px';
    }
    
    elem = mdvLib.$('cms-content');
    if (elem) {
        elem.style.height = contentHeight + 'px';
        elem.style.width = windowWidth + 'px';
    }
}


// Set dimensions of right column.
mdv.beg.setRightColDim = function(rightContentWidth, contentHeight) {
    if (mdvLib.$('rightColumn')) {
        mdvLib.$('rightColumn').style.width = rightContentWidth + 'px';
       
        if(mdvLib.$('belowContentRight')) {
            mdvLib.$('belowContentRight').style.width = rightContentWidth + 'px';
            return;
            mdvLib.$('infoText1').style.width = rightContentWidth - 270 + 'px';
            mdvLib.$('infoText2').style.width = rightContentWidth - 270 + 'px';
        }
   }
}


// Set dimensions of overlay.
mdv.beg.setOverlayDim = function(contentWidth, contentHeight) {
    var elem = mdvLib.$('overlay');
    if (elem) {
        elem.style.top =  (contentHeight / 2); 
        elem.style.left = (contentWidth / 2); 
    }
}


// Set map dimensions.
mdv.beg.setMapDim = function(rightContentWidth, contentHeight) {
    if (!mdvLib.$('mdvMap') || typeof mdv.map.mdvMap === 'undefined')  {
        return;
    }
  
    contentHeight += 2;
    
    mdvLib.$('mdvMap').style.width = rightContentWidth + 'px';
    mdvLib.$('mdvMap').style.height = contentHeight + 'px';   
    mdvLib.$('mdvMapOverlay').style.width = rightContentWidth + 'px';
    mdvLib.$('mdvMapOverlay').style.height = contentHeight + 'px';     
    jQuery('#mdvMapOverlayText').css('margin-top', (contentHeight / 2 -10) + 'px');     
       
    if (mdv.map.mdvMap) {
        mdv.map.mdvMap.viewport.style.height = contentHeight + 'px';
        mdv.map.mdvMap.viewport.style.width = rightContentWidth + 'px';
        mdv.map.resizeMapComponents();
    }
}


// ================================= main menu ===========================================================

// Fix main menu
mdv.beg.fixMainMenu = function () {
    // change home button
    if (jQuery('#portal-mainmenu .navTreeLevel0 li').length > 0) {
        jQuery('#portal-mainmenu .navTreeLevel0 li:eq(0) a').text(' ');
        jQuery('#portal-mainmenu .navTreeLevel0 li:eq(0) a').append('&nbsp;');
        jQuery('#portal-mainmenu .navTreeLevel0 li:eq(0) a').css('cssText', 'width: 46px !important; padding: 0 !important;background: url("/begWR/img/layout/home_button.gif") no-repeat #01a0e4 !important');
        if (jQuery('#portal-mainmenu .navTreeLevel0 li:gt(0) .navTreeCurrentNode').length === 0) {
            jQuery('#portal-mainmenu .navTreeLevel0 li:eq(0) a').css('cssText', 'width: 46px !important; padding: 0 !important;background: url("/begWR/img/layout/home_button.gif") no-repeat #01a0e4 !important; background-position: 0px -30px !important;');
        }
    }
    // remove last separator
    if (jQuery('#portal-mainmenu .navTreeLevel1 li')) {
        jQuery('#portal-mainmenu .navTreeLevel1 li').eq(jQuery('#portal-mainmenu .navTreeLevel1 li').length-1).children('div').children('a').css('cssText','border-right: 0 none !important;');
    }
    // toggle login/logoff item
    if (jQuery('.actionicon-user-logout').length != 0) {
        jQuery('.navTree .navTree li:has(span:contains("Anmelden"))').css('cssText','display: none !important');
        jQuery('.navTree .navTree li:has(span:contains("Log in"))').css('cssText','display: none !important');
    }
    else if (jQuery('.navTree .navTree li:has(span:contains("Abmelden"))')) {
        jQuery('.navTree .navTree li:has(span:contains("Abmelden"))').css('cssText','display: none !important');
        jQuery('.navTree .navTree li:has(span:contains("Log off"))').css('cssText','display: none !important');
    }
}

// ================================= event listener ======================================================

mdv.common.attachEventListener(window, 'resize', function() { 
    mdv.beg.setDimensions();
}, false);


jQuery(document).ready(function() {
    // exchange image for internet explorer 6
    if (mdv.common.getIEVersion() === 6 && mdvLib.$('logo_bayernfahrplan')) {
        mdvLib.$('logo_bayernfahrplan').src = '/begWR/img/layout/logo_bayernfahrplan.gif';
    }
    
    // disable map for delfi
    if (jQuery('#itdLPxx_delfiActive').length > 0 || (jQuery('#delfi').length > 0 && jQuery('#delfi').attr('checked'))) {
        mdv.beg.setInputForDelfi();
    }
    else if (jQuery('#name_via').length > 0){
        mdv.beg.removeInputForDelfi();
    }
    
    // fix main menu
    mdv.beg.fixMainMenu();
    
    if(mdvLib.$('efa')) {
        // mot exclusion
        mdv.beg.setCombMOT();
        // bayern ticket
        if (mdvLib.$('inclMOT_15')) {
            mdv.common.attachEventListener(mdvLib.$('inclMOT_15'), 'click', function() { 
                if (mdvLib.$('inclMOT_15').checked) {
                    mdv.beg.toggleBayernTicket(false);
                }
            }, false);
        }
        if (mdvLib.$('inclMOT_16')) {
            mdv.common.attachEventListener(mdvLib.$('inclMOT_16'), 'click', function() { 
                if (mdvLib.$('inclMOT_16').checked) {
                    mdv.beg.toggleBayernTicket(false);
                }
            }, false);
        }
        // calendar
        mdv.beg.loadDatepicker();
        // ttb mode switcher
        mdv.beg.switchTTBMode();
    }
    
    // show minimap
    if (mdv.map) {
        mdv.map.toggleMiniMap(true);
    }
});

mdv.common.attachEventListener(window, 'load', function() {   
    // set dimenions
    if (mdvLib.$('cms-content') && !mdvLib.$('efa')) {
        mdv.beg.setDimensions();
    }
    // check key (add info)
    if  ( jQuery('#itdLPxx_addInfo').length > 0) {
         mdv.common.checkKey();
    }
    // enable delfi after reset of odv
    if (jQuery('#itdLPxx_odvReseted').length > 0 && jQuery('#itdLPxx_odvReseted').attr('value') === 'true') {
        // set delfi checkbox
        jQuery('#delfi').attr('checked',true);
        // set delfi parameter
        mdv.beg.setDelfiParams();
    }
}, false);





