// Parameter obj
function Parameter()
{
    // variables
    var parameters 		= new Object;

	// functions
	this.addParameter 	= addParameter;
	this.getParameter 	= getParameter;
	this.getKeys 		= getKeys;

	// return the parameter for looping of the keys
    	function getKeys() {
		return parameters;
 	}

	// add the parameter
    	function addParameter(key, value) {
		if(!parameters[key])
			parameters[key] = new Array();

    	parameters[key].push(value);
 	}

	// get the parameter
	function getParameter(key, separator) {
		if(!parameters[key])
			return;
    	
		return parameters[key].join(separator);
	}
}

// Url obj
function Url()
{
	var p = new Parameter();

	// functions
	this.addParameter 			= p.addParameter;
	this.getParameter 			= p.getParameter;
	this.getKeys 			 	= p.getKeys;
	this.url 			= new String();
	this.buildUrl 		= buildUrl;
	this.buildExpandedUrl = buildExpandedUrl;
	this.setUrl 		= setUrl;
	this.getUrl 		= getUrl;
	this.buildQueryStringValuePairs = buildQueryStringValuePairs;
	this.buildExpandedQueryStringValuePairs = buildExpandedQueryStringValuePairs;
 
 	// setUrl
	function setUrl(u) {
	    this.url = u
	}


	// getUrl
	function getUrl() {
	    return this.url;
 	}

	// buildQueryStringValuePairs
	function buildQueryStringValuePairs() {
			var queryString = "";
			for ( key in this.getKeys() ) {
				if (queryString != "")
					queryString += '&'
				queryString += key +'='+ this.getParameter(key, ',');
	    	}
	    	return queryString;
	}

	// buildUrl
	function buildUrl() {
		return this.getUrl() + this.buildQueryStringValuePairs();
	}
	
	
	// buildExpandedQueryStringValuePairs
	function buildExpandedQueryStringValuePairs() {
			var queryString = "";

			for ( key in this.getKeys() ) {
				
					var item = new String(this.getParameter(key, ","));
					var iArray = item.split(",");
					
					for(i = 0; i < iArray.length; i++) {
						if (queryString != "" && iArray[i] != "" && iArray[i] != undefined)
							queryString += '&'
						if (iArray[i] != "" && iArray[i] != undefined)
							queryString += key +'='+ iArray[i];
				
					}
	    	}
	    	return queryString;
	}
	
	// buildUrl
	function buildExpandedUrl() {
		return this.getUrl() + this.buildExpandedQueryStringValuePairs();
	}

}


Ad.prototype = new Url;
function Ad()
{
	var url = new Url();
	this.addParameter 	= url.addParameter;
	this.getParameter 	= url.getParameter;
	this.getKeys 		= url.getKeys;
	this.buildUrl 		= url.buildUrl;
	this.buildExpandedUrl = url.buildExpandedUrl;
	
	var feature 		= new Parameter();
	this.useFeature 	= useFeature;
	this.getFeature 	= getFeature;
	this.debug 			= debug;
	this.write 			= write;
    this.deferrable 		= 1;

	// add the parameter
	function useFeature(key) {
	     feature.addParameter(key, "T");
 	}

	// add the parameter
	function getFeature(key) {
		   return feature.getParameter(key, ",");
 	}
 	
 	// this should be overloaded
	function debug() {
		document.write('<DIV style="BACKGROUND-COLOR:RED;COLOR:WHITE">'+ this.buildExpandedUrl() +'</DIV>');		
	}

	// this should be overloaded
	function write() {
	}
}


/* -------------------------------------------
Ad Object inherits paramter
------------------------------------------- */

DartAd.prototype = new Ad();
function DartAd()
{
	DartAd.prototype = new Ad();
	this.write = write;
	this.useFeature("site");
	this.useFeature("category");
	this.useFeature("vgncontent");
	this.useFeature("ord");
	this.useFeature("topic");
	this.useFeature("tile");
	this.useFeature("pagetype");
	this.useFeature("SECTION_ID");
	this.useFeature("SUBSECTION");
	this.useFeature("page");
	this.useFeature("uniqueid");
	this.adClass = new String("AD_CLASS");

	function write() {
		document.write('<script type="text/javascript" language="JavaScript" src="'+ this.buildExpandedUrl() +'"></script>');
	}
}

AdUrl.prototype = new Ad();
function AdUrl()
{
	AdUrl.prototype = new Ad();
	this.write = write;
	this.useFeature("site");
	this.useFeature("category");
	this.useFeature("vgncontent");
	this.useFeature("ord");
	this.useFeature("topic");
	this.useFeature("tile");
	this.useFeature("pagetype");
	this.useFeature("SECTION_ID");
	this.useFeature("SUBSECTION");
	this.useFeature("page");
	this.useFeature("uniqueid");
	this.useFeature("SearchKeywords");
	this.useFeature("SearchFilters");
	
	function write() {
	}
}


