
var LEXUS_ROOT_URL = (LEXUS_ROOT_URL)?LEXUS_ROOT_URL:"";
var LEXUS_ROOT_ASSET = (LEXUS_ROOT_ASSET)?LEXUS_ROOT_ASSET:"";

/**
 * Function is for debugging.  
 * Will iterate over all properties of the given object and alert each to the UI.
 * @param {Object} obj Object to be debugged.
 */
function whatis (obj) {
	alert(obj)
	for(i in obj) {
		alert(i);	
	}
}	
/**
 * @constructor
 * This Class provides static methods to find the relative postion based on the current browser.
 */
var Dom = function() {
	
	
};

/**
  * Static Method finds the left most X-axis coordinate of the given object.
  * @param {Object} Object or String id of the DOM desired
  * @param topNode Object or String id to declare the top node to traverse too.
  * measurment will include the topNode
  * @return int - X-axis coordinate
  * @member Dom
  */  
Dom.getLeftX = function(id, topNode) {
	var obj = $(id);
	topNode = (topNode) ? $(topNode) : topNode;
  var curleft = 0;
  
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
			if(topNode && obj == topNode)
			  break;
		}
	} else if (obj.x)
		curleft += obj.x;
	return curleft;
}
Dom.getLeftXAsPx = function(id, topNode) {
	return Dom.getLeftX(id, topNode) + "px";
}
/**
 * Static Method finds the right most X-axis coordinate of the given object.
 * @param {Object} id Object or Id of the DOM desired
 * @param topNode Object or String id to declare the top node to traverse too.
 * measurment will include the topNode
 * @return int - X-axis coordinate
 * @member Dom
 */
Dom.getRightX = function(id, topNode) {
	var obj = $(id);
  
  return Dom.getLeftX(obj, topNode) + obj.offsetWidth;
}
Dom.getRightXAsPx = function(id,topNode) {
	return Dom.getRightX(id,topNode) + "px";
}
 /**
 * Static Method finds the bottom most Y-axis coordinate of the given object.
 * @param {Object} Object or String id of the DOM desired
 * @param topNode Object or String id to declare the top node to traverse too.
 * measurment will include the topNode
 * @return int - Y-axis coordinate
 * @member Dom
 */ 
Dom.getBottomY = function(id,topNode) {
	var obj = $(id);
	  
	return Dom.getTopY(obj,topNode) + obj.offsetHeight;
}
Dom.getBottomYAsPx = function(id,topNode) {
	return Dom.getBottomY(id,topNode) + "px";
}
 /**
 * Static Method finds the top most Y-axis coordinate of the given object.
 * @param {Object} Object or String id of the DOM desired
 * @param topNode Object or String id to declare the top node to traverse too.
 * measurment will include the topNode
 * @return int - Y-axis coordinate
 * @member Dom
 */ 
Dom.getTopY = function(id,topNode) {
	var obj = $(id);
	topNode = $(topNode)
	var curtop = 0;
	var index = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			index++;
			curtop += obj.offsetTop		  
			obj = obj.offsetParent;
			if(topNode && obj == topNode)
			  break;
		}
	} else if (obj.y)
	curtop += obj.y;
	return curtop;
}
Dom.getTopYAsPx = function(id,topNode) {
	return Dom.getTopY(id,topNode) + "px";
}
/**
 * Static Method finds the Left, Right, Top, Bottom positions of a certain object
 * and returns an object with 4 properties: <br />
 * .leftX <br />
 * .rightX <br />
 * .topY <br />
 * .bottomY <br />
 * @param {Object} id Object or String id of the DOM desired
 * @param topNode Object or String id to declare the top node to traverse too.
 * measurment will include the topNode
 * @return Object - with 4 properties .leftX, .rightX, .topY, bottomY
 * @member Dom
 */
Dom.getBoundries = function(id,topNode) {

	 var boundries = {
		leftX:   Dom.getLeftX(id,topNode),
		rightX:  Dom.getRightX(id,topNode),
		topY:    Dom.getTopY(id,topNode),
		bottomY: Dom.getBottomY(id,topNode),
		width: Dom.getRightX(id,topNode) - Dom.getLeftX(id,topNode),
		height: Dom.getBottomY(id,topNode) - Dom.getTopY(id,topNode)		
	}
	return boundries;	
}
Dom.isInBounds = function(id, x, y) {
	var rectangle = Dom.getBoundries(id);
	//IE doesn't recognize a one pixel borders as part of the boundaries, but still needs to be included.
	if(document.all){
		rectangle.leftX = rectangle.leftX + 2;
		rectangle.rightX = rectangle.rightX - 2;
		rectangle.topY = rectangle.topY + 2;
		rectangle.bottomY = rectangle.bottomY - 2;
	}
	
	if(x<=rectangle.leftX || x>=rectangle.rightX)
	  return false;
	if(y<=rectangle.topY || y>=rectangle.bottomY)
	  return false;
	
	return true;
}

