

function ActiveSelector(relativeRoot) {

var _this = this;
var _contentDiv = "#EDN_mainContent";
var _containerDiv = "#engineDetailSelectors";
var _isIE6 = false;
var _relativeRoot = relativeRoot;
var _itemArr = new Array();

$(document).ready(function() {
	_this.setIE6();
	_this.setItemArr();
	_this.setInitialHoverPosition();
	_this.setHoverListeners();
	_this.setClickListeners();
	_this.setCufon();
});	

/**********************************************************************************************************
*	Listeners
**********************************************************************************************************/

this.setHoverListeners = function() {
	$(".engineDetailsItem").hover(function(){$(this).removeClass("hoverOff").addClass("hoverOn");},function() {$(this).removeClass("hoverOn").addClass("hoverOff");});
}

this.setClickListeners = function() {
	$(".engineDetailsItem").click(function(){
		_this.setActive($(this));
	});
}

/**********************************************************************************************************
*	Core Methods
**********************************************************************************************************/

this.setActive = function(target) {
	
	var aTag = target.children("a");
	
	/* Remove previous */
	$(".engineDetailsItem").removeClass("active");
	target.removeClass("hoverOff").removeClass("hoverOn").addClass("active");
	
	$(".engineDetailsItem").each(function(i){
		$(this).children("a").remove();
		
		if(_itemArr[i].link == aTag.attr("rel"))
		{
			/* Create entirely new element */
			var newTarget = $(document.createElement("a"));
				newTarget.html(_itemArr[i].value);
				newTarget.attr("rel", _itemArr[i].link);
				newTarget.addClass("activeItem");
				newTarget.appendTo(target);
		
			/*  Apply Cufon to this specific element */
			Cufon.replace('.activeItem',{fontfamily: 'Trade Gothic LT Std', color: '#fff'});
			
			/*  Set Main Content */
			_this.setContent(target);
			_this.setHeader(newTarget);
		}
		else
		{
			/* Create entirely new element */
			var newElement = $(document.createElement("a"));
				newElement.html(_itemArr[i].value);
				newElement.attr("rel", _itemArr[i].link);
				newElement.addClass("ednItemCufon");
				newElement.appendTo($(this));
		}
	});
	_this.setCufon();
}

this.setContent = function(target) {
	var rel = target.children("a").attr("rel");
	if(rel.indexOf("CONTROL_") != -1)
	{
		/* 	SWF object rewrites the DOM each time (rewriting _contentDiv from <div> to <object>) so
			this block of code simply undoes that (when needed) */
		if($(_contentDiv).get(0).tagName.toLowerCase() != "div")
		{
			var parent = $(_contentDiv).parent().get(0);
				$(_contentDiv).remove();
				
			var newElement = document.createElement('div');
				newElement.setAttribute('id', _contentDiv.replace("#", ""));
				
		   	parent.appendChild(newElement); 
		}
		
		/* 	Now normalized, load in content */
		var target = $(_contentDiv);
		$.ajax({
		type: "POST",
		url: _relativeRoot + rel.replace("CONTROL_", ""),
		data: "",
		success: function(val){
			$(_contentDiv).html(val);
			Cufon.replace('.ednAjaxContentCufon',{fontfamily: 'Trade Gothic LT Std'});
			$(_contentDiv).css({"opacity": 0});
			$(_contentDiv).animate({"opacity":1}, "medium");
			return false;
		}
		});					
	}
	/* 	Load SWF instead */
	else
	{
	    if($(_contentDiv).get(0).tagName.toLowerCase() != "div")
		{
			var parent = $(_contentDiv).parent().get(0);
				$(_contentDiv).remove();
				
			var newElement = document.createElement('div');
				newElement.setAttribute('id', _contentDiv.replace("#", ""));
				
		    				
			parent.appendChild(newElement); 
		}
		
		$(_contentDiv).html("<div class=\"noFlash\"><div class=\"noFlashText\">Please install the latest Flash Player to see this content.</div><div class=\"noFlashImg\"><a href=\"http://get.adobe.com/flashplayer/\" target=\"_blank\"><img src=\"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif\" alt=\"Get Adobe Flash Player\" border=\"0\" /></a></div></div>");
		
		_this.setSWF(target);
		/* Special case for trucks */
		if(rel.toLowerCase() == "trucks.swf"){ _this.setTrucksCSS();	}
		else{ _this.removeTrucksCSS(); }
	}
	
	return false;
}

this.setSWF = function(target) { 
	/* See SWFObject docs for more information */
    var rel = target.children("a").attr("rel");
    var swfLocation = _relativeRoot + "/Flash/" + rel;
    var params = { "wmode": "transparent" };
    var flashVars = { "data_api": _relativeRoot + "/EnginesAPI.aspx", "engineType": engineID, "isLatin": "true" };

    if (rel.toLowerCase() == "trucks.swf") {
        flashVars["truckFilterID"] = truckFilterID;
        $(_contentDiv).removeClass("noFlash");	
        $(_contentDiv).addClass("noFlashTrucks");	
    }
	swfobject.embedSWF(swfLocation, _contentDiv.replace("#", ""), "600", "300", "10.0.45", null, flashVars, params);

	return false;
}

/**********************************************************************************************************
*	Helper Methods
**********************************************************************************************************/

this.setItemArr = function() {
	$(".engineDetailsItem").children().each(function(){
		var item = { link: $(this).attr("rel"), value: $(this).html() }
		_itemArr.push(item);
	});
}

this.setCufon = function() {
	Cufon.replace('.ednItemCufon',{fontfamily: 'Trade Gothic LT Std', fontWeight: 'bold', color: '#676767', hover: {color : '#ffffff'}});
}

this.setInitialHoverPosition = function() {
	_this.setActive($(".engineDetailsItem:first"));
}

this.setHeader = function(target) {
	$("#EDN_header").html(target.html());
	Cufon.replace('.ednHeaderCufon',{fontfamily: 'Trade Gothic LT Std'});
}

this.setTrucksCSS = function() {
	$(_contentDiv).addClass("trucks");	
}

this.removeTrucksCSS = function() {
	$(_contentDiv).removeClass("trucks");	
}

this.setIE6 = function() {
	if($.browser.msie){
		if($.browser.version.substr(0,1) < 7){
			_isIE6 = true;
		}
		// Do this if we're in IE; regardless if its 6.
		$("#engineDetailsContent").children("h2").addClass("ie");
	}
}
 
} // Ends class



