/* site.js */

/* story tool functions */
var toolstate = "off";
var toolnames = new Array ();
toolnames['email'] = "email this story";

function storyTool(tool,url) {
	// set title of tool
	$("#toolname").empty();
	$("#toolname").append(toolnames[tool]);
	// clean up tool area
	$("#tool").empty();
	$("#tool").append("loading...");

	// send request for tool, display if loads ok otherwise display error
	$.ajax({
		type: "GET",
		url: url,
		success: function(msg){
			$("#tool").empty();
			$("#tool").append(msg);
		},
		error: function(){
			$("#tool").empty();
			$("#tool").append("There was a problem loading this tool.");
		}
	});
	// display toolbox
	if(toolstate == "off") {
		$("#story_activity_count").css("display","none");
		$("#toolbox").fadeIn("fast");
	}
	toolstate= "on";
}

function closeTool() {
	// hide toolbox
	$("#story_activity_count").css("display","block");
	$("#toolbox").fadeOut("fast");
	toolstate = "off";
}

function sendStory(theForm) {
	// validate form
	if(validate(theForm) === true)
	{
		// clear tool and display message
		$("#tool").empty();
		$("#tool").append("sending...");

		// send form for processing
		$.ajax({
			type: "POST",
			url: "/cgi-bin/mi/mailastory.cgi",
			data: { domain: theForm.domain.value, url_form: theForm.url_form.value, email_type: theForm.email_type.value, url_text: theForm.url_text.value, url_html: theForm.url_html.value, url_story: theForm.url_story.value, to_email: theForm.to_email.value, from_email: theForm.from_email.value, from_name: theForm.from_name.value, comments: theForm.comments.value, headline: theForm.headline.value  },
			success: function(msg){
				// clear tool message, display message from server
				$("#tool").empty();
				$("#tool").append(msg);
				$("#emailForm").empty();
				// close tool after delay
				$("#toolbox").fadeOut(7000);
				toolstate = "off";
			},
			error: function(){
				$("#tool").empty();
				$("#tool").append("There was a problem sending this story, please try again.");
			}
		});
		$("#story_activity_count").css("display","block");
	}
	return false;
}

function mvForm() {
	var adj = $("#story_body").height() - 150;
        $("#toolbox").css("top",adj);
}

/* story tool functions END */

/* date functions */

Date.prototype.getDateString = function(){
 var day = '';
var date=this.getDate();
date = date.toString();
if (date.length == 1)
{
date = '0'+date;
}
return date;
 switch (this.getDate().length) {
 case 2:
 return this.getDate();
 case 1:
 return '0'+this.getDate();
 }
}

Date.prototype.getMMString = function(full){
 var day = '';
 switch (this.getMonth()) {
 case 0:
 return '01';
 case 1:
 return '02';
 case 2:
 return '03';
 case 3:
 return '04';
 case 4:
 return '05';
 case 5:
 return '06';
 case 6:
 return '07';
 case 7:
 return '08';
 case 8:
 return '09';
 case 9:
 return '10';
 case 10:
 return '11';
 case 11:
 return '12';
 }
}

function searchDateRange(range) {
	var myDate=new Date();
	var today=new Date();
	myDate.setDate(myDate.getDate()-range);
	return str = 'sf_pubsys_pubobj_publish_dt=%3E%3D+'+myDate.getMonthString()+'\/'+myDate.getDateString()+'\/'+myDate.getFullYear()+'%3B+%3C%3D+'+today.getMonthString()+'\/'+today.getDateString()+'\/'+today.getFullYear();
}

/* date functions END */

/* login related functions */

/* The folloing is not used anymore. The standard MI login links is used. - CNC - 6/10/08 */

//function loginDiv() {
//	var insitecookie=document.cookie.match('mcclatchydc_user_auth');
//	var user_name;
//	var icname = GetCookie(insitecookie);
//	try {
//		var user_name = icname.replace(/\|.*/,"");
//	} catch (err) {}
//	var loginDiv = (document.all) ? document.all("login") : document.getElementById("login");
//	if (icname && icname.indexOf('.threshold') == -1) { 
//		loginDiv.innerHTML = "<ul class=\"inlist\"><li><span>Welcome "+user_name+"!</span> | </li><li><a href=\"/reg-bin/tint.cgi?mode=logout\">logout</a></li></ul>\n";
//		// add member functions to nav
//	}
//	else {
//		// add nonmember functions to nav
//	}
//}
//onloadFunctions("loginDiv()","d");


/* login related functions END */

/* navigation menu handler */
function nav_form(theForm) { 
	location.href='/'+theForm.section.value;
	return false; 
}
/* navigation menu handler END */

/* homepage functions */

function equalHeight() {
	if($("#right_wrap").length == 1) {
        var col2 = $("#col_2content").children(":first-child").height() + 15;
        var col3 = $("#col_3content").children(":first-child").height() + 15;
        var col2ext1 = $("#col_2ext").children(":first-child").height() + 15;
        var col3ext1 = $("#col_3ext").children(":first-child").height() + 15;
        var col2ext2 = $("#col_2ext").children(":last-child").height() + 15;
        var col3ext2 = $("#col_3ext").children(":last-child").height() + 15;
        if(col2 > col3) {
			$("#col_3content").children(":first-child").height(col2);
			$("#col_2content").children(":first-child").height(col2);
		}
		else {
			$("#col_2content").children(":first-child").height(col3);
			$("#col_3content").children(":first-child").height(col3);
		}
		if(col2ext1 > col3ext1) {
			$("#col_3ext").children(":first-child").height(col2ext1);
			$("#col_2ext").children(":first-child").height(col2ext1);
		}
		else {
			$("#col_2ext").children(":first-child").height(col3ext1);
			$("#col_3ext").children(":first-child").height(col3ext1);
		}
		if(col2ext2 > col3ext2) {
			$("#col_3ext").children(":last-child").height(col2ext2);
			$("#col_2ext").children(":last-child").height(col2ext2);
		}
		else {
			$("#col_2ext").children(":last-child").height(col3ext2);
			$("#col_3ext").children(":last-child").height(col3ext2);
		}
        }
        else {
                var col1 = $("#spotlight").height();
	        var col2 = $("#section_promo").height();
	
	        if(col1 > col2) {
	                $("#spotlight").height(col1);
	                $("#section_promo").height(col1);
	        }
	        else {
	                $("#spotlight").height(col2);
	                $("#section_promo").height(col2);
        	}
        }
}
onloadFunctions("equalHeight()","w");

/* This function load a story via AJAX if SSI fails */
function storyLoad(elem,url) {
	if($("#"+elem).html().indexOf("[an error occurred while processing this directive]") > 0) {
		$("#"+elem).load(url);
	}
}

/* DART vars */
var _krdDartOrd = new Date().getTime();
var _krdDartInc = 0;

/* END homepage function */


//****************************************************************************
// Form Input value mouseover/out script 
//****************************************************************************
function clearf(object,text)
{
 if (object.value == text)
 {
  object.value = "";
  object.focus();
 }
}

function setf(object,text)
{
 if(object.value == "")
 {
  object.value = text;
  this.focus();
 }
}
$(function() {
	$(".left_50 .blog_header:gt(0), .right_50 .blog_header:gt(0)").css("border-top","1px solid #CCC")
});
