/*
<!-- 

Author: Robert Birkline

http://www.chron.com/privacy.html

Copyright Notice:
All content of HoustonChronicle.com, including but not limited to, all text, photos, graphics, audio, software, and/or video is copyrighted by Houston Chronicle Publishing Company Division (\u201cThe Chronicle\u201d), Hearst Newspapers Partnership, L.P (\u201cHearst\u201d), or its suppliers, news and feature syndicates and wire services. ?? 1985 - 2002 Hearst Newspapers Partnership, L.P. All rights reserved.

No portion of the content may be directly or indirectly copied, published, reproduced, modified, performed, displayed, sold, transmitted, published, broadcast, rewritten for broadcast or publication or redistributed in any medium. Nor may any portion of the content be stored in a computer or distributed over any network except that you may download or print one copy of occasional articles strictly for personal and non-commercial use 

Notice of Copyright Infringement:
HoustonChronicle.com respects the copyrights of others. If you believe that your copyrighted work has been reproduced on our site in a way that constitutes copyright infringement you may notify our designated copyright agent, Sherry Adams, Library Manager, 801 Texas, Houston, Texas 77002, 713 -220-7312, or by e-mail at sherry.adams@chron.com. 
 
  -->
*/
var adwiz_errors = ''; //this is a container to hold errors

function CHRON_get_param(){
	var CHRON_gp = window.location.href;
	var CHRON_gp_list = CHRON_gp.split(/\?/);
	if (CHRON_gp_list.length > 1){
		CHRON_gp_list[1] = CHRON_gp_list[1].replace(/\&/g,',');
		CHRON_gp_list[1] = CHRON_gp_list[1].replace(/\=/g,'~');
		return CHRON_gp_list[1];
	}
	return '';	
}

function CHRON_get_param_array(){
	var CHRON_gpa = window.location.href;
	var CHRON_gpa_list = CHRON_gpa.split(/\?/);
	var CHRON_gpa_list_final = Array();
	if (CHRON_gpa_list[1]){
		var CHRON_gpa_list_sp = CHRON_gpa_list[1].split(/\&/);
		for (i=0; i < CHRON_gpa_list_sp.length; i++){
			try{
			CHRON_tmp = CHRON_gpa_list_sp[i].split(/=/);
			CHRON_gpa_list_final[CHRON_tmp[0]] = CHRON_tmp[1];
			}catch(e){}
		} 
	}
	return CHRON_gpa_list_final;
}

function CHRON_tag_normal(x){

	x = x.replace(/\s+/g,'+');
	return x;
}

function CHRON_reset_query_string(){
	CHRON_query ='';
	return true;
}


function CHRON_add_to_query_string(x){
	CHRON_query += x+'&';
	return true;
}

function CHRON_get_adwiz_tags(){
        //get the tags information
	
        var CHRON_gat = document.getElementsByTagName('adwiz');
        var CHRON_gat_length = CHRON_gat.length;
        if (!CHRON_query){CHRON_query='';}
        if (!CHRON_listpos){CHRON_listpos='';}
	//set the sitepage array up, this allows ordering of content.
	var CHRON_sitepage_array = Array();
	CHRON_sitepage?CHRON_sitepage_array[0]=CHRON_sitepage:CHRON_sitepage_array[0]='';
	CHRON_sitepage='';
        for (i=0; i < CHRON_gat_length; i++){
		var tag_name = CHRON_gat[i].getAttribute('name');
		var tag_type = CHRON_gat[i].getAttribute('type');
		var tag_value = CHRON_gat[i].getAttribute('value');
		var tag_order = CHRON_gat[i].getAttribute('order');

                switch (tag_type){
                        case 'keyname':
                                if ( tag_name && tag_value ){
					 CHRON_adwiz_tag.keyname[tag_name] = tag_value;
                                         CHRON_add_to_query_string(tag_name + '=' + tag_value);
                                }
                                break;
                        case 'keyword':
                                if ( tag_value ){
					CHRON_adwiz_tag.keyword.push(tag_value);
                                        CHRON_add_to_query_string(tag_value);
                                }
                                break;
                        case 'rkeyname':
                                if ( tag_name && tag_value ){
					CHRON_adwiz_tag.keyname[tag_value] = tag_name;
                                        CHRON_add_to_query_string( tag_value + '=' + tag_name );
                                }
                                break;
                        case 'sitepage':
                                if ( tag_value && tag_order ){
					CHRON_adwiz_tag.sitepage = tag_value;
                                        CHRON_sitepage_array[tag_order] = tag_value;
                                }
                                break;
                        case 'listpos':
                                if (tag_value){
					CHRON_adwiz_tag.listpos = tag_value;
                                        CHRON_listpos = tag_value;
                                }
                                break;
                        default:
                                break;
                }
        }
        //CHRON_query == clean up the query tag
        CHRON_query = CHRON_query.replace(/\&$/,'');
	CHRON_query = CHRON_tag_normal(CHRON_query);
	//CHRON_sitepage == set order to sitepage variable
	for (i = 0; i < CHRON_sitepage_array.length; i++){
		try {
			if (CHRON_sitepage_array[i]){
				CHRON_sitepage += CHRON_sitepage_array[i] + '/';
			}
			
		}catch(e){}	
	}
	CHRON_sitepage = CHRON_sitepage.replace(/\/$/,'');
	CHRON_sitepage = CHRON_tag_normal(CHRON_sitepage);
}

