//----------------------------------------
//
//	Article Tools javascript
//	created for NYTRNG
//      Fixed by Charlie Szymanski
//----------------------------------------

//	toolsToggle
//	Used for animated slidedown options on article tools (requires prototype and scriptaculous)
function toolsToggle( ID ){
	new Effect.toggle(ID, 'blind', {duration: .2});
	if(ID == "art_facts"){
		if($("factboxClickLink"))
			$("factboxClickLink").toggle();
	}
}

//	emailArticle
//	AJAX request that emails an article to someone
function emailArticle( url, permalinkt, title, category, categoryid, byline, leadin, date, from, fromemail, toemail, comments ){
	
	// if all boxes not filled in, display error and quit
	if(!from || !fromemail || !toemail){
		$("formInfo").innerHTML = "Please fill out your name, your e-mail address, and the recipients e-mail address";
		$("formInfo").style.display = "block";
		$("formInfoFooter").innerHTML = "Please fill out your name, your e-mail address, and the recipients e-mail address";
		$("formInfoFooter").style.display = "block";
		return;
	}else{
		Element.hide('formInfo');
		Element.hide('formInfoFooter');
	}
	
	// shows loading image
	Element.show('email_loading');
	Element.show('email_loadingFooter');
	// disables form elements
	Form.disable('email_article_form');
	Form.disable('email_article_formFooter');
	//console.log(comments);
	// creates vars to be passed
	var vars = 'link=' + permalinkt.gsub("http:%2F%2Fwww.heraldtribune.com", "");
	vars += '&tittel=' + 'HeraldTribune.com: ' + title;
	vars += '&articlecategory=' + category;
	vars += '&categoryid=' + categoryid;
	vars += '&from=' + from;
	vars += '&fromemail=' + fromemail;
	vars += '&to=' + toemail;
	vars += '&title=' + 'HeraldTribune.com: ' + title;
//PLAIN TEXT VERSION
//vars += '&message=' + title + '\n' + category + ' - ' + byline + '\n' + date + '\n\n' + leadin + '...';
	vars += '&html=' + '1';
	vars += '&message=' + '<b>' + title + '</b><br>' + byline + '<br>' + date + '<br><br>' + leadin + '...' + "<br /><br /><b>Comments by "+from+": </b><br /><br />" + comments;

var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: vars, 
			onComplete: emailArticleComplete
		});
		
	function emailArticleComplete(originalRequest){
		$("formInfo").innerHTML = "Your Email has been sent";
		$("formInfo").style.display = "block"; //because element.show displays inline
		$("formInfoFooter").innerHTML = "Your Email has been sent";
		$("formInfoFooter").style.display = "block";
		/*
		// closes email toolbox
		if($('email_article').style.display != 'none'){
			new Effect.toggle('email_article', 'blind', {duration: .2});
			var element = $('email_article_link').getElementsByTagName('img')[0];
			element.src = '/graphics/article_tools/email.gif';
		}
		*/
		// hides loading image
		Element.hide('email_loading');
		Element.hide('email_loadingFooter');
		
		// re-enables and clears form elements
		Form.enable('email_article_form');
		Form.reset('email_article_form');
		Form.enable('email_article_formFooter');
		Form.reset('email_article_formFooter');
		
	}
}




function setTextSize(size, elemID){
	elem = $(elemID);
	$("articleText").style.fontSize = size+".5%";
	if(!elem) return;
	elem.addClassName("selectedTextSize");
	$A(elem.siblings()).each(function(e){e.removeClassName("selectedTextSize")});
	//and for the footer
	elemFooter = $(elemID+"_footer");
	if(!elemFooter) return;
	elemFooter.addClassName("selectedTextSize");
	$A(elemFooter.siblings()).each(function(e){e.removeClassName("selectedTextSize")});
}
//cookie stuff added by CS
function setTextCookie(size){
	var exdate = new Date();
	exdate.setDate( exdate.getDate()+"999" );
	document.cookie = "articleTextSize=" + escape(size) + ";expires=" + exdate.toGMTString();
}
//check for cookie on page load
Event.observe(window, "load", getArticleCookie);
//get cookie value and assign to font size
function getArticleCookie(){
	var fontSize = getCookie2("articleTextSize");
	if( (fontSize != "140") || (fontSize==0) ){
		if(!$("articleText")) return;
		setTextSize(fontSize, "textSizer_"+fontSize);
		//$("articleText").style.fontSize = fontSize;
	}
}

