/**
 *	MME Scripts
 *	--------------------------
 */

window.onload = function() {
	handleModelSubmenu();

	var menu = document.getElementById('menu');
	var content = document.getElementById('content');
	if(content) {
		window.onresize = function() {
			try {
				content.style.height = 'auto !important';	
			} catch (e) {
				content.style.height = '100%';
			}			
			
			var width = document.body.offsetWidth;
			var space = 0;
			
			if(width > 800) {
				space = 306 + Math.floor((width - 800)/6);
				content.style.marginLeft = space + 'px';
			}

			var mHeight = menu.offsetHeight + 96;
			var cHeight = content.offsetHeight;

			if(mHeight > cHeight) {
				content.style.height = (mHeight + 10) + 'px';				
			}
		};
		window.onresize();
	}	
	
	parseImageLinks(document.getElementsByTagName('a'));
}

function handleModelSubmenu() {
	var navigation = document.getElementById('navigation');
	var subnav = document.getElementById('subnavigation');	
	if(!navigation || !subnav) return;

	var title = getNodeValue(findChildOfType(subnav, 'h3'));
	if(!title) return;

	title = title.replace(/(^\s+)|(\s+$)/g, '')

	var regEx = new RegExp().compile('^\\s*' + title + '\\s*$', 'i');
	var item, link, items = navigation.getElementsByTagName('li');

	for(var i=0; i<items.length; i++) {
		item = items.item(i);
		link = findChildOfType(item, 'a');
		
		if(regEx.test(getNodeValue(link))) {
			item.replaceChild(subnav, link);
			break;
		}
	}
}

/**
 *	dom helpers
 *	--------------------------
 */

function findChildOfType(parent, child) {
	return findSiblingOfType(parent.firstChild, child)
}

function findParentOfType(target, parent) {
	var item = target.parentNode;
	while(item) {
		if(item.nodeName.toLowerCase() == parent) {
			return item;
		}	item = item.parentNode;
	}	return null;
}

function findSiblingOfType(sibling, child) {
	var item = sibling;
	while(item) {
		if(item.nodeName.toLowerCase() == child){
			return item;
		}	item = item.nextSibling;
	}	return null;
}

function getNodeValue(node) {
	return (node && node.firstChild)? 
		node.firstChild.nodeValue : null;
}

/**
 *	Countries
 *	--------------------------
 */

function openCountryLink(select) {
	var lang = select[select.selectedIndex].value;
	window.open('/countryforward.jsp?country='+lang);
}

/**
 *	Images autopreload
 *	--------------------------
 */

var REG_button = new RegExp().compile('but_');

function parseImageLinks(list) {
	var link, child, src;
	for(var i=0; i<list.length; i++) {
		link = list.item(i);
		child = link.firstChild;
		
		if(/img/i.test(child.nodeName)) {
			src = child.getAttribute("src");			
			if(REG_button.test(src)) {
				new HoverImage(link, child);
			}
		}
	}
}

function HoverImage(link, image) {
	var offState = new Image()
		offState.src = image.src;
	
	var onState = new Image();
		onState.src = offState.src.replace(/\.(gif|jpg)/, '_hi.$1');

	link.onmouseover = function() { image.src = onState.src; }
	link.onmouseout = function() { image.src = offState.src; }
}

/**
 *	Movies
 *	--------------------------
 */

function openSpecial(movie, w, h) {
	var x = ',left=' + (screen.width - w)/2;
	var y = ',top=' + (screen.height - h)/2;
	var special = window.open('/playmovie.jsp?movie=' + movie, '', 'width=' + w + ',height=' + h + x + y);
}

/**
 *	popups
 *	--------------------------
 */

function popRoute() {
	prnt = window.open('/corporate/route/', '', 'width=520,height=600,scrollbars=1');
}

function popupImage(path) {
	window.open('/imageviewer.jsp?path=' + path, 
		'', 'width=320,height=240,left=100,top=100');
}

function launchCenter(url, name, height, width) {
  var str = "height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width;
  if (window.screen) {
    var ah = screen.availHeight - 30;
    var aw = screen.availWidth - 10;

    var xc = (aw - width) / 2;
    var yc = (ah - height) / 2;

    str += ",left=" + xc + ",screenX=" + xc;
    str += ",top=" + yc + ",screenY=" + yc;
  }
  newWindow=window.open(url, name, str);
}

function openLancer() {
    //launchCenter('/lancer/swf/main.jsp','_blank','420','760');    
	launchCenter('/lancer2005/swf/index.jsp','_blank','420','760');    
}

function openLancerEvo() {
    launchCenter('http://www.mitsubishi-evolution8.com/swf/index.jsp?id=null&language=default','_blank','425','764');    
}

function openLancerEvolution() {
	launchCenter('http://www.mitsubishi-evolution9.com/swf/index.jsp?language=default', '', 425,764)
}

function openLancerEvolutionx() {
	launchCenter('http://www.lancer-evolution.eu/', '_blank', '700', '1024');
}

function openGrandis() {
    launchCenter('http://www.newgrandis.eu/?language=default','_blank','426','764');    
}

function openPajero() {
	launchCenter("http://www.mitsubishi-pajero.com/swf/index.jsp?lang=default","", 525, 792);
}

function openColt3door() {
	launchCenter('http://www.newcolt.com/?language=default', '', 525, 792);
}

function openNewpajero() {
	launchCenter("http://www.newpajero.com/swf/index.jsp?lang=default","", 525, 792);
}

function openColtczc() {
	launchCenter("http://www.newcolt.com/?language=default","", 525, 792);
}