/**
 * Open a popup window with the given parameters
 * @param pUrl URL to open
 * @param pTarget Window target
 * @param pParameter Window parameter string
 * @param pSrcHandle Handle of the popup source page (if set popups with same handle are opened only once)
 * @param pWidth Width of the window
 * @param pHeight Height of the window
 * @return Window handle for the new window or null if the window could not be opened
 */
function cms_openWindow(pUrl, pTarget, pParameter, pSrcHandle, pWidth, pHeight) {
	// center popup on screen
	if (pWidth) {
		var winX = (screen.availWidth/2)-(pWidth/2);
		pParameter += ",width=" + pWidth + ",left=" + winX + ",scrX=" + winX;
	}
	if (pHeight) {
		var winY = (screen.availHeight/2)-(pHeight/2);
		pParameter += ",height=" + pHeight + ",top=" + winY + ",scrY=" + winY;
	}

	var win = null;
	var isPopup = false;
	if (pTarget==null || pTarget=="" || pTarget=="_self") {
	  document.location = pUrl;
	  win = window;
	}
	else {
	  win = window.open(pUrl, pTarget, pParameter);
	  isPopup = true;
	}
	try {
		if (isPopup) {
			if (pSrcHandle && document.cookie) {
				// set cookie
				document.cookie = pSrcHandle + "=true; Path=/";
			}
			win.focus();
			win.setTimeout("try { self.focus() } catch (ex) {}", 1000);
		}
	}
	catch (e) {
		// ignore
	}
	return win;
}


function openDWB(params) {
        if(typeof(params) == "undefined"){
            params = "";
        }
				if(params == "op=intro"){
						params = "partner=&app_kind=default";
				}
        var url = "http://vtp.audi.com/awdwbek/controller.do?" + params;
        var name = "DWBSATELLIT";
        var width = "792";
        var height = "600";
        var spec = "scrollbars=yes,resizable=yes";

        open_satellite_without_querystring(url, width, height, true, name, spec);
}

function openNWB (params) {
	open_neuwagenboerse(params);
}

function open_neuwagenboerse(params) {
        if(typeof(params) == "undefined"){
            params = "";
        }
        var url = "http://neuwagenboerse.audi.de/neuwagenboerse/controller?" + params;
        var name = "NWBSATELLIT";
        var width = "792";
        var height = "600";
        var spec = "scrollbars=yes,resizable=yes";

        open_satellite_without_querystring(url, width, height, true, name, spec);
}




function openAK(params){
    if(typeof(params) == "undefined"){
      params = "";
    }
	var lang = "de";
	open_konfigurator_with_params(params, lang, '')
}

// *** Wichtig AK4 LIVE URL ***
var base_AK4_Url = "http://ak4-de.audi.de/entry.html";

function open_konfigurator(lang, model){
    var url = base_AK4_Url;
    if(model){
    		if(model.indexOf("allroad") != -1){
    			model = "aaq";
    		}
        url += "?carline="+model;
    }
    open_satellite(url, 800, 600, true, "AK4SATELLIT","status=yes");
}

function open_konfigurator_with_params(params, lang, model){
	var url = base_AK4_Url;
	if (params) {
		url+="?"+params;
		if (model) {
    		if(model.indexOf("allroad") != -1){
    			model = "aaq";
    		}
			url += "&carline="+model;
		}
	}
    open_satellite_without_querystring(url, 800, 600, true, "AK4SATELLIT","status=yes")
}


function open_satellite_without_querystring(url, w, h, resizable, name, spec){
  if(typeof(spec) == "undefined"){
      spec = 'directories=no,menubar=no,toolbar=no,status=no,scrollbars=yes';
      if(w>0 && h>0){
         if(! resizable){
           //spec += ",scrollbars=yes";
           spec += ",resizable=no";
         } else{
           spec += ",scrollbars=yes";
           spec += ",resizable=yes";
         }
         spec += (",width=" + w);
         spec += (",height=" + h);
      }
  }
  else{
      if(w>0 && h>0){
         if(! resizable){
           spec += ",scrollbars=yes";
           spec += ",resizable=no";
         } else {
           spec += ",scrollbars=yes";
           spec += ",resizable=yes";
         }
         spec += (",width=" + w);
         spec += (",height=" + h);
      }
  }

  var win=window.open(url, name, spec);
  win.focus();
  
}




window.onresize = checkSubnavPos;
//Variable für den popup-timer
var zu;
var subMenuActivated = 0;