DartAdvanceAd.prototype = new DartAd();
function DartAdvanceAd()
{
	DartAdvanceAd.prototype = new DartAd();
	this.write = write;
	this.align='';
	this.frameborder = 0;
	this.height='';
	this.longdesc='';
	this.marginheight=0;
	this.marginwidth=0;
	this.name='';
	this.scrolling = 'no';
	this.width = '100%';
	this.useIframe = false
	
	function write() {
		if(this.useIframe == false) {
			this.setUrl("http://adsremote.scripps.com/js.ng/");
			document.write('<script type="text/javascript" language="JavaScript" src="'+ this.buildExpandedUrl() +'"></script>');
		} else {
			this.setUrl("http://adsremote.scripps.com/html.ng/");
			document.write('<iframe src ="'+this.buildExpandedUrl()+'" align ="'+this.align+'" frameborder ="'+this.frameborder+'" height ="'+this.height+'" longdesc ="'+this.longdesc+'" marginheight ="'+this.marginheight+'" marginwidth ="'+this.marginwidth+'" name ="'+this.name+'" scrolling ="'+this.scrolling+'" width ="'+this.width+'"        ></iframe>');
		}
	}
}

/* -------------------------------------------
AdManager
------------------------------------------- */
function AdManager()
{
	var p = new Parameter();
	this.addParameter = p.addParameter;
	this.getParameter = p.getParameter;
	this.getKeys = p.getKeys;
	this.createAd = createAd;
	this.createDeferredAd = createDeferredAd;
	this.moveAds = moveAds;
	this.ads = new Array();	
	this.defer = false;

	if(document.deferAds != null && 
	 	   document.deferAds == 1 &&
	   	   document.deferEnabled != null &&
	   	   document.deferEnabled == 1) {
	   	   this.defer = true;
	 }
		
	// add the parameter
	function createAd(ad) {

		// add the site params		
		for ( key in this.getKeys()) {
			if ( ad.getFeature(key) != undefined) {
				ad.addParameter(key, this.getParameter(key, ','));
			}
 		}
		
		if(document.debug == 1) 
 			ad.debug();
		ad.write();
	}

	// Create Deferred Ad
	function createDeferredAd(i) {
	}
	
	// Move Ads
	function moveAds() {
	}
}


// Url obj
function AdRestriction()
{
	var p = new Parameter();	
	// functions
	this.addParameter 			= p.addParameter;
	this.getParameter 			= p.getParameter;
	this.getKeys 			 	= p.getKeys;
	this.isActive = true;
	this.isIframe = false;
	
}

function AdRestrictionManager() {
	this.restriction = new Array();
	this.isActive = isActive;
	this.isIframe = isIframe;
	this.isMatch = isMatch;
	this.startMatch = startMatch;
	// is active
	function isActive(ad, mdm) {
		var value = false;
		for (var i = 0; i < this.restriction.length; i++){
			adRestriction = this.restriction[i];
			if(!adRestriction.isActive) {
				value = this.startMatch(ad, mdm, adRestriction);
			}
			if(value == true) 
				return false;
		}
		
		return true;
	}
	
	// is Iframe
	function isIframe(ad, mdm) {
		var value = false;
		for (var i = 0; i < this.restriction.length; i++){
			adRestriction = this.restriction[i];
			if(adRestriction.isIframe) {
				value = this.startMatch(ad, mdm, adRestriction);
			} 
			
		}
		return value;
		
	}
	
	function startMatch(ad, mdm, adRestriction) {
		var match = true;
		for ( var key in adRestriction.getKeys() ) {
					var restrictions = adRestriction.getParameter(key, ',');
					
					// get it from the mdm
					var value = mdm.getParameter(key, '----');
					match = this.isMatch(value, restrictions);
					
					// ad
					if(!match) {
						value = ad.getParameter(key, '----');
						match = this.isMatch(value, restrictions);
					}
					if(!match) return false;
				
		}
		return match;
	}
	
	function isMatch(value, restrictions) {
		var match = false;
		if(value) {
			splitValue = value.split('----');
			for(var x = 0; x < splitValue.length; x++) {
				if(restrictions == splitValue[x]) match = true;
				for(var a; a < restrictions.length; a++) {	
					if(splitValue[x] == restrictions[a]) {
						return match = true;
					}
				}
			}
		}
		return match;
	}
	
	
}
