/*
	/static/scripts/site.js
	holds site-specific js code, included in aggregation/minification
	jQuery required
	(c) 2010, The State Media Company
	Chris Hessert <chris.hessert@gmail.com>
*/


/*  declarations  */
var rscriptloaded = 0;


/*  functions  */

// toggle asset list on/off
function asset_accordion_toggle() {
	var assets = $('div.story_assets ul');
	var headline = $('div.story_assets h5 a');
	if (assets.css('display') == "block") {
		// Assets are currently shown
		headline.text("Click for Related Content");
		assets.css('display', 'none');
	} else {
		// Assets are currently invisible
		headline.text("Click to Hide");
		assets.css('display', 'block');
	}
 }
 
// grab recaptcha js file if showing email form
function displayEmailForm() {
	if (rscriptloaded) {
		mi_story_tool.storyTool("email", "<& /mi/pubsys/story/page_url, version=>'email_form' &>");
	} else {
		$.getScript("http://api.recaptcha.net/js/recaptcha_ajax.js", function() {
			mi_story_tool.storyTool("email", "<& /mi/pubsys/story/page_url, version=>'email_form' &>");
			rscriptloaded = 1;
		});
	}
}

// build 'today' string
function today_string() {
	// dependant on date functions defined in mi-utilities.js
	var today = new Date();
	return today.getDayString()+', '+today.getMonthString()+' '+today.getDate()+', '+today.getFullYear();
}

// search
function searchFormSubmit(form) {
	if (form.aff.value == 'archives' || form.aff[1].checked == true) {
		window.location = "http://www.newslibrary.com/nlsearch.asp?search_mode=all&action=search&sort=d%3Ah&nitems=10&region=cs&dbquery=" + form.keywords.value;
		return false;
	} else if (form.aff[2].checked == true) {
		window.location = "http://www.thestate.com/cgi-bin/mi/overture/overture.pl?Keywords=" + form.keywords.value;
		return false;
	}
	return true;
}

// search **********************************************************************
function searchFormSubmit(form) {
    if (form.aff.value == 'archives' || form.aff[1].checked == true) {
                window.location = "http://www.newslibrary.com/nlsearch.asp?search_mode=all&action=search&sort=d%3Ah&nitems=10&region=cs&dbquery=" + form.keywords.value;
                return false;
	}
	else if (form.aff[2].checked == true || form.aff.value == 'web') {
        	var encoded_keywords = encodeURIComponent(form.keywords.value);
	        var section_num = '299';
        	var url_version = 'ysr';
	        var params = 'product=Yahoo%2COverture&' +
        	             'collection=WEB&' +
                	     'live_template=http%3A%2F%2Fwww.thestate.com%2F' + section_num + '%2Fv-' + url_version + '%2Findex.html&' + 
	                     'error_template=http%3A%2F%2Fwww.thestate.com%2F' + section_num + '%2Fv-yerr%2Findex.html&' +
        	             'preview_template=http%3A%2F%2Fpreview.thestate.com%2F' + section_num + '%2Fv-' + url_version + '%2Findex.html&' +
                	     'results_per_page=10' +
			     '&prop_related=1&prop_dym=1';
	        window.location = "http://search.thestate.com/search-bin/search.pl.cgi?sf_Keywords=" + encoded_keywords + '&' + params;

        	return false;
     }
        return true;
}

function searchFormSubmitGC(form) {
	if (form.gg[1].checked || form.gg.value == 'yahoo') {
        	var encoded_keywords = encodeURIComponent(form.keywords.value);
	        var section_num = '299';
        	var url_version = 'ysr';
	        var params = 'product=Yahoo%2COverture&' +
        	             'collection=WEB&' +
                	     'live_template=http%3A%2F%2Fwww.thestate.com%2F' + section_num + '%2Fv-' + url_version + '%2Findex.html&' + 
	                     'error_template=http%3A%2F%2Fwww.thestate.com%2F' + section_num + '%2Fv-yerr%2Findex.html&' +
        	             'preview_template=http%3A%2F%2Fpreview.thestate.com%2F' + section_num + '%2Fv-' + url_version + '%2Findex.html&' +
                	     'results_per_page=10' +
			     '&prop_related=1&prop_dym=1';
	         window.location = "http://search.thestate.com/search-bin/search.pl.cgi?keep_keys=" + encoded_keywords + "&sf_Keywords=" + encoded_keywords + '&' + params;

        	return false;
	}
	else if (form.gg[0].checked || form.gg.value == 'gamecocks') {
        var encoded_keywords = encodeURIComponent(form.keywords.value);
	    var section_num = '299';
        var url_version = 'ggc';
		var params = 'product=pubsys&collection=ENDECA_INDEX&' +
                	 'live_template=http%3A%2F%2Fwww.thestate.com%2F' + section_num + '%2Fv-' + url_version + '%2Findex.html&' + 
        	         'preview_template=http%3A%2F%2Fpreview.thestate.com%2F' + section_num + '%2Fv-' + url_version + '%2Findex.html&' +
    				 'fields=*&' +
    				 'results_per_page=25&' +
    				 'aggregate_key=meta_rollup&' +
					 'sf_meta_domain=www.thestate.com&' +
    				 'sort=pubsys_story_print_publish_dt+desc&' +
					 'prop_matchmode=boolean&' +
					 'sf_pubsys_section_id=591+OR+592+OR+594+OR+595+OR+599+OR+600+OR+603+OR+604+OR+605+OR+606+OR+607+OR+608+OR+610+OR+611+OR+617+OR+618&' + 
//					 'sf_pubsys_section_id=>+590;+=<+619&' +
    				 'sf_pubsys_story=' + encoded_keywords;

        window.location = "http://search.thestate.com/search-bin/search.pl.cgi?keep_keys=" + encoded_keywords + "&" + params;
		return false;
	}
    return true;
}
// end search ******************************************************************

// returns url query string as an array of key/value pairs
function getParams(params) {
	var Params = new Object ();
	if ( ! params ) return Params; // return empty object
	var Pairs = params.split(/[;&]/);
	for ( var i = 0; i < Pairs.length; i++ ) {
		var KeyVal = Pairs[i].split('=');
		if ( ! KeyVal || KeyVal.length != 2 ) continue;
		var key = unescape( KeyVal[0] );
		var val = unescape( KeyVal[1] );
		val = val.replace(/\+/g, ' ');
		Params[key] = val;
	}
	return Params;
}

// tableStriper adds classes to even and odd rows when fed table id, so that styles can be attached, by K. Davis 10/17/10 
function tableStriper(tableName){
	var theTable = document.getElementById(tableName);
	var rows = theTable.getElementsByTagName('tr');
	for (var i = 2; i < rows.length; i++) {
	if (i%2) {
		rows[i].className+=' story-table-odd';
	} else {
		rows[i].className+=' story-table-even';
	}
}

