//Hover function ie
var siteBaseUrl = '/';
var onloadEvents = new Array();

function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() {
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


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 setBaseUrl(baseUrl){
	siteBaseUrl = baseUrl;
}

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

var SectionImages = {
	section1 : { imageSrc: '', animate: 'none' },
	section2 : { imageSrc: '', animate: 'none' },
	section3 : { imageSrc: '', animate: 'none' },
	section4 : { imageSrc: '', animate: 'none' },
	section5 : { imageSrc: '', animate: 'none' },
	section6 : { imageSrc: '', animate: 'none' },
	section7 : { imageSrc: '', animate: 'none' },
	section8 : { imageSrc: '', animate: 'none' }
};

function LoadImage(imageSrc, section ){
	//&& SectionImages['section'+section].animate != true
	//var e= document.getElementById('test');
	
	
	if (SectionImages['section'+section].imageSrc != imageSrc ){
		SectionImages['section'+section].imageSrc = imageSrc;
		
		//e.innerHTML = imageSrc+' - '+section+' imgsrc - '+SectionImages['section'+section].animate;
		
		var mySection = $('section'+section);
		
		if (SectionImages['section'+section].animate != 'loading'){
			SectionImages['section'+section].animate = 'loading';
			var currentOpacity = mySection.getStyle('opacity');
			mySection.effect('opacity', {duration	: 300, wait: false, onComplete: NextImage.bind(mySection)}).start(currentOpacity,0.1);	
		}
	}
}


function NextImage(){
	SectionImages[this.id].animate = 'none'; 
	var preload = new Image();
	preload.onload = ShowImage.bind(this);
	preload.src = SectionImages[this.id].imageSrc;
}

function ShowImage(){
	this.setHTML('<img src="'+SectionImages[this.id].imageSrc+'" />');
	SectionImages[this.id].animate = 'none'; 
	this.effect('opacity',{duration: 200, wait: true}).start(this.getStyle('opacity'),1);
}


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 site = {	
	
	feedbackAnimation : function(element, state){
		var eList = element.split(" ");
		
		for (var y = 0; y < eList.length ; y++){
			var e = document.getElementById(eList[y]);
			if (e){
				if (state == 'processing'){
					site.displayBusySignal(e);	
				} else if (state == 'ready'){
					site.clearBusySignal(e);	
				}
			}
		}
	},displayBusySignal: function(pNode){
		
		//Generate a prefix id for our feedback animation containers.
		var idPrefix = pNode.id+'_';
		
		var containerWidth = pNode.offsetWidth;
		var containerHeight = pNode.offsetHeight;
		
		var busySignal = document.getElementById(idPrefix+'busySignal');
		var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');
		
		if (!busySignal){
			var busySignalContainer = document.createElement('div');
			busySignalContainer.id = idPrefix+'busySignalContainer';
			
			var busySignal = document.createElement('div');
			busySignal.id = idPrefix+"busySignal";
			busySignal.className = "busySignal";
			busySignal.style.paddingBottom = '10px';

			pNode.parentNode.insertBefore(busySignalContainer, pNode);
			var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');
			busySignalContainer.appendChild(busySignal);
		}
		busySignal.style.filter = 'alpha(opacity=50)';
		busySignal.style.opacity = '.5';
		busySignal.style.height = containerHeight + "px";
		busySignal.style.width = containerWidth+'px';
	},
	clearBusySignal: function (pNode){
		//Generate a prefix id for our feedback animation containers.
		var idPrefix = pNode.id+'_';
		
		var busySignalContainer = document.getElementById(idPrefix+'busySignalContainer');	
		var busySignal = document.getElementById(idPrefix+'busySignal');
		busySignal.parentNode.removeChild(busySignal);
		busySignalContainer.parentNode.removeChild(busySignalContainer);
	}
}

/* GOOGLE MAPS */


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 getScrollTop() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return scrOfY;
	}
var siteBaseUrl = '/';

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

function closePopup() {
	e = document.getElementById('map-display');
	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];
}
/**
 * Google maps function
 * @param {Object} id
 * @param {Object} dealershipId
 * @param {Object} addressType
 */
function attachOnClickMap(id, lat, lng, 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();
			
			var myDiv = document.createElement("DIV");
			myDiv.id = "map-display";
			myIframe = document.createElement('iframe');
			myIframe.src = siteBaseUrl+'map.php?lat='+lat+'&lng='+lng+'&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 = (getScrollTop() + 50)+'px';
			myDiv.style.left = (myWidth / 2) - (divWidth / 2) + "px";
			myDiv.style.backgroundColor = '#fff';
			document.body.appendChild(myDiv);
			
		}
		
		return false;
	}
}

