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

	this.newsdayDomainCheck = function() {
		// Pages on the newsday-cars.optimumautos.com are sometimes
		// 	loading with the cablevision template, rather than the
		//	newsday template.
		//
		//	As a workaround we check the current page's url's domain,
		//  and if it includes "newsday" we reload the page with
		//  ?tplOverrideClientid=newsday passed in the query string
		// 	which will force adicio to serve out the page with the proper
		//  template (newsday's).

		// First look for the tplOverride parameter -- if it's there we'll
		// short circuit the process to prevent any potential loop conditions
		var overrideParam = this.qs.get('tplOverrideClientid');

		if (!overrideParam && location.host.indexOf('newsday') != -1) {
			// The CVC template was pulled into the newsday site,
			// reload w/template override
			location.href = location.protocol + "//" +
					location.host +
					location.pathname +
					// append the current query string, if there is one
					(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, "/", ".optimumhomes.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));
	}


	/* 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();