function CHRON_set_type_adwiz_tags(xvalue,xname){
//get the tags information
//xname is an option value and used to set tags with specific name
var tagname;
xname ? tagname = xname : tagname = '';
        var CHRON_stat = document.getElementsByTagName('adwiz');
        var CHRON_stat_length = CHRON_stat.length;
	if ( tagname == '' ){
	//set all the tags the same
		for (i=0; i < CHRON_stat_length; i++){
				CHRON_stat[i].setAttribute('type', xvalue);
		}
	}else{
		for (i=0; i < CHRON_stat_length; i++){
			if ( CHRON_stat[i].getAttribute('name') == tagname){
				CHRON_stat[i].setAttribute('type', xvalue);
			}
		}
		
	}	
}

function CHRON_adwiz_tag_name_change(tag2change, newname){
//find the tag and change the name of the tag
        var CHRON_stat = document.getElementsByTagName('adwiz');
        var CHRON_stat_length = CHRON_stat.length;
	for (i=0; i < CHRON_stat_length; i++){
		if (tag2change == CHRON_stat[i].getAttribute('name') ){
			CHRON_stat[i].setAttribute('name', newname);
		}
	}
}

function CHRON_clone_adwiz_tags(tagtype, tagname){
try{
//get the tags information
//xname is an option value and used to set tags with specific name
var xname;
var adwiz_errors;
tagname ? xname = tagname : xname = '';
        var CHRON_clone = document.getElementsByTagName('adwiz');
        var CHRON_clone_length = CHRON_clone.length;
	var CHRON_max =1000;

	if ( xname == '' ){
	//clone set all the tags the same
		for (i=0; (i < CHRON_clone_length || i > CHRON_max) ; i++){
			try {
				var CHRON_cloned_node = CHRON_clone[i].cloneNode(true);
				CHRON_cloned_node.setAttribute('type',tagtype);
				CHRON_clone[i].parentNode.appendChild(CHRON_cloned_node);
				delete(CHRON_cloned_node);
			}catch(e){
				adwiz_errors += 'CHRON_clone_adwiz_tags --> '+e+'\n';
			}
				
		}
	}else{
		for (i=0; i < CHRON_clone_length; i++){
			if ( CHRON_clone[i].getAttribute('name') == tagname){
				var CHRON_cloned_node = CHRON_clone[i].cloneNode(true);
				CHRON_cloned_node.setAttribute('type',tagtype);			
				CHRON_clone[i].parentNode.appendChild(CHRON_cloned_node);
				delete(CHRON_cloned_node);
			}
		}
		
	} 
	delete(CHRON_clone); delete(CHRON_clone_length); // clean up a little
}catch(e){}
}

function CHRON_make_adwiz_tags_readable(){
//go thru the tags and set the name or value to something readable
	var CHRON_stat = document.getElementsByTagName('adwiz');
        var CHRON_stat_length = CHRON_stat.length;
        for (i=0; i < CHRON_stat_length; i++){
		try {
			CHRON_tmp = CHRON_stat[i].getAttribute('name');
    			CHRON_stat[i].setAttribute('name', CHRON_adwiz_lookup[CHRON_tmp].name);
		}catch(e){}
		try{
			CHRON_tmp = CHRON_stat[i].getAttribute('value');
    			CHRON_stat[i].setAttribute('value', CHRON_adwiz_lookup[CHRON_tmp].name);
		}catch(e){}
        }
}

