/**
 * OptAutos - global namespace for OptimumAutos functions.
 */
var OptAutos = new function() {

	this.newsdayDomainCheck = function() {
		// hitting newsday-cars.optimumautos.com sometimes loads
		// 	the cablevision template rather than the newsday template
		//	as a fix we check the domain here -- if it is newsday we 
		//  reload the page with ?tplOverrideClientid=newsday passed
		//  which adicio will see and force the inclusion of the 
		//  newsday template.
		
		// look for the tplOverride parameter, if it's there short circuit
		// the process to prevent any potential loop conditions
		var overrideParam = this.qs.get('tplOverrideClientid');
		
		if (!overrideParam && location.host.indexOf('dev') != -1) {
			// we were pulled into the newsday site ... reload w/override!
			location.href = location.protocol + "//" + 
					location.host +
					location.pathname +
					(location.search? location.search + "&tplOverrideClientid=newsday" : "?tplOverrideClientid=newsday");
		}
	}

	this.initN12Detection = function() {
		var n12Str = "news12.com";
		var n12Ref = document.referrer;
		if ((n12Ref && (n12Ref != null) && (n12Ref.indexOf(n12Str) != -1)) 
			|| (document.location.search && document.location.search.indexOf("enableN12") != -1)) {
			var fromN12Val = "true|LI"
			var n12RegionsAllowed = ["LI", "NJ", "WC", "CT", "BX", "BK", "HV"];
			var n12End = n12Ref.indexOf(n12Str) + n12Str.length;
			var n12Domain = n12Ref.substring(0,n12End);
			
			if ((n12Domain.length + 3) >= n12Ref.length) {
				var n12Region = n12Ref.substring(n12End+1, n12Domain.length+3);

				for (i=0; i<n12RegionsAllowed.length; i++) {
					if (n12Region == n12RegionsAllowed[i]) {
						fromN12Val = "true|" + n12RegionsAllowed[i];
						break;
					}
				}
			}
			
			this.setCookie2('oa_from_n12', fromN12Val, null, "/", ".optimumautos.com", false);
			return true;
		}
		
		return false;
	}
	
	this.isFromN12 = function() {
		return ((this.getN12Cookie("oa_from_n12").split("|")[0] == "true"));
	}
	
	this.getN12Cookie = function(name) { 
		var c = ""; 
		if(document.cookie.length > 0){ 
			var pref = "; "; 
			var c2 = pref + document.cookie + ";"; 
			var beg = c2.indexOf(pref + name + "="); 
			if (beg != -1) { 
				beg = beg + pref.length + name.length + 1; 
				var end = c2.indexOf(";", beg); 
				if(end != -1){ 
					c = unescape(c2.substring(beg, end)); 
				} 
			} 
		} 
		return c;
	}
	
	this.writeN12Ad = function() {
		// document.write('<td valign="middle" align="right">Sponsored<br> by:</td><td align="right" style="padding-right:35px; width:98px;"><script type="text/javascript">OptAutos.writeN12AdScript("AutosSponsor",1,88,31);<\/script></td>');
	}
	
	this.writeN12Header = function() {
		document.write('<div style="margin-left: -13px;"><IFRAME SRC="http://www.news12.com/includes/home_header.jsp?optimum=1" WIDTH="998" HEIGHT="130" FRAMEBORDER="0" scrolling="No" style="margin:0px; margin-bottom:10px;"></IFRAME></div>');
	}
	
	this.writeN12AdScript = function(channel,tile,x,y) {
		var lbound = 1000;
		var ubound = 9999;
		var rnd = Math.floor(Math.random() * (ubound - lbound)) + lbound;
		var size = x + 'x' + y;
		var url = 'http://ad.doubleclick.net/adi/cablevision.news12/' + channel + ';sz=' + size + ';tile=' + tile + ';ord=' + rnd;
		contents =  '<ifr' + 'ame src=' + url + '? name=frame1 width=' + x + ' height=' + y + ' frameborder=no border=0 marginwidth=0 marginheight=0 scrolling=no>';
		contents += '<scr' + 'ipt language=JavaScript1.1 src=' + url + '></scr' + 'ipt>';
		contents += '<nosc'+'ript>';
		contents += '<a href=' + url + '? target=\"_blank\"><img src=' + url + '? border=0 height=' + y + ' width=' + x + ' vspace=0 hspace=0></a>';
		contents += '</nosc'+'ript>';
		contents += '</ifr'+'ame>';
		document.write(contents);
	}
  
  	/*
	   name - name of the cookie
	   value - value of the cookie
	   [expires] - expiration date of the cookie
	     (defaults to end of current session)
	   [path] - path for which the cookie is valid
	     (defaults to path of calling document)
	   [domain] - domain for which the cookie is valid
	     (defaults to domain of calling document)
	   [secure] - Boolean value indicating if the cookie transmission requires
	     a secure transmission
	   * an argument defaults when it is assigned null as a placeholder
	   * a null placeholder is not required for trailing omitted arguments
	*/	
	this.setCookie2 = function(name, value, expires, path, domain, secure) {
		var curCookie = name + "=" + escape(value) +
	    	((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	}
	
	
	/*
	  name - name of the desired cookie
	  return string containing value of specified cookie or null
	  if cookie does not exist
	*/
	this.getCookie2 = function(name) {
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}
  

	this.initHeaderTab = function() {
		var pageName = document.location.pathname;
		var tabs = [
			["BtnHomePage", ["autos/home.shtml"]],
			["BtnFindACar", ["autos/findacar.shtml", "/autos/search/results.php", "/autos/search/detail.php"]],
			["BtnResearch", ["autos/research.shtml", "/autos/comparator/controllers/featuresController.php", "/autos/researchCenter/"]],
			["BtnVideos", ["autos/videos.shtml", "/autos/research/reviews/video/"]],
			["BtnFindADealer", ["autos/dealer.shtml", "/autos/dealers/contactDealerships.php", "/autos/dealers/"]],
			["BtnSellMyCar", ["autos/sellmycar.shtml", "/autos/ads/", "/autos/ads/inputAd.php"]],
			["BtnMemberCenter", ["autos/login.shtml", "/autos/login.php", "/autos/mymotors/"]]
		];
		
		var selectedTab = null;
		
		for (var i = 0; i < tabs.length; i++) {
			var tabName = tabs[i][0];
			var tabPages = tabs[i][1];
			
			for (var j = 0; j < tabPages.length; j++) {
				if (pageName.indexOf(tabPages[j]) != -1) {
					selectedTab = tabName;
				}
			}
		}
		
		if (selectedTab && document.getElementById(selectedTab)) {
			document.getElementById(selectedTab).className += " selected";
		}
	} 
	
	/* Client-side access to querystring name=value pairs
		Version 1.3
		28 May 2008
		
		License (Simplified BSD):
		http://adamv.com/dev/javascript/qslicense.txt
	*/
	this.Querystring = function(qs) { // optionally pass a querystring to parse
		this.params = {};
		
		if (qs == null) qs = location.search.substring(1, location.search.length);
		if (qs.length == 0) return;
	
		// Turn <plus> back to <space>
		// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
		qs = qs.replace(/\+/g, ' ');
		var args = qs.split('&'); // parse out name/value pairs separated via &
		
		// split out each name=value pair
		for (var i = 0; i < args.length; i++) {
			var pair = args[i].split('=');
			var name = decodeURIComponent(pair[0]);
			
			var value = (pair.length==2)
				? decodeURIComponent(pair[1])
				: name;
			
			this.params[name] = value;
		}
	}
	
	this.Querystring.prototype.get = function(key, default_) {
		var value = this.params[key];
		return (value != null) ? value : default_;
	}
	
	this.Querystring.prototype.contains = function(key) {
		var value = this.params[key];
		return (value != null);
	}
	
	this.qs = new this.Querystring();
};

OptAutos.initN12Detection();