//Hover function ie

var siteBaseUrl = '/';
function setBaseUrl(baseUrl){ siteBaseUrl = baseUrl;}

var onloadEvents = new Array();

//Hover function ie
sfHover = function() {
	//Trigger only for IE
	
	if (document.all && !window.opera){
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		if (sfEls){
			for (var i=0; i<sfEls.length; i++){
				
				sfEls[i].style.filter = 'alpha(opacity=85)';
				
				sfEls[i].onmouseover=function() {
					this.className += " sfhover";
				}
				sfEls[i].onmouseout=function() {
					this.className = this.className.replace(new RegExp(" sfhover\\b"), "");					
				}
			}
		}
	}
}
if (window.opera){
	var t = document.getElementsByTagName('html');
	t[0].style.height = 'auto';
	t[0].style.minHeight = '100%';
}
//if (window.attachEvent){window.attachEvent("onload", sfHover);}

if (document.all && window.attachEvent){
	addToOnload('attachButtonHover()');	
}

var attachButtonHover = function(){
	var list = document.getElementsByTagName('button');
	for (var i=0; i<list.length; i++) {
		list[i].onmouseover=function() {
			this.className+=" buttonHover";
		}
		list[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" buttonHover\\b"), "");
		}
	}
}

function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() { 
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}


function disableField(element){ document.getElementById(element).style.display = 'none';}

//Select onchange for homepage
function selectOnChange(){
	var cpForm = document.getElementById('control-panel-form');
	var selectForm = document.getElementById('select-vd-display');
	selectForm.onchange = function () {
			cpForm.submit();
	}
	
	var selectForm2 = document.getElementById('select-vd-relating');
	selectForm2.onchange = function () {
			cpForm.submit();
	}
	
	document.getElementById('select-vd-submit').style.display = 'none';
}

var clearOnClick = function(id) {
	var form = document.getElementById(id);	
	
	if (form)
	{
		var formElems = form.elements;
		
		for (var i = 0; i < formElems.length; i++)
		{
			if (formElems[i].type == 'text')
			{
				formElems[i].oldValue = formElems[i].value;
				
				formElems[i].onfocus = function () { 
					if (this.value == this.oldValue) {
						this.value = ''; 
					}
				} 
				
				formElems[i].onblur = function () {
					if (this.value == '') {
						this.value = this.oldValue;
					}
				}
				
			}
		}
	}
}

addToOnload('clearOnClick("index-service-booking")');	

//Generic select option url jump
function jumpTo(s){
    var url = s.value;
	if (url !='') loadUrl(url);
}

function loadUrl(url){ window.location.href = url;}

function processKeyPress(e){
	var e = window.event  || e;
	
	try {
		var key = e.keyCode || e.which;
	} catch (e){
		var key = 0;
	}
	//Esc or x (lowercase)
	if (key == 27 || key == 120) closePopup();
	
}

function popupKeyListener() {document.onkeypress = processKeyPress; }

function closePopup() {
	e = document.getElementById('map-display');
	if (e){
		e.parentNode.removeChild(e);
	} 
}

function attachOnClickMap(id, dealershipId, addressType){
	element = document.getElementById(id);
	element.style.cursor = 'pointer';
	var divWidth = 600;
	var divHeight = 450;		
	element.onclick = function (){
		//check if this element exists.
		e = document.getElementById('map-display');
		if (!e){
			myDimensions = getWindowDimensions();
			myWidth = myDimensions[0];
			
			popupKeyListener();
			if (document.all && !window.opera){
				var spinner = document.createElement("DIV");
				spinner.id = "spinner";
				spinner.className = 'busySignal';
				spinner.style.zIndex = '10000';
				spinner.style.width = '628px';
				spinner.style.top = '50px';
				spinner.style.left = (myWidth / 2) - (divWidth / 2) + "px";
				spinner.style.height =  '490px';				
				document.body.appendChild(spinner);		
			}
			
			var myDiv = document.createElement("DIV");
			myDiv.id = "map-display";
			myIframe = document.createElement('iframe');
			myIframe.src = siteBaseUrl+'map.php?dealerId='+dealershipId+'&type='+addressType;	
			myIframe.style.zIndex = '1';
			myIframe.scrolling = 'no';
			myIframe.width = divWidth;
			myIframe.height = divHeight+40;					
			myIframe.frameBorder = "0";
			
			//Append
			myDiv.appendChild(myIframe);
			
			myDiv.style.position = 'absolute';
			myDiv.style.width = divWidth+'px';
			myDiv.style.top = '50px';
			myDiv.style.left = (myWidth / 2) - (divWidth / 2) + "px";
			document.body.appendChild(myDiv);
			
		}
		
		return false;
	}
}

function clearSpinner() {
	var e = document.getElementById('spinner');	
	if (e){
		e.parentNode.removeChild(e);	
	}
}