Dom.isInModBounds = function(id, x, y, topMod, rightMod, botMod, leftMod) {
	botMod = (botMod) ? botMod : topMod;
	leftMod = (leftMod) ? leftMod : rightMod;
	var rectangle = Dom.getBoundries(id);
	rectangle.leftX = rectangle.leftX + leftMod;
	rectangle.rightX = rectangle.rightX - rightMod;
	rectangle.topY = rectangle.topY + topMod;
	rectangle.bottomY = rectangle.bottomY - botMod;
	
	if(x<=rectangle.leftX || x>=rectangle.rightX)
	  return false;
	if(y<=rectangle.topY || y>=rectangle.bottomY)
	  return false;
	
	return true;
}

/**
 * Add some logic to an HTMLImage node to fix transparent png behavior in IE < 7
 *
 * @function
 * @param {HTMLImage} img the image node
 * @param {boolean} [force] by default, only images where the url ends in 'png' are affected
 *         by this method.  supply true to this argument to force the image to be modified.
 * @returns {HTMLImage} the image node is returned to allow for method chaining
 */
Dom.IEPNG = function(img, force){
    if(document.all && !img.src.match('shim.gif')){
        // hide the image to start
        img.style.visibility = 'hidden';
        if(!Dom.IEPNG.wait_list)
            Dom.IEPNG.wait_list = [];
        var iefix_img = function(img, imgObj){
            if((img.src.match(/\.png(\?.*)?$/i) || force) && !img.src.match('shim.gif')){
                //FOR IE, Fix png images
                img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + img.src + "\', sizingMethod='scale')";
                img.style.height = imgObj.height + 'px';
                img.style.width = imgObj.width + 'px';
               
                ///TODO: we should not be using absolute paths in base libraries.  find another solution
                img.src = LEXUS_ROOT_ASSET + '/lexus-share/images/shim.gif';
            }
        };
        var clearWaitList = function(){
            var list = Dom.IEPNG.wait_list;
            var ll = list.length;
            for(var i = 0; i < ll; ++i){
                var obj = list.shift();

                if(obj.imgObj.complete && obj.imgObj.height > 1 && obj.imgObj.width > 1){
                    iefix_img(obj.img, obj.imgObj);
                    obj.img.style.visibility = 'visible';
                }
                else{
                    list.push(obj);
                }
            }
            if(list.length < 1){
                clearInterval(Dom.IEPNG.interval);
                Dom.IEPNG.interval = null;
            }
        };
        if(img.src.match(/\.png(\?.*)?$/i) || force){
            var imgObj = new Image();
            imgObj.src = img.src;
           
            Dom.IEPNG.wait_list.push({img:img,imgObj:imgObj});
           
            if(!Dom.IEPNG.interval){
                Dom.IEPNG.interval = setInterval(clearWaitList, 10);
            }
        }
    }   
    return img;
}; 

//TODO This code has been moved into prototype and should be deleted once we are
// sure there are no other users out there on the site.
function Node() {}

Node.ELEMENT_NODE = 1; 
Node.ATTRIBUTE_NODE = 2; 
Node.TEXT_NODE = 3; 
Node.CDATA_SECTION_NODE = 4; 
Node.ENTITY_REFERENCE_NODE = 5; 
Node.ENTITY_NODE = 6; 
Node.PROCESSING_INSTRUCTION_NODE = 7; 
Node.COMMENT_NODE = 8; 
Node.DOCUMENT_NODE = 9; 
Node.DOCUMENT_TYPE_NODE = 10; 
Node.DOCUMENT_FRAGMENT_NODE = 11; 
Node.NOTATION_NODE = 12;

Node.getFirstElementNode = function(elements) {
	// Hack for Safari's issue with converting to prototype's $A() for childNodes
	var array = new Array();
	for(var i=0; i<elements.length; i++) {
		array.push(elements[i]);
	}
	elements = $A(array);
	return elements.find(function(element) {	
      if(element.nodeType == Node.ELEMENT_NODE)
  		  return element;	
    });
}
Node.getAllElementNode = function(elements) {
   	// Hack for Safari's issue with converting to prototype's $A() for childNodes
    var array = new Array();
	for(var i=0; i<elements.length; i++) {
		array.push(elements[i]);
	}
	return $A(array).inject($(), function(elementNodes, element) {	
      if(element.nodeType == Node.ELEMENT_NODE)
  	    elementNodes.push(element);
      return elementNodes;
    })
}