//creating Subnav-SubMenu (dropdownbox)
function subMenu(whichMenu,whichLevel,whichPreselected)
{
	//props for checking subnav-submenu-position
	if (whichLevel == 2)
		{this.top = 8;}
	else
		{this.top = 28;}
	//reading Menu-Array, writing HTML-Codes
	this.activeMenu = eval(whichMenu);

	this.subMenuItemsList = "";
	if (this.activeMenu != 0)
	{
		for (var i = 0; i < this.activeMenu.length; i+=2)
			{this.subMenuItemsList += "<a" + this.activeMenu[i+1] + this.activeMenu[i] + "</a>";}
		this.subMenuItemsList = "<div id='subnav_sub_list'>" + this.subMenuItemsList + "</div>";
	}

}

//function for subnav-submenu-popup (dropdownbox)
//args = name of menuset

function showSubMenu(whichMenu)
{
	//workaround mozilla menuwidth bug
	if (navigator.appVersion.indexOf("MSIE") != -1)
		{document.getElementById("subnav_submenu").style.width = 155 + "px";}
	else
	{
		//document.getElementById("subnav_submenu").style.width = "auto";
		document.getElementById("subnav_submenu").style.width = 155 + "px";
		//mozilla clipping workaround
		document.getElementById("subnav").style.clip = "rect(auto,auto,800px,0px)";
	}
	document.getElementById("subnav_submenu").style.visibility = "hidden";
	document.getElementById("subnav_submenu").style.display = "none";
	//writing html-strings
	if (whichMenu.activeMenu != 0)
	{
		document.getElementById("subnav_submenu").innerHTML = whichMenu.subMenuItemsList;
		document.getElementById("subnav_submenu").style.display = "block";
	}
	//positioning menu-layers
	if (whichMenu.activeMenu != 0)
	{
		document.getElementById("subnav_submenu").style.top = whichMenu.top + "px";
		document.getElementById("subnav_submenu").style.visibility = "visible";
		document.getElementById("subnav_submenu").style.display = "block";
		document.getElementById("subnav_submenu").style.backgroundImage = "url(fileadmin/AUDIvorlagen/main/icons/bg_subnav_submenu.gif)";
	}
	//if there is no submenu
	else if (whichMenu.activeMenu == 0)
	{
		document.getElementById("subnav_submenu").style.visibility = "hidden";
		document.getElementById("subnav_submenu").style.display = "none";
	}
	//reset timeout
	window.clearTimeout(zu);
}

function submenuCollaps()
{
	//verstecken des popup-menues
	window.clearTimeout(zu);
	document.getElementById("subnav_submenu").style.visibility="hidden";
	document.getElementById("subnav_submenu").style.display="none";
}

//************************************************************//
//functions for subnavigation fold show/hide
var subnavzu;
var subnavauf;
var slidetimer;
slidetoggle = 0;
var isIE = '';
if (navigator.appVersion.indexOf("MSIE") != -1)	isIE = true;

function subnavTimer()
{
	if (!document.getElementById('subnav'))	return;
	window.clearTimeout(subnavzu);
	try
	{
		if (parseInt(currSubnavLeft) < cms4imp_fullStageWidth)
			{subnavauf = window.setTimeout("showSubnav()",50);}
	}
	catch (e)
	{
		// ignore
	}
}

function showSubnav()
{
	if (slidetoggle==1)	return;
	var subnavContent=document.getElementById("subnav_content");
	if (!subnavContent || !subnavContent.childNodes || subnavContent.childNodes.length == 0) return;

	window.clearTimeout(subnavauf);
	document.getElementById("subnav").style.visibility="visible";
	document.getElementById("subnav").style.display = "block";
	document.getElementById("subnav").style.clip = "rect(auto,160px,auto,160px)";
	mycurrentwidth = 160;
	slidecount = 4;
	slideSubnavOn();
	slidetoggle=1;
}

function subnavCollaps()
{
	if (!document.getElementById('subnav'))	return;
	try
	{
		if (parseInt(currSubnavLeft) < cms4imp_fullStageWidth)
		{
			window.clearTimeout(subnavauf);
			window.clearTimeout(subnavzu);
			slidecount = 8;
			mycurrentwidth=0;
			//window.clearTimeout(slidetimer);
			slideSubnavOff();
			slidetoggle=0;
		}
	}
	catch (e)
	{
		// ignore
	}
}
function dropdown_subnavmouseEnter()
{
	window.clearTimeout(subnavzu);
	window.clearTimeout(slidetimer);
}

//**************************************************************//
function dropdown_mouseEnter(){window.clearTimeout(zu);}

function dropdown_mouseLeave(whichMenu)
{
	switch (whichMenu)
	{
	case 'main':
		zu = window.setTimeout("menuCollaps()",50);
		break;
	case 'sub':
		zu = window.setTimeout("submenuCollaps()",300);
		break;
	case 'subnav':
		subnavzu = window.setTimeout("subnavCollaps()",500);
		break;
	}
}

//===================================================
//functions for subnav-positioning
//===================================================

// allows to be overriden by templates with extra-wide stages
cms4imp_fullStageWidth = 640;
cms4imp_foldMinHeight = 300;