/**
 * Basic functions to load and play the movie. 
 * Uses swfobject2, and will install flash for those running v7
 */

function initMovie() {
	window.setTimeout('TrivettVideo.prepareMovie()',200);
}

var TrivettVideo = {
	
	playingMovie 	: false,
	startTime		: 0,
	endTime			: 0,
	throughput		: false,
	
	/**
	 * Attaches the tile click to play events
	 */
	attachTileEvents : function() {
		var e = document.getElementById('hp-movie-tile');
		var hoge = document.getElementById('hog-movie-tile');
		
		var	tileClickEvent = function(movieName){
			//if the current movie playing is different then play it
			
			if (TrivettVideo.playingMovie != movieName) {
				//do we have throughput details?
				if (TrivettVideo.throughput != false) {
					TrivettVideo.playMovie(movieName);
				} else {
					TrivettVideo.prepareMovie(movieName);	
				}
			}
			return false;
		}	
		/*if(e) {
			e.onclick = function(){
				return tileClickEvent('harley');
			}
		}*/
		if(hoge){
			hoge.onclick = function(){
				return tileClickEvent('hog');
			}
		}
		
	},
	/**
	 * Prepare the movie. 
	 */
	prepareMovie: function(movieName) {
		var mymovie = movieName || 'harley';
		TrivettVideo.playingMovie = mymovie;
		
		if (TrivettVideo.throughput != false) {
			TrivettVideo.playMovie(mymovie);
			return;
		}
		
		var myDate = new Date();
		var himage = new Image();
		TrivettVideo.startTime = myDate.getTime();
		himage.src =  '/func/Harley.image/'+TrivettVideo.startTime+'.jpg';
		
		himage.onload = function(){
			var myDate = new Date();
	    	TrivettVideo.endTime = myDate.getTime();
	    	var diffTimeSeconds = (TrivettVideo.endTime - TrivettVideo.startTime)/1000;
	  		var kbits = (87137*8)/1024;
	 	 	TrivettVideo.throughput = (kbits/(diffTimeSeconds));
			TrivettVideo.throughput = Math.round(TrivettVideo.throughput * (93/100));		
			document.cookie = 'throughput='+TrivettVideo.throughput+'; path=/';
	 	 	
			if (TrivettVideo.throughput >= 175) {
	 	 		TrivettVideo.playMovie('harley');
	 	 	} else {
	 	 		TrivettVideo.playAnimation();
	 	 	}
		};
	},
	/**
	 * Play default flash animation
	 */
	playAnimation: function() {
		var flashvars = {};
		var params = {'wmode' : 'opaque'};
		
		var swfcontainer = 'video-inner';
		
		var videoOuter = document.getElementById('video');
		
		if (swfcontainer == 'video-inner') {
			var videoInner = document.getElementById('video-inner');
			
			if (!videoInner) {
				videoOuter.innerHTML = '<div id="video-inner"></div>';
			}
		}
		
		swfobject.embedSWF( "/media/swf/feature-flash-july10.swf",
							swfcontainer,
							"656",
							"305",
							"6.0.65",
							"/js/expressInstall.swf", 
							flashvars,
							params);
	},
	/**
	 * Play the movie
	 */
	playMovie : function(movieName) {
		
		TrivettVideo.playingMovie = movieName;
		
		var video = '100';
		var bufferLength = '7';
		var type = 'low bandwidth';
		var width = 243;
		var height = 156;
		var swfcontainer = 'video-inner';
		
		if (TrivettVideo.throughput > 1500) {
			video = '1500';
			type = 'highest bandwidth';
			width = 656;
			height = 305;
			bufferLength = '2';
			swfcontainer = 'video';
		} else if (TrivettVideo.throughput > 850) {
			video = '800';
			type = 'high bandwidth';
			width = 656;
			height = 305;
			swfcontainer = 'video';
			bufferLength = '3';
		} else if (TrivettVideo.throughput >= 450) {
			bufferLength = '4';
			video = '400';	
			type = 'medium bandwidth';
			width = 656;
			height = 305;
			width = 352;
			height = 216;
		}
		
		var videoOuter = document.getElementById('video');
		
		if (swfcontainer == 'video-inner') {
			var videoInner = document.getElementById('video-inner');
			
			if (!videoInner) {
				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'};
		
		swfobject.embedSWF( "/media/mediaplayer.swf", 
							swfcontainer, 
							width,
							height, 
							"8.0.0",
							"/js/expressInstall.swf", 
							flashvars, 
							params);
			
		/*
		var e = document.getElementById('movie-info');
		if (e){
			//e.innerHTML = 'Throughput = '+TrivettVideo.throughput+'. Playing movie ('+type+')';
		}
		*/
	}
	
}