var isFlashHidden = false;


function hideAllFlashInSafari() {
	if (!isFlashHidden && isMacBrowser()){
		var flashObjs = $A(document.getElementsByTagName('OBJECT')).concat($A(document.getElementsByTagName('EMBED')));
		
		flashObjs.each(function(flashObj) {
			flashObj.parentNode.style.visibility   = 'hidden';	
		})
		isFlashHidden = true;		
	}
}
function showAllFlashInSafari() {
	
	if (isFlashHidden){
		var flashObjs = $A(document.getElementsByTagName('OBJECT')).concat($A(document.getElementsByTagName('EMBED')));
		
		flashObjs.each(function(flashObj) {
			flashObj.parentNode.style.visibility = 'visible';		
		})		
		isFlashHidden = false;		
	}
}
function isMacBrowser() {
	if(navigator.userAgent.toLowerCase().indexOf('mac') > -1) {
		return true;	
	}
	return false;
}
function isFirefoxBrowser(){
	var ua = navigator.userAgent.toLowerCase();
	return ( ua != null && ua.indexOf( "firefox" ) != -1 ) ? true : false;
}

var isSelectHidden = false;
var selectsFound;
function disableAllSelectElementsInIE() {
	if (!isSelectHidden && hideSelectElements()){
		
		if(!selectsFound) {
			var selectObjs = document.getElementsByTagName('SELECT');
			selectsFound = new Array();
			
			for(var i=0; i<selectObjs.length; i++) {
				selectsFound.push(selectObjs[i]);
			}
		}
		for(var i=0; i<selectsFound.length; i++) {
			selectsFound[i].style.visibility = 'hidden';
		}
		isSelectHidden = true;		
	}
}
function enableAllSelectElementsInIE() {
	
	if (isSelectHidden && selectsFound){
		for(var i=0; i<selectsFound.length; i++) {
				selectsFound[i].style.visibility = 'visible';	
		}
		isSelectHidden = false;
	}
		
}
function hideSelectElements() {
	if(document.all) {
		return true;	
	}
	return false;
}


function hideElementsForOverlays() {
	hideAllFlashInSafari();
	disableAllSelectElementsInIE();
}

function showElementsForOverlays() {
	showAllFlashInSafari();
	enableAllSelectElementsInIE();
}
function Address() {}

Address.getParameters = function() {
	var params = new Object;
	
	var search = window.location.search.replace('?','');
	
	if(search != '') {
		var items = search.split('&');
		
		for(var i=0; i<items.length; i++) {
			var keyValue = items[i].split('=');
			
			params[keyValue[0]] = keyValue[1];
		}
	}
	return params;
}

function MovieUtils() {}


MovieUtils.buildFlashMovie = function(destination, params, noPlugin) {
	 	
	 	if(Plugin.isInstalled('Flash') && Plugin.getVersion('Flash') >= 7) {
			var objectCall = 'AC_FL_RunContent(~params~);';
			var objectParams = '';
			
			for(param in params) {
				objectParams += "'" + param + "',";
				objectParams += "'" + params[param] + "',"
			}
			objectParams += "'" + destination + "'";
			objectCall = objectCall.replace('~params~', objectParams);
			
			eval(objectCall);
	 	
	 	}
	 	else if(noPlugin){
	 		MovieUtils.buildNoPlugin(destination,'flash', params['width'], params['height']);
	 		return;
	 	}
	 	
}
 