//detect mouse-over image position for correct layer positioning

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function getElementByClass(pClassName) {
	elements = window.document.getElementsByTagName('*');

	for (i=0; i<elements.length; i++) {
		if (elements[i].className == pClassName) {
			return elements[i];
		}
	}
}

function checkSubnavPos() {
	// Do nothing if there's no mainnav
	if (!document.getElementById('mainnav')) return;

	// Stretch the content frame at least as high as the mainnav
	var frame = getElementByClass("screen_4");
	var mainnav_end = document.getElementById("mainnav_end");
	if (frame && mainnav_end) {
		var posY = findPosY(mainnav_end);
		if ((findPosY(frame) + frame.offsetHeight) < posY) {
			var height = posY - findPosY(frame);
			if (navigator.appVersion.indexOf("MSIE") != -1) {
				frame.style.height = height + "px";
			} else {
				frame.style.minHeight = height + "px";
			}
		}
	}

	var currLeft = findPosX(document.getElementById('mainnav'));
	var currWidth = currLeft - cms4imp_fullStageWidth;
	var currBrowserWidth = currLeft + 140;

	if (!document.getElementById('subnav')) {
		// Set the background unconditionally if there's no subnav
		if (document.body.background && document.body.background.indexOf("/bg.gif") == -1) {
			document.body.background = global_staticpath_images + "/bg.gif";
		}
		return;
	}

	if (currWidth > 160) {
		document.getElementById("subnav").style.left = (currLeft - currWidth) + "px";
		document.getElementById("subnav").style.width = currWidth + "px";
	} else {
		document.getElementById("subnav").style.left = (currLeft - 160) + "px";
		document.getElementById("subnav").style.width = 160 + "px";
	}
	currSubnavLeft = document.getElementById("subnav").style.left;
	if (parseInt(currSubnavLeft) < cms4imp_fullStageWidth) {
		document.getElementById("subnav").style.visibility = "hidden";
	} else {
		document.getElementById("subnav").style.visibility = "visible";
		document.getElementById("subnav").style.display = "block";
		document.getElementById("subnav").style.clip = "rect(auto,auto,auto,0px)";
	}

	// resize the fold and set the background image depending on the subnav height
	var mysubnavheight = document.getElementById("subnav").offsetHeight;
	if (mysubnavheight < cms4imp_foldMinHeight) {
		document.getElementById("subnav").style.height = cms4imp_foldMinHeight + "px";
		if (document.body.background && document.body.background.indexOf("/bg.gif") == -1) {
			document.body.background = global_staticpath_images + "/bg.gif";
		}
	} else if (mysubnavheight > cms4imp_foldMinHeight && mysubnavheight < 468) {
		document.getElementById("subnav").style.height = 468 + "px";
		if (document.body.background && document.body.background.indexOf("/bg_long.gif") == -1) {
			document.body.background = global_staticpath_images + "/bg_long.gif";
		}
	} else if (cms4imp_foldMinHeight == 492) {
		if (document.body.background && document.body.background.indexOf("/bg_long2.gif") == -1) {
			document.body.background = global_staticpath_images + "/bg_long2.gif";
		}
	}

}

//slide function for subnav fold
function slideSubnavOn() {
	slidecount = slidecount*2;
	mycurrentwidth = (mycurrentwidth - slidecount);
	var mycurrentrect = "rect(auto,160px,auto," + mycurrentwidth + "px)";
	if (isIE) {
		document.getElementById("subnav").style.clip = mycurrentrect;
	} else {
		document.getElementById("subnav").style.visibility = "hidden";
		document.getElementById("subnav").style.clip = "rect(auto,160px,auto,0px)";
		document.getElementById("subnav").style.clip = mycurrentrect;
		document.getElementById("subnav").style.visibility = "visible";
	}
	if (mycurrentwidth<=0) {
		window.clearTimeout(slidetimer);slidetimer=0;
	} else {
		slidetimer = window.setTimeout("slideSubnavOn()",5);
	}
}

function slideSubnavOff() {
	slidecount = slidecount*3;
	mycurrentwidth = mycurrentwidth + slidecount;
	var mycurrentrect = "rect(auto,160px,auto," + mycurrentwidth + "px)";
	if (isIE) {
		document.getElementById("subnav").style.clip = mycurrentrect;
	} else {
		document.getElementById("subnav").style.visibility = "hidden";
		document.getElementById("subnav").style.clip = "rect(auto,160px,auto,0px)";
		document.getElementById("subnav").style.clip = mycurrentrect;
		document.getElementById("subnav").style.visibility = "visible";
	}
	if (mycurrentwidth>=160) {
		window.clearTimeout(slidetimer);slidetimer=0;
	} else {
		slidetimer = window.setTimeout("slideSubnavOff()",1);
	}
}