function CHRON_order_sitepage_append(liststring){
	var CHRON_osa = CHRON_sitepage_append.split(/&/);
	for (i=0; i < CHRON_osa.length; i++){
		var CHRON_osa_tmp = CHRON_osa[i].split(/=/);
		try{
			regex = CHRON_osa_tmp[0];
			liststring = liststring.replace(regex, CHRON_osa_tmp[1] ) ;
		}catch(e){}
	}
	//finally set the sitepage_append
	CHRON_sitepage_append = liststring;
}


function CHRON_adCode(x){
	return document.getElementById('CHRON_ADPLACEMENT_'+x).innerHTML;
}

function CHRON_MJX_TAG(){
	CHRON_adwiz_tag.MJX = document.getElementById('OAS_MJX_CALL');
}

function CHRON_adwiz_add_history(x){
	//ad history information to the call
	try{
	var chron_history_url = document.referrer;
	chron_history_url = chron_history_url.replace(/http:\/\//,'');
	var chron_history = chron_history_url.split(/\//);
	CHRON_adwiz_tag.referrer=chron_history_url;
	CHRON_adwiz_tag.referrer_domain=chron_history[0];
	CHRON_query += 'referrer_domain='+ chron_history[0] +'&referrer='+ chron_history_url +'&';
	}catch(e){adwiz_errors += 'history error\n'; }
}


/* ADWIZ SERVING SECIONT */
function CHRON_serve_adwiz(){
	// adWiz call to adwiz.mpl
	try{
	var CHRON_adwiz_url = CHRON_adwiz_src + '?url=' + CHRON_page + '&param='+ CHRON_param;
	document.write('<script id="ADWIZ_TAG" language="javascript" src="' + CHRON_adwiz_url +'"></script>');
	}catch(e){adwiz_errors += 'adwiz serve error\n';}
	CHRON_adwiz_tag.adwiz = document.getElementById('ADWIZ_TAG');

}

function CHRON_serve_reporter(sitepage, listpos, chron_url){
	// adWiz call to error reporting script
	try{
	document.write('<script id="CHRON_REPORTER" language="javascript" src="http://www.chron.com/apps/adWiz/adWizRecorder.mpl?sitepage=' + sitepage + '&listpos='+ listpos + '&url=' + chron_url +'"></script>');
	}catch(e){adwiz_errors += 'reporter error\n';}
}

function CHRON_serve_OAS_MJX(sitepage, listpos, query){
	// adWiz call to Call the MJX code
	try{
	var rn = CHRON_random_number();
  	var OAS_MJX_CALL = OAS_url + 'adstream_mjx.ads/' + sitepage + '/1' + rn + '@' + listpos + '?' + query;
  	document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.1" ID="OAS_MJX_CALL" SRC="' + OAS_MJX_CALL + '"><\/SCRIPT>');
	}catch(e){adwiz_errors += 'MJX error\n';}
	CHRON_adwiz_tag.MJX = document.getElementById('OAS_MJX_CALL');
}

function CHRON_serve_OAS_NX(sitepage, listpos, pos, query){
	//adWiz call to Call the OAS normal function for nx tags
	try{
	var OAS_target = '_top';
     document.write('<A HREF="' + OAS_url + 'click_nx.ads/' + sitepage + '/1' + rn + '@' + listpos + '!' + pos + '?' + query + '" TARGET=' + OAS_target + '>');
     document.write('<IMG SRC="' + url + 'adstream_nx.ads/' + sitepage + '/1' + rn + '@' + listpos + '!' + pos + '?' + query + '" BORDER=0></A>');
	}catch(e){adwiz_errors += 'NX error\n';}
}

/*  END ADWIZ SERVING SECTION */

function CHRON_random_number(){
	var rns = '1234567890';
	var rn = new String (Math.random()); 
	return (rn.substring (2, 11));
}


function detectAdwizSubDomain(){
	//this function will look at the detect the chron subdomain used
	var CHRON_detecthostname = window.location.hostname.split(/\./);
	var CHRON_subdomain = CHRON_detecthostname[0];
	var CHRON_domain = CHRON_detecthostname[1];
	var CHRON_toplevel = CHRON_detecthostname[2];

	if (CHRON_domain == 'chron'){
		switch (CHRON_subdomain){
		case 'www':
		case 'images':
			return 'www.chron.com'; 
			break;
		case 'apps':
		case 'testakamai':
		case 'origin':
		case 'origin-www':
			return CHRON_subdomain + '.chron.com';
			break;
		default:
			return 'www.chron.com';
			break;
		}
	}else{
		return 'apps.chron.com';
	}
}

function CHRON_adwiz_sync_prototype(){
	var x = CHRON_adwiz_tag;
	x.sitepage = OAS_sitepage;
	x.listpos = OAS_listpos;
	x.query = OAS_query;
}

/* RELOAD AD FEATURE */
function CHRON_reload_all(){

}

function CHRON_showad(x){
	try{
	var curdiv = document.getElementById('CHRON_ADPLACEMENT_'+x);
	curdiv.style.border="2px solid red";
	var addiv = document.createElement('div');
	addiv.style.background = 'pink';
	addiv.style.zIndex='9999';
	addiv.style.border='2px solid black';
	addiv.onClick="alert(CHRON_adwiz_tag.pos("+x+"))";
	if ( CHRON_adwiz_tag.pos(x).search(/iframe|\-flash/) ){
		//alert('flash/iframe detected');
		addiv.style.position="absolute";
		addiv.style.inherit='no';
	}else{
		addiv.style.position="relative";
	}

	addiv.innerHTML = 'Position: '+x;
	curdiv.insertBefore(addiv,curdiv.firstChild);
	window.curdiv.focus();
	}catch(e){}
}

function CHRON_reload_select(listpos){
	//reset listpos
	CHRON_adWiz_tag.listpos=listpos;
	

	//replace the OAS_MJX_CALL with new call
	

}
/* END RELOAD AD FEATURE */

//audit functions
window.onbeforeunload = chron_audit_ads;

function chron_audit_pos (pos) {
  if (CHRON_audit_list == '') {CHRON_audit_list = OAS_listpos}
  var reg = new RegExp("\\b"+pos+"\\b");
  CHRON_audit_list = CHRON_audit_list.replace(reg,'');
}
function chron_audit_ads () {
 try {
//  if (window.OAS_RICH) {
   if (CHRON_audit_list.match(/\w/)) {
	   var loc = window.location.toString();
      chron_deadDrop('http://origin.chron.com/apps/audit/ads.gif?sitepage='+OAS_sitepage+';listpos='+OAS_listpos+';noref='+CHRON_audit_list+';loc='+encodeURI(loc));
   }
//  }
 } catch (e) {}
} 
function chron_deadDrop(src) {
   try {
     var ifr = document.createElement('img');
     ifr.src=src;
     document.body.appendChild(ifr);
     ifr.style.visibility='hidden';
   } catch(e) {}
   return null;
}

// chronicle control variables need by adWiz.mpl
/* Scoped globally */
var CHRON_page = window.location.hostname + window.location.pathname;
var CHRON_param = CHRON_get_param();
var CHRON_hostname = detectAdwizSubDomain();
var CHRON_adwiz_src = 'http://'+ CHRON_hostname +'/apps/adWiz/adWiz.mpl';
//special variables used by the adwiz html tags and other processing
var CHRON_query ='';
var CHRON_listpos = '';
var CHRON_sitepage = '';
var CHRON_sitepage_append = '';
var CHRON_audit_list = '';

/* Protype of the adwiz tag structure*/
var CHRON_adwiz_tag = {
	keyname: new Array(), /* keyvalue pair */
	keyword: new Array(), /* arary of keywords */
	sitepage: '',
	listpos: '',
// 	query: ''
	pos: CHRON_adCode,
	param: CHRON_get_param_array(),
	rn: CHRON_random_number,
	referrer: '',
	referrer_domain: '',
	reload_all: CHRON_reload_all,
	reload_select: CHRON_reload_select,
	MJX: '',
	adwiz: '',
	show: CHRON_showad
};
/* prototype alias */
var pageinfo = CHRON_adwiz_tag;


// rbx: get and process all of the adwiz custom tags
CHRON_adwiz_add_history();
CHRON_get_adwiz_tags();
CHRON_serve_adwiz(); /* actually make the adwiz call */