//	printPage
//	prints page if it can. else it alerts user on how to print.
function printPage(){
	if (!window.print) {
		alert('To print this article, open the file menu and choose Print.');
	} else {
		setTimeout("window.print();", 500);
	}
}

//vars needed for photo tools hover
var photoToolsInterval = new Array();
var photoToolsHidden = new Array();

//	showPhotoTools
//	shows photo tools
function showPhotoTools(elementId){
	if(photoToolsHidden[elementId]==undefined){
		photoToolsHidden[elementId] = true;
	}
	clearInterval(photoToolsInterval[elementId]);
	Element.setOpacity(elementId, 0.85);
	if( photoToolsHidden[elementId] ){
		new Effect.BlindDown(elementId, {duration: .2, queue:'end'});
		photoToolsHidden[elementId] = false;
	}
}

//	hidePhotoTools
//	sets photo tools to hide after a certain time limit
function hidePhotoTools(elementId){
	clearInterval(photoToolsInterval[elementId]);
	photoToolsInterval[elementId] = setInterval("hidePhotoToolsOnInterval('"+elementId+"');", 500);
}

//	hidePhotoToolsOnInterval
//	hides photo tools
function hidePhotoToolsOnInterval(elementId){
	clearInterval(photoToolsInterval[elementId]);
	photoToolsHidden[elementId] = true;
	new Effect.BlindUp(elementId, {duration: .2, queue: 'end'});
}

//vars needed for weather extra
var weatherExtraInterval;
var weatherExtraHidden = true;

//	showWeatherExtra
//	shows weather extra content
function showWeatherExtra( element, extraID ){
	clearInterval(weatherExtraInterval);
	var weatherPointers = document.getElementsByClassName('weather_pointer');
	var theWeatherPointer = element.getElementsByClassName('weather_pointer')[0];
	for(i=0;i<weatherPointers.length;i++){
		if(weatherPointers[i]==theWeatherPointer){
			Element.show(weatherPointers[i]);
		}else{
			Element.hide(weatherPointers[i]);
		}
	}
	
	var weatherContent = document.getElementsByClassName('weather_extra_content');
	var theWeatherContent = $(extraID);
	for(i=0;i<weatherContent.length;i++){
		if(weatherContent[i]==theWeatherContent){
			Element.show(weatherContent[i]);
		}else{
			Element.hide(weatherContent[i]);
		}
	}
	
	if(weatherExtraHidden){
		new Effect.BlindDown('weather_extra', {duration: .2, queue: 'end'});
		weatherExtraHidden = false;
	}
}

//	stopHideWeatherExtra
//	stops content from being hidden
function stopHideWeatherExtra(){
	clearInterval(weatherExtraInterval);
}

//	hideWeatherExtra
//	sets weather extra to hide after a certain time limit
function hideWeatherExtra(){
	clearInterval(weatherExtraInterval);
	weatherExtraInterval = setInterval("hideWeatherExtraOnInterval();", 500);
}

//	hideWeatherExtraOnInterval
//	hides weather extra
function hideWeatherExtraOnInterval(){
	clearInterval(weatherExtraInterval);
	new Effect.BlindUp('weather_extra', {duration: .2, queue: 'end'});
	weatherExtraHidden = true;
	setTimeout("var weatherPointers = document.getElementsByClassName('weather_pointer'); for(i=0;i<weatherPointers.length;i++){Element.hide(weatherPointers[i]);}", 200);
}

// save page -- hilariously this just throws an alert.
function saveAsMe (filename)
{
	document.execCommand('SaveAs',null,filename)
}