MovieUtils.buildViewPointMovie = function(destination,params,noPlugin) {
 	
 	if(Plugin.isInstalled('MetaStream')) {
	 	var vmp = new MTSPlugin(params.movie, params.width*1, params.height*1, params.broadcast, params.alt, params.contentType);
		$(destination).innerHTML = vmp.OutputStream();
		
		return vmp;
 	}
 	else if(noPlugin){
 		MovieUtils.buildNoPlugin(destination,'viewpoint', params['width'], params['height']);
 	}
}
MovieUtils.buildQuickTimeMovie = function(destination,params,noPlugin) {
 	
	if(Plugin.isInstalled('QuickTime') && Plugin.getVersionAsFloat('QuickTime') >= 5) {
	    //** Show QTVR **
		var newAnimation = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH=' + params.width*1 + ' HEIGHT=' + params.height*1 + ' CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">\n';
		newAnimation = newAnimation + '<PARAM name="SRC" VALUE="' + params.movie + '">\n';
		newAnimation = newAnimation + '<PARAM name="AUTOPLAY" VALUE="true">\n';
		newAnimation = newAnimation + '<PARAM name="CONTROLLER" VALUE="false">\n';
		newAnimation = newAnimation + '<EMBED SRC="' + params.movie + '" WIDTH=' + params.width*1 + ' HEIGHT=' + params.height*1 + ' AUTOPLAY="true" CONTROLLER="false" PLUGINSPAGE="http://www.apple.com/quicktime/download/">\n';
		newAnimation = newAnimation + '</EMBED>\n';
		newAnimation = newAnimation + '</OBJECT>\n';
		
		$(destination).innerHTML = newAnimation;
		return new Object();
   	}
   	else if(noPlugin) {
   		MovieUtils.buildNoPlugin('spinsGalleryViewer', 'quicktime');
   	}
 	
}
MovieUtils.buildMediaPlayerMovie = function(destination,params, noPlugin) {
	
	if(Plugin.isInstalled('Windows Media')) {
		
		var embed = '<object id="MediaPlayer" width=~width~ height=~height~ assid="CLSID:226BF52A52-394A-11D3-B153-00C04F79FAA6 standby="Loading Windows Media Player components..." type="application/x-oleobject">'
				  +  '<param name="url" value="~movie~">'
				  +  '<param name="showcontrols" value="true">'
				  +  '<param name="autoStart" value="true">'
				  +  '<PARAM NAME="uiMode" VALUE="mini">'
				  +  '<embed type="application/x-mplayer2" src="~movie~" name="MediaPlayer" width=~width~ height=~height~></embed>'
				  +  '</object>';
				  
		embed = embed.replace(/~movie~/g, params['movie']);
		embed = embed.replace(/~width~/g, params['width']);
		embed = embed.replace(/~height~/g, params['height']);
		
		$(destination).innerHTML = embed;
	}
	else if(noPlugin){
		MovieUtils.buildNoPlugin(destination,'mediaplayer', params['width'], params['height']);
	}	
	
}

MovieUtils.buildNoPlugin = function(destination, type,width, height) {
	
	var div = '<DIV style="~style~"><DIV>~content~</DIV></DIV>';
	var content = '';
	var style = 'padding-left:5px;padding-right:5px;';
	if(width) {
		style += 'width:'+ (width - 10) + 'px;'
	}
	if(height) {
		style += 'height:'+ (height - 10) + 'px;'
	}
	switch (type) {
		case 'flash':
			content = '<a href="' + MovieUtils.getPluginDownloadLink(type) + '" target="_blank">Download Plug-in</a>'
			break;
		case 'quicktime':			
			content = '<A href="' + MovieUtils.getPluginDownloadLink(type) + '" target="_blank">Download Plug-in</a>'
			break;
		case 'viewpoint':
			content = '<A href="' + MovieUtils.getPluginDownloadLink(type) + '" target="_blank" ></a>'
			break;
		case 'mediaplayer':
			content = '<A href="' + MovieUtils.getPluginDownloadLink(type) + '" target="_blank">Download Plug-in</a>'
			break;	
		default:
			break;
	}
	div = div.replace('~style~', style);
	div = div.replace('~content~', content);
	
	if(destination) {
		$(destination).innerHTML = div;
	}
	else {
		document.write(div);
	}
	
}
MovieUtils.getPluginDownloadLink = function(type) {
	
	switch (type) {
		case 'flash':
			return 'http://get.adobe.com/flashplayer/';
			break;

		case 'quicktime':
			var url = 'http://www.apple.com/quicktime/download/';
			var version = 0;
			if(navigator.appVersion && navigator.appVersion.indexOf('MSIE') > -1) {
				var arVersion = navigator.appVersion.split("MSIE")
				var version = parseFloat(arVersion[1])
				
				if(version <= 5.5) {
					url =  'http://www.apple.com/support/downloads/quicktime652forwindows.html';	
				}
			}
			return url;
			break;

		case 'viewpoint':
			return 'http://www.viewpoint.com/cgi-bin/redirector.pl';
			break;

		case 'mediaplayer':
			return 'http://www.microsoft.com/windows/windowsmedia/player/download/download.aspx'
			break;

		default:
			break;
	}

}
function pluginPopup(url) {
	
}
function hasObject(parent, objectStr) {
	
	if(parent[objectStr])
		return true;
	else
		return false;
}
function ajaxUtils() {

}
ajaxUtils.buildAjaxUrl = function(urlPieces) {
	urlPieces.push('?random=' + Math.random());
	return urlPieces.join('');
}