/**
 For divs with id start with "top" and end with "____" (4 underscores)
change the position property from 'relative' to 'static'
Chen 1-29-2008
*/
function ChangePosition(){
	var elems = document.getElementsByTagName('div');
	for (var i=0;i<elems.length;i++) {
		if(((elems[i].id.indexOf("____"))> 0) && (elems[i].id.indexOf("top")==0)){
		//	alert(elems[i].id);
			elems[i].style.position = "static";
		}
	}
}

/**
 For td with id start with "top" and end with "Col1"
change the padding for the td
Jin Chen 1-29-2008.  For use with DWS secondary grid.
*/
function ChangePadding(){
	var elems = document.getElementsByTagName('td');
	var browserName = navigator.appName;
	for (var i=0;i<elems.length;i++) {
		if(((elems[i].id.indexOf("Col1")+4)== elems[i].id.length) && (elems[i].id.indexOf("DWS")==0)){
			if(browserName == "Microsoft Internet Explorer"){
				elems[i].style.paddingLeft = "2px";
			}else{
				elems[i].style.padding = "0px";
			}
		}
	}
}

/* Used for links to channels in the same domain */
function launchChannel (dest) {
	/*
	var myPath = location.pathname;	
	location.href = "/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath;
	*/
	launchChannelWithParams(dest,'');
}