function getWindowDimensions(){
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

var FooterEffects = {
	imageList : ['/media/images/footer-commerical.gif','/media/images/footer-passenger.gif'],
	current : 1,
	effectInterval : 0,
	init : function(){
		//preload other image
		var image = new Image();
		image.src = '/media/images/footer-commerical.gif';
		FooterEffects.effectInterval = setInterval("FooterEffects.next()", 4000);
	},
	next : function(){
		FooterEffects.current = FooterEffects.current ==  1 ? 0 : 1;
		FooterEffects.fadeOut();
	},
	fadeIn : function(){
		$('logo-image').src = FooterEffects.imageList[FooterEffects.current];
		$('logo-image').effect('opacity',{duration: 500}).start(0,1);
	},
	fadeOut : function	(){
		$('logo-image').effect('opacity',{duration: 500, onComplete:FooterEffects.fadeIn}).start(1,0);
	}
}


var AttachEnquiryForm = function(){
	
	var e = new Element('a',{
		'class' : 'more-button right',
		'styles' : {'cursor': 'pointer'},
		'events' : {
				'click' : function(){
					this.remove();
					$('stdForm').setStyle('display','block');
				}
			}
	});
	e.setText('Apply for this position');
	e.injectBefore('stdForm');
	$('stdForm').setStyle('display','none');
}

var VehicleTabs = {
	
	tabList : ['SPECS','EQUIPMENT','OPTIONS','PRICE'],
	active : 'SPECS',
	
	init: function(){
		$('info-ENQUIRY').setStyle('display','none');
		$('enquiry-button').addEvent('click', this.showTab.bind(this, 'ENQUIRY')); 
		$('enquiry-button').removeProperty('href');
		$('enquiry-button').setStyle('cursor','pointer');
		
		this.tabList.each(function(tab){
			try {
				var tabAnc = $('tab-'+tab);
				
				tabAnc.addEvent('click',this.showTab.bind(this, tab));
				tabAnc.removeProperty('href');
				
				var myLi = tabAnc.getParent();
				
				if (this.active != tab){
					$('info-'+tab).setStyle('display','none');	
				} else {
					myLi.addClass('tab-active');
				}
			}
			catch(e){}
		}, this);
	},
	
	showTab: function(tab){
		this.active = tab;
		this.tabList.each(function(tab){
			try {
			if (this.active != tab){
				$('info-'+tab).setStyle('display','none');
				$('tab-'+tab).parentNode.className = '';
			} else {
				$('info-'+tab).setStyle('display','block');
				$('tab-'+tab).parentNode.className = 'tab-active';
			}
			} catch (e){}
		}, this);
		if (tab == 'ENQUIRY'){
			$('info-ENQUIRY').setStyle('display','block');
		} else {
			$('info-ENQUIRY').setStyle('display','none');
		}
		return false;
	}
}
var TableRollover = {
		
	init : function(groupSite){
		
		try {
			$$('tr a').each(function(e){
				e.onclick = function(){return false};
			});
			
			$$('tr').each(function(tr){
				var td = tr.getFirst();
				if (td.getTag() == 'td'){
					var anc = td.getFirst();
					var type = anc.getText();
										
					if (anc.href != ''){
						tr.addEvents({
							'click' : function(){
								loadUrl(anc.href);								
							},
							'mouseenter' : function(){
								this.className = 'over';
								this.setStyle('cursor','pointer');
							},
							'mouseleave' : function(){
								this.className = '';
								this.setStyle('cursor','pointer');
							}
						});
					}
				}
			});
			
		} catch (error){ }
	}
}
/** 
 * Attaches onclicks to the tr element for career listing
 */
var CareerRollover = {
	
	clicked : false,
	previousTR: false,
	makeList: false,
	
	init : function(groupSite){
		
		try {
			$$('tr').each(function(tr){
				var td = tr.getFirst();
				if (td.getTag() == 'td'){
					var nextTD = td.getNext();
					var anc = nextTD.getFirst();
					var type = anc.getText();
					var make = nextTD.className;
					
					if (anc.href != ''){
						tr.addEvents({
							'click' : function(){
								loadUrl(anc.href);
							},
							'mouseover' : function(){
								this.id = 'overCareer';
								this.setStyle('cursor','pointer');
							},
							'mouseout' : function(){
								this.id = '';
								this.setStyle('cursor','pointer');
							}
						});
					}
				}
			});
			
		} catch (error){ }
	}
}

var HeaderReplacement = {
	//List of images
	list : [],
		
	add : function(cnt, val){
		HeaderReplacement.list[cnt] = val;	
		var header = document.getElementById(val.id);	
		if (!header){
			return 0;
		}
		var bits = header.id.split('_');
		var param = HeaderReplacement.list[Number(bits[1])];
		if(!param) return 1;
		header.className = header.className+' image-heading';
		var bg = param.url;
		var bgString = "url('"+bg+"') no-repeat 0 0";
		header.style.background = "url('"+bg+"') no-repeat 0 0";
		header.style.height = param.imgHeight+'px';
		header.style.width = param.imgWidth+'px';
		header.style.textIndent = '-9000em';
		return 1;
	}
}

var MonoSlideShowBorder = {

	init: function () {
		var leftPos = 10;
		// Safari specific
		if (window.devicePixelRatio) {
			leftPos = 0;
		}
		$$('#slideshow div').each(function (elem) {
			new Element('img', {
				'src': '/css/images/monoslideshow-border.gif',
				'alt': 'img',
				'styles': {
					'display': 'block',
					'width': '257px',
					'height': '191px',
					'position': 'absolute',
					'left': '0',
					'top': '0'
				}
			}).injectInside(elem);
		});
		
	}

}

var ProductForm = {
	init: function () {
		$('content-products').getElement('form').setProperty('id', 'prodForm');
		$('prodForm').getElement('select').addEvent('change', function () {
			$('prodForm').submit();
		});
	}
}