function pcw_AdInit () {
	//set global defaults
	pcw_ad_site = "pcw.general";
	pcw_ad_zone = "";
	pcw_ad_pos = "";
	pcw_ad_channel = "";
	pcw_ad_section = "";
	pcw_ad_pcat = "";
	pcw_ad_cats = "";
	pcw_ad_sz = "";
	pcw_ad_tile = "";
	pcw_ad_tags = "";
	pcw_ad_ord = "";
	pcw_ad_custom = "";
	ad_debug = "";

	//set 8-digit random number
	for(var o=0;o < 7;o++) {
		pcw_ad_ord = pcw_ad_ord + Math.floor((Math.random()*10));
	}
}

//overrides to be called on the page *after* the server-side options have been set
function pcw_AdOverride () {

	//what are some cookies that I have set
	//what is my referrer? do something with it
	
}

/*
unit: specifies named unit (banner|tower|showcase|halfpage|button)
type: specifies type of tag (iframe|js)
*/

function pcw_AdRender(adtype, adunit,tile) {

	var pcw_ad_width;
	var pcw_ad_height;
	
	switch (adunit) {
		case "leader":
			pcw_ad_width="728";
			pcw_ad_height="90";
			pcw_ad_tile="1";
			pcw_ad_custom="dcopt=ist;";
			break;
		case "showcase-lg":
			pcw_ad_width="336";
			pcw_ad_height="280";
			pcw_ad_tile="2";
			pcw_ad_custom=""
			break;
		case "tower-lg":
			pcw_ad_width="160";
			pcw_ad_height="600";
			pcw_ad_tile="3";
			pcw_ad_custom=""
			break;
		case "halfpage":
			pcw_ad_width="336";
			pcw_ad_height="850";
			pcw_ad_tile="4";
			pcw_ad_custom=""
			break;
		case "textbox":
			pcw_ad_width="160";
			pcw_ad_height="42";
			pcw_ad_tile="5";
			pcw_ad_custom=""
			break;
			//textbox2 = textbox + tile 6
		case "button":
			pcw_ad_width="160";
			pcw_ad_height="60";
			pcw_ad_tile="7";
			pcw_ad_custom=""
			break;
		case "custom1":
			pcw_ad_width="408";
			pcw_ad_height="87";
			pcw_ad_tile="8";
			pcw_ad_custom=""
			break;
		case "button-sm":
			pcw_ad_width="120";
			pcw_ad_height="30";
			pcw_ad_tile="10";
			pcw_ad_custom=""
			break;
		case "interstitial":
			pcw_ad_width="1";
			pcw_ad_height="1";
			pcw_ad_tile="13";
			pcw_ad_custom=""
			break;
			//button-sm 2 = buttom-sm + tile 11
			//button-sm 2 = buttom-sm + tile 12
	}

	//override with incoming tile value
	if (tile) {
		pcw_ad_tile = tile;
	}

	pcw_ad_pos=adunit;
	pcw_ad_sz=pcw_ad_width+"x"+pcw_ad_height;

	var ad;

	//build the ad
	if (ad_debug) {
		ad = '<div style="width:'+pcw_ad_width+'px;height:'+pcw_ad_height+'px;background-color:#606;color:#ff0">';
		ad = ad + pcw_AdDartUrl("adi");
		ad = ad + '</div>';
	} else {
		switch (adtype) {
			case "script":
				ad = "document.write('<scr'+'ipt type=\"text/javascript\" ');";
				ad += "document.write('src=\""+pcw_AdDartUrl("script")+"\">');";
				ad += "document.write('</scr'+'ipt>');";
				break;
			case "iframe":
				ad = '<iframe src="' + pcw_AdDartUrl("adi") + '" width="' + pcw_ad_width + '" height="' + pcw_ad_height + '" marginheight="0" marginwidth="0" scrolling="no" frameborder="0">';
				ad = ad + '<a href="' + pcw_AdDartUrl("jump") + '" target="_top">';
				ad = ad + '<img width="' + pcw_ad_width + '" height="' + pcw_ad_height + '" src="' + pcw_AdDartUrl("image") + '" alt="Advertisement" /></a>';
				ad = ad + '</iframe>';
				break;
		}
	}

	return ad;

}

/*
unit: specifies named unit (banner|tower|showcase|halfpage|button)
type: specifies type of tag (iframe|js)
*/

function pcw_AdDartUrl(type) {

	//what kind uf url to build?
	switch (type) {
		case "iframe":
			type="adi";
			break;
		case "click":
			type="jump";
			break;
		case "image":
			type="ad";
			break;
		case "script":
			type="adj";
			pcw_ad_sz = "";
			break;
	}
	
	//base url
	url = 'http://ad.doubleclick.net/'+type+'/'+pcw_ad_site+'/'+pcw_ad_zone+";";
	
	//content attributes
	url = url + pcw_AdKey("ch",pcw_ad_channel) + pcw_AdKey("sec",pcw_ad_section) + pcw_AdKey("pc",pcw_ad_pcat) + pcw_AdCats(pcw_ad_cats)
	
	//size attributes
	url = url + pcw_AdKey("pos",pcw_ad_pos) + pcw_AdKey("tile",pcw_ad_tile) + pcw_AdKey("sz",pcw_ad_sz);

	//custom attributes
	url = url + pcw_ad_custom

	//cache breaker	
	url = url + pcw_AdKey("ord",pcw_ad_ord);
	
	return url;

}

/*
constructs the key tags in their proper form
*/

function pcw_AdKey (key, val) {
	var strOut = "";
	if (ad_debug) {strOut += " "}
	if (val) {
		strOut+=key+"="+val+";";
	}
	
	return strOut;
}

/*
splits cat values and generates their key tags
*/

function pcw_AdCats (sVal) {
	var strOut = "";
	if (sVal) {
		var aVal = new Array();
		aVal = sVal.split(",");
		var ctr = 0;
		for (var i=0;i<aVal.length;i++) {
			if (aVal[i] == pcw_ad_pcat) { 
				continue;
			} else {
				if (ctr < 5) {
					strOut += pcw_AdKey("c",aVal[i]);
					ctr++;
				} else {
					break;
				}
			}
		}
	}
	return strOut;
}

