//Hover function ie

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

var onloadEvents = new Array();

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';}

//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 CalendarEvent = {
	
	init : function() {
		var y = 1;
		$$('#left-column .desc').each(function(e){
			var html = e.innerHTML+' ';
			if (html.length > 120) {
				var mytitle = e.getPrevious().getPrevious().getText();
				var introText = CalendarEvent.maxChars(html,120);
				e.innerHTML ='';
				var textContainer = new Element('span');
				
				
				var moreLink = new Element('a',{
					'events' : {
						'click' : function(){
							this.getPrevious().innerHTML = html;
							this.toggleClass('hidden');
							this.getNext().toggleClass('hidden');
						}
					}
				});
				
				var closeButton = new Element('a',{
					'events' : {
						'click' : function(){
							this.getPrevious().getPrevious().innerHTML = introText;
							this.toggleClass('hidden');
							this.getPrevious().toggleClass('hidden');
						
						}
					}
				});
	
				textContainer.innerHTML = introText;
				textContainer.injectInside(e);
				
				moreLink.innerHTML = 'more<span class="hidden"> about '+mytitle+'</span>';
				closeButton.injectInside(e).toggleClass('hidden');
				moreLink.injectInside(e);
				
				closeButton.setText('less');
				
				closeButton.injectInside(e);
			}
			
			
		});
	},
	maxChars :function(mystring, len ){ 		
		if (mystring.length > len){
 			var achar = mystring.charAt(len);
 			
			var i = len - 1;
 			while(achar != ' '){
 				achar = mystring.charAt(i);
 				i--;
 			}
 			var areturn = mystring.substr(0,i+1)+'... ';
 		} else {
 			areturn = mystring;
 		}
 		
 		return areturn;
 	}
}

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;
	}
}

/**
 * Basic functions to load and play the movie. 
 * Uses swfobject2, and will install flash for those running v7
 */
 
function goToAndPlayVideo(vidName) {
	document.location = '/videos/' + vidName + '/';
}

function initMovie(vid) {
	window.setTimeout('HVideo.prepareMovie(\'' + vid + '\')', 200);
}

var HVideo = {
	playingMovie	: false,
	startTime		: 0,
	endTime			: 0,
	throughput		: false,
	
	/**
	 * Prepare the movie. 
	 */
	prepareMovie: function(movieName) {
		var mymovie = movieName;
		HVideo.playingMovie = mymovie;
		
		if (HVideo.throughput != false) {
			HVideo.playMovie(mymovie);
			return;
		}
		
		var myDate = new Date();
		var himage = new Image();
		HVideo.startTime = myDate.getTime();
		himage.src =  '/func/Hog.image/'+HVideo.startTime+'.jpg';
		
		himage.onload = function(){
			var myDate = new Date();
	    	HVideo.endTime = myDate.getTime();
	    	var diffTimeSeconds = (HVideo.endTime - HVideo.startTime)/1000;
	  		var kbits = (87137*8)/1024;
	 	 	HVideo.throughput = (kbits/(diffTimeSeconds));
			HVideo.throughput = Math.round(HVideo.throughput * (93/100));		
			document.cookie = 'throughput='+HVideo.throughput+'; path=/';
	 	 	
 	 		HVideo.playMovie(mymovie);
		};
	},
		
	/**
	 * Play the movie
	 */
	playMovie : function(movieName) {
		
		HVideo.playingMovie = movieName;
		
		var video = '100';
		var bufferLength = '7';
		var type = 'low bandwidth';
		var width = 243;
		var height = 156;
		var swfcontainer = 'video-inner';
		
		if (HVideo.throughput > 1500) {
			video = '1500';
			type = 'highest bandwidth';
			width = 605;
			height = 305;
			bufferLength = '2';
			swfcontainer = 'video';
		} else if (HVideo.throughput > 850) {
			video = '800';
			type = 'high bandwidth';
			width = 605;
			height = 305;
			swfcontainer = 'video';
			bufferLength = '3';
		} else if (HVideo.throughput >= 450) {
			bufferLength = '4';
			video = '400';	
			type = 'medium bandwidth';
			width = 352;
			height = 216;
		}
		
		var videoOuter = document.getElementById('video');
		videoOuter.innerHTML = '<div id="video-inner"></div>';

		if (type == 'low bandwidth') {
			videoOuter.className = 'low-band';
		} else if (type == 'medium bandwidth') {
			videoOuter.className = 'medium-band';
		} else if (type == 'high bandwidth') {
			videoOuter.className = 'high-band';
		} else if (type == 'highest bandwidth') {
			videoOuter.className = 'highest-band';
		}
		
		var flashvars = {'width' : width, 'height' : height, 'screencolor' : '0x000000', 'autostart' :"true",
						 'backcolor' : '0x000000', 'frontcolor' : '0xff5500', 'lightcolor' : '0x000000',
						 'bufferlength' : bufferLength, 'file' : "/media/flv/"+movieName+"_"+video+".flv"};
		var params = {'allowfullscreen' : 'true'};
		
		
		//var flashvars = {'width' : width, 'height' : height, 'screencolor' : '0x000000', 'autostart' :"true",
		//				 'backcolor' : '0x000000', 'frontcolor' : '0xff5500', 'lightcolor' : '0x000000',
		//				 'bufferlength' : bufferLength, 'file' : "/media/flv/"+movieName+".flv",
		//				 'bwfile' : '/func/Hog.image/blah.jpg', 'bwstreams' : '100,400,800,1500', 'enablejs' : 'true'};
		
		swfobject.embedSWF( "/media/mediaplayer.swf", 
							swfcontainer, 
							width,
							height, 
							"8.0.0",
							"/js/expressInstall.swf", 
							flashvars, 
							params);
	}
}