/* Request Parameters channelRedirect*/
function launchChannelWithParams (dest, params) {
	var myPath = location.pathname;
	var mySearch = location.search.replace(/\?/,"");
    var queryStr = '';
	
	if (params.length > 0) {
		params = "&" +params.replace(/ /g, "").replace(/,/g, "&");
	}
	//queryStr = "&" + mySearch +  params;
	queryStr = params;
	
	//if (myPath.length > 0)
	//	myPath = myPath.replace(".", "");
	//alert(queryStr);
	//alert("blah - /mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + queryStr);
	//location.href = "/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + queryStr;
	if (dest == 'cpoInventorySearch'|| dest == 'cpoOffers') {	// ugly code just to make it work for CPOInventorySearch .......
		if(queryStr.indexOf('vgnextnoice=1')>0) {
			location.href = "/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + "&vgnextnomenu=1&vgnextrefresh=1&vgnextnoice=1&incontextedit=true";
		}
		else {
			location.href = "/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + "&&";
		}
	}
	else {
		if(queryStr.indexOf('vgnextnoice=1')>0) {
			//alert("/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + queryStr + "&vgnextnomenu=1&vgnextrefresh=1&vgnextnoice=1&incontextedit=true");
			location.href = "/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + queryStr + "&vgnextnomenu=1&vgnextrefresh=1&vgnextnoice=1&incontextedit=true";
		}
		else
		{
			//alert("/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + queryStr);
			
			location.href = "/mbusa-templating/DWSMaster/views/channelRedirect.jsp?dest=" + dest + "&path=" + myPath + queryStr;
		}
	}
}
function launchCPOWindow() {
	//var path = location.pathname.split("DWS");
 //var thisdcode = path[1].substring(0,5);
 var thisdcode = (location.pathname.split("DWS"))[1].substring(0,5);
	if(thisdcode == "01329"){
		thisdcode = "01120";
	}
	if(thisdcode == "22120"){
		thisdcode = "22127";
	}
	if(thisdcode =="21100"){
		thisdcode = "47100";
	}
	if(thisdcode == "52101"){
		thisdcode = "51158";
	}
	if(thisdcode == "73404"){
		thisdcode = "73100";
	}
	window.open('http://www.mypreownedmercedes.com/matrix.php?mbid='+thisdcode,'name','resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=no,status=no,height=615,width=995');
}
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
/*   +++++++++++++++++++++++++++++++++++++++++++++   Special Features Home +++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
// Special feature slideshow Do not edit below unless you know what you are doing
var splFtrCnt=0;
var splFtrTimeDelay=10000;
var splFtr=new Array(4);
var myinterval;

function addSpecialFeature(){
	splFtr[splFtrCnt]= new Array(3);
	for(var i=0;i<arguments.length;i++){
		splFtr[splFtrCnt][i]=arguments[i];
	}
	splFtrCnt++;
}
function startSpecialFeatures(){
	splFtrCnt=0;
	setTimeout("switchFeature()", 500);
	myinterval = setInterval("switchFeature()", splFtrTimeDelay);
}
function stopSpecialFeatures(){
	clearInterval(myinterval);
}
function switchFeature(){
	envokeSpecialFeatureSwitch(splFtr[splFtrCnt][2],splFtr[splFtrCnt][1],splFtr[splFtrCnt][0]);
	if(splFtrCnt+1>=splFtr.length){
		splFtrCnt=0;
	}
	else{
		splFtrCnt++;
	}
}
function envokeSpecialFeatureSwitch(bgURL,id,imgId){
	changeBG(bgURL);
	changeTR(id);
	changImgThumb(imgId);
}
function changeBG(bgURL){
	if(null != document.getElementById('center_bg')){
		document.getElementById('center_bg').style.background="#000000";
		document.getElementById('center_bg').style.backgroundImage="url('" + bgURL + "')";
		//following code was comented out by Dmitriy on 12/8/2008 because there were some browser compatiblity issues with fade in effects for background image.
		/*if(null != document.getElementById('bgimage')){
			crossfade(document.getElementById('bgimage'), bgURL, '1');
		}
		else{
			document.getElementById('center_bg').innerHTML="<img alt=\"\" src=\""+bgURL+"\" id=\"bgimage\" />";
		}
		*/
	}
}
function changeGalleryBG(bgURL){
	if(null != document.getElementById('gallery_bg')){
		document.getElementById('gallery_bg').style.backgroundImage="url('" + bgURL + "')";
	}
}
function changeTR(id){
	document.getElementById('txtRgnWrap').innerHTML=document.getElementById(id).innerHTML;
}
function changImgThumb(imgId){
	var table = document.getElementById('specialFeatureSetTable');
	var specFtImages = getElementsByClass('specFtrThmbImg',table,'img');
	var image = document.getElementById(imgId);
	resizeImg (image, 135);
	for(var i = 0; i < specFtImages.length; i++){
		if(image != specFtImages[i]){
			resizeImg(specFtImages[i], 100);
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}  
function changeBaseImg(bgURL){
	if(bgURL.length > 0){
		document.getElementById('mainBaseImage').style.backgroundImage="url('" + bgURL + "')";
	}
}

/*   ++++++++++++++++++++++++++++++++++++++++++++ Gradual Image Resize  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
// adjust these two parameters to control how fast or slow the images grow/shrink 
// how many milliseconds we should wait between resizing events
var resizeDelay = 10;
// how many pixels we should grow or shrink by each time we resize
var resizeIncrement = 5;
// this will hold information about the images we're dealing with
var imgCache = new Object();
/*
The getCacheTag function just creates a (hopefully) unique identifier for
each <img> that we resize.
*/
function getCacheTag (imgElement) {
	return imgElement.src + "~" + imgElement.offsetLeft + "~" + imgElement.offsetTop;
}
/*
We're using this as a class to hold information about the <img> elements
that we're manipulating.
*/
function cachedImg (imgElement, increment) {
	this.img = imgElement;
	this.cacheTag = getCacheTag(imgElement);
	this.originalSrc = imgElement.src;
	
	var h = imgElement.height;
	var w = imgElement.width;
	this.originalHeight = h;
	this.originalWidth = w;
	
	increment = (!increment) ? resizeIncrement : increment;
	this.heightIncrement = Math.ceil(Math.min(1, (h / w)) * increment);
	this.widthIncrement = Math.ceil(Math.min(1, (w / h)) * increment);
}


/*
This is the function that should be called in the onMouseOver and onMouseOut
events of an <img> element. For example:

<img src='onesmaller.gif' onMouseOver='resizeImg(this, 150, "onebigger.gif")' onMouseOut='resizeImg(this)'>

The only required parameter is the first one (imgElement), which is a
reference to the <img> element itself. If you're calling from onMousexxx, 
you can just use "this" as the value.

The second parameter specifies how much larger or smaller we should resize
the image to, as a percentage of the original size. In the example above,
we want to resize it to be 150% larger. If this parameter is omitted, we'll
assume you want to resize the image to its original size (100%).

The third parameter can specify another image that should be used as the
image is being resized (it's common for "rollover images" to be similar but
slightly different or more colorful than the base images). If this parameter
is omitted, we'll just resize the existing image.
*/
function resizeImg (imgElement, percentChange, newImageURL) {
	// convert the percentage (like 150) to an percentage value we can use
	// for calculations (like 1.5)
	var pct = (percentChange) ? percentChange / 100 : 1;
	
	// if we've already resized this image, it will have a "cacheTag" attribute
	// that should uniquely identify it. If the attribute is missing, create a
	// cacheTag and add the attribute
	var cacheTag = imgElement.getAttribute("cacheTag");
	if (!cacheTag) {
		cacheTag = getCacheTag(imgElement);
		imgElement.setAttribute("cacheTag", cacheTag);
	}
	
	// look for this image in our image cache. If it's not there, create it.
	// If it is there, update the percentage value.
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal) {
		imgCache[cacheTag] = new Array(new cachedImg(imgElement), pct);
	} else {
		cacheVal[1] = pct;
	}
	
	// if we're supposed to be using a rollover image, use it
	if (newImageURL)
		imgElement.src = newImageURL;
	
	// start the resizing loop. It will continue to call itself over and over
	// until the image has been resized to the proper value.
	resizeImgLoop(cacheTag);
	return true;
}


/*
This is the function that actually does all the resizing. It calls itself
repeatedly with setTimeout until the image is the right size.
*/
function resizeImgLoop (cacheTag) {
	// get information about the image element from the image cache
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal)
		return false;
	
	var cachedImageObj = cacheVal[0];
	var imgElement = cachedImageObj.img;
	var pct = cacheVal[1];
	var plusMinus = (pct > 1) ? 1 : -1;
	var hinc = plusMinus * cachedImageObj.heightIncrement;
	var vinc = plusMinus * cachedImageObj.widthIncrement;
	var startHeight = cachedImageObj.originalHeight;
	var startWidth = cachedImageObj.originalWidth;
	
	var currentHeight = imgElement.height;
	var currentWidth = imgElement.width;
	var endHeight = Math.round(startHeight * pct);
	var endWidth = Math.round(startWidth * pct);
	// if the image is already the right size, we can exit
	if ( (currentHeight == endHeight) || (currentWidth == endWidth) )
		return true;
	
	// increase or decrease the height and width, making sure we don't get larger or smaller than the final size we're supposed to be
	var newHeight = currentHeight + hinc;
	var newWidth = currentWidth + vinc;
	if (pct > 1) {
		if ((newHeight >= endHeight) || (newWidth >= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	} else {
		if ((newHeight <= endHeight) || (newWidth <= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	}
	
	// set the image element to the new height and width
	imgElement.height = newHeight;
	imgElement.width = newWidth;
	
	// if we've returned to the original image size, we can restore the
	// original image as well (because we may have been using a rollover
	// image in the original call to resizeImg)
	if ((newHeight == cachedImageObj.originalHeight) || (newWidth == cachedImageObj.originalwidth)) {
		imgElement.src = cachedImageObj.originalSrc;
	}
	
	// shrink or grow again in a few milliseconds
	setTimeout("resizeImgLoop('" + cacheTag + "')", resizeDelay);
}
// IXF1.11 :: Image cross-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var ixf = { 'clock' : null, 'count' : 1 }
/* ****************************************************************************
 List the images that need to be cached
**************************************************************************** */
//ixf.imgs = ['','',''];
//*****************************************************************************
//cache the images
//ixf.imgsLen = ixf.imgs.length;
ixf.cache = [];
/*for(var i=0; i<ixf.imgsLen; i++)
{
	ixf.cache[i] = new Image;
	ixf.cache[i].src = ixf.imgs[i];
}
*/
cacheCounter=0;function cacheImage(imageString){ixf.cache[cacheCounter] = new Image;ixf.cache[cacheCounter].src = imageString;cacheCounter++;}
//crossfade setup function
function crossfade()
{
	//if the timer is not already going
	if(ixf.clock == null)
	{
		//copy the image object 
		ixf.obj = arguments[0];
		//copy the image src argument 
		ixf.src = arguments[1];
		//store the supported form of opacity
		if(typeof ixf.obj.style.opacity != 'undefined')
		{
			ixf.type = 'w3c';
		}
		else if(typeof ixf.obj.style.MozOpacity != 'undefined')
		{
			ixf.type = 'moz';
		}
		else if(typeof ixf.obj.style.KhtmlOpacity != 'undefined')
		{
			ixf.type = 'khtml';
		}
		else if(typeof ixf.obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string			
			ixf.type = (ixf.obj.filters.length > 0 && typeof ixf.obj.filters.alpha == 'object' && typeof ixf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			ixf.type = 'none';
		}
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			ixf.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(ixf.type != 'none')
		{
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			ixf.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			//set positioning classname
			ixf.newimg.className = 'idupe';
			
			//set src to new image src
			ixf.newimg.src = ixf.src
			//move it to superimpose original image
			ixf.newimg.style.left = ixf.getRealPosition(ixf.obj, 'x') + 'px';
			ixf.newimg.style.top = ixf.getRealPosition(ixf.obj, 'y') + 'px';
			
			//copy and convert fade duration argument 
			ixf.length = parseInt(arguments[2], 10) * 1000;
			
			//create fade resolution argument as 20 steps per transition
			ixf.resolution = parseInt(arguments[2], 10) * 20;
			
			//start the timer
			ixf.clock = setInterval('ixf.crossfade()', ixf.length/ixf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			ixf.obj.src = ixf.src;
		}
		
	}
};
//crossfade timer function
ixf.crossfade = function()
{
	//decrease the counter on a linear scale
	ixf.count -= (1 / ixf.resolution);
	
	//if the counter has reached the bottom
	if(ixf.count < (1 / ixf.resolution))
	{
		//clear the timer
		clearInterval(ixf.clock);
		ixf.clock = null;
		
		//reset the counter
		ixf.count = 1;
		
		//set the original image to the src of the new image
		ixf.obj.src = ixf.src;
	}
	
	//set new opacity value on both elements
	//using whatever method is supported

	switch(ixf.type)
	{
		case 'ie' :
			ixf.obj.filters.alpha.opacity = ixf.count * 100;
			ixf.newimg.filters.alpha.opacity = (1 - ixf.count) * 100;
			break;
			
		case 'khtml' :
			ixf.obj.style.KhtmlOpacity = ixf.count;
			ixf.newimg.style.KhtmlOpacity = (1 - ixf.count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.obj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newimg.style.MozOpacity = (1 - ixf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.obj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newimg.style.opacity = (1 - ixf.count);
	}
	
	//now that we've gone through one fade iteration we can show the image that's fading in
	ixf.newimg.style.visibility = 'visible';
	ixf.newimg.style.display = 'block';
	
	//keep new image in position with original image in case text size changes mid transition or something
	ixf.newimg.style.left = ixf.getRealPosition(ixf.obj, 'x') + 'px';
	ixf.newimg.style.top = ixf.getRealPosition(ixf.obj, 'y') + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	if(ixf.count == 1)
	{
		//remove the duplicate image
		ixf.newimg.parentNode.removeChild(ixf.newimg);
	}
};
//get real position method
ixf.getRealPosition = function()
{
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null)
	{
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	
	return this.pos;
};
/*   +++++++++++++++++++++++++++++++++++++++++++++   Special Features Home End +++++++++++++++++++++++++++++++++++++++++++++++++++++++  */

/******for DWS Hours and Location Page (JSP version)*************/
function popmap(flexHttpRoot, dlrzip5){
					var dlrzip5 = dlrzip5; 
					var flexHttpRoot = flexHttpRoot;
				//	alert(flexHttpRoot);
					var url = flexHttpRoot+"/MBHome.swf?appMode=dlr&state=GET_DIRECTIONS&zip="+dlrzip5;
					window.open(url,"newwin","width=978,height=489,resizable=1,scrollbars=1");
				}

/*added by ruslan testing cobalt tracking phone number code*/
function cobaltphone(){
	var dn='1-888-881-4075';
	var cn=dn;
	if (document.cookie && new RegExp(/phone=([^;]*)/).test(document.cookie)) {
		cn=RegExp.$1;
	}else {
		if (new RegExp(/(?:\&|\?)(?:amp;)?phone=([^&]*)/i).test(parent.document.location.href)) {
			cn=RegExp.$1;
			var m=new Date();
			m.setMonth(m.getMonth() + 1);
			document.cookie='phone=' + cn + '; expires=' + m.toGMTString();
		}
	}
	document.write(cn);
}
/*enddd*/

/*added by ruslan, campaign tracking*/
function CampaignCode(){
	var CActive = 1;
	if (CActive != 1 ){
		Campaign = 0;
		cls = 0;
		chc = 0;
	}
}
function getcc(){
	//var Campaign = "";//session.getAttribute("Campaign");
	//if(Campaign == null){Campaign = "4170";}
	return Campaign;
}
function getchc(){
	//var chc = "30081P";//session.getAttribute("chc_code");
	//if(chc == null){chc="38084P";}
	return chc;
}
function getcls(){
	//var cls = "10765";//session.getAttribute("cls_code"); 
	//if(cls == null){cls="11033";}
	return cls;
}
/*enddd*/