/*
<!-- 

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_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++){
	//debug alert
	//alert(i+CHRON_gat[i].getAttribute('name'));
                switch (CHRON_gat[i].getAttribute('type')){
                        case 'keyname':
                                if ( CHRON_gat[i].getAttribute('name') && CHRON_gat[i].getAttribute('value') ){
                                        CHRON_query += CHRON_gat[i].getAttribute('name') + '=' + CHRON_gat[i].getAttribute('value') + '&';
                                }
                                break;
                        case 'keyword':
                                if ( CHRON_gat[i].getAttribute('value') ){
                                        CHRON_query += CHRON_gat[i].getAttribute('value') + '&';
                                }
                                break;
                        case 'rkeyname':
                                if ( CHRON_gat[i].getAttribute('name') && CHRON_gat[i].getAttribute('value') ){
                                        CHRON_query += CHRON_gat[i].getAttribute('value') + '=' + CHRON_gat[i].getAttribute('name') + '&';
					
                                }
                                break;
                        case 'sitepage':
                                if (CHRON_gat[i].getAttribute('value') && CHRON_gat[i].getAttribute('order')){
                                        CHRON_sitepage_array[CHRON_gat[i].getAttribute('order')] = CHRON_gat[i].getAttribute('value');
                                }
                                break;
                        case 'listpos':
                                if (CHRON_gat[i].getAttribute('value')){
                                        CHRON_listpos = CHRON_gat[i].getAttribute('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);
	//CHRON_listpos
	//nothing to do
        //alert('<!-- CHRON_query    [[ ' + CHRON_query + ' ]] -->');
        //alert('<!-- CHRON_listpos  [[ ' + CHRON_listpos + ' ]] -->');
        //alert('<!-- CHRON_sitepage [[ ' + 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);
				//document.body.insertBefore(CHRON_cloned_node, document.getElementsByName('body')[0]); //insert at the top of the body -- DOESN'T WORK IN IE
				/*document.body.appendChild(CHRON_cloned_node);*/			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);
				//document.body.insertBefore(CHRON_cloned_node, document.getElementsByName('body')[0]);
				//insert at the top of the body -- DOESN'T WORK IN IE
				//document.body.appendChild(CHRON_cloned_node);			
				CHRON_clone[i].parentNode.appendChild(CHRON_cloned_node);
				delete(CHRON_cloned_node);
			}
		}
		
	} 
	//adwiz_errors ?	alert(adwiz_errors):0;	
	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){
	//liststring=liststring.replace(/^\/|\/$/, '');
	//give me the the variables and I will sort it out
	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 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':
			return CHRON_subdomain + '.chron.com';
			break;
		default:
			return 'www.chron.com';
			break;
		}
	}else{
		return 'www.chron.com';
	}
}

// chronicle control variables need by adWiz.mpl

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 = '';

// rbx: get and process all of the adwiz custom tags
CHRON_get_adwiz_tags();
// adWiz call -- NO NEED TO EDIT
document.write('<script id="OAS_MJX_TAG" language="javascript" src="' + CHRON_adwiz_src + '?url=' + CHRON_page + '&param='+ CHRON_param +'"></script>');


