//jQuery functions
$(function() { //Initialization function, runs when the DOM has loaded
	var columnsList1 = "#eyepiece1, #eyepiece2, #eyepiece3, #Featured";
	var columnsList2 = "#topstories_top, #topstories_popular";
	var columnsList3 = "#Featured, #TopStoriesBox";
	createSubMenu();
	equalHeight(columnsList1);
	equalHeight(columnsList2);
	equalHeight(columnsList3);
	$(window).load(function() {
		equalHeight(columnsList1);
		equalHeight(columnsList2);
		equalHeight(columnsList3);
	});
	setTimeout(function(){
	// multimedia tabbed boxes
	$('#mmtabs').tabs({ fxFade: true, fxSpeed: 'fast' });
	$('#videotabs').tabs({ fxFade: true, fxSpeed: 'fast' }); },6000);
});

function createSubMenu(){
	newHeight = findPos(document.getElementById($('#MainNav').attr('id'))).y+$('#MainNav').height()-2;
	$('#MainNav li').hover(
		function(){	$('#subnav' + $(this).attr('id')).css('top',newHeight+'px'); },
		function(){ $('#subnav' + $(this).attr('id')).css('top','-999em'); }
	);
	$('.subnav').hover(
		function(){
			$(this).css('top',newHeight+'px');
			$('#'+$(this).attr('id').substring(6)+' a').addClass('MainNav_hover');
		},
		function(){
			$(this).css("top","-999em");
			$('#'+$(this).attr('id').substring(6)+' a').removeClass('MainNav_hover');
		}
	);
}
function findPos(obj) { //returns the x & y coordinates of an element
	var curleft = obj.offsetLeft || 0;
	var curtop = obj.offsetTop || 0;
	while (obj = obj.offsetParent) {
		curleft += obj.offsetLeft
		curtop += obj.offsetTop
	}
	return {x:curleft,y:curtop};
}

function equalHeight(colList) {
	$(colList).each(function() {$(this).height('auto');});
	var topHeight = 0;
	var staticTop = -1;
	$(colList).each(function() {
		if ($(this).height() >= topHeight) {
			topHeight = $(this).height();
		}
		if ($(this).css('position') == 'static') {
			staticTop = findPos(document.getElementById($(this).attr('id'))).y;
		}
	});
	topHeight = topHeight + 'px';
	staticTop = staticTop + 'px';
	$(colList).each(function() {
		$(this).height(topHeight);
		if (staticTop != -1) $(this).css('top',staticTop);
	});
};

function show_hide(id){
	if (document.getElementById){
		var obj = document.getElementById(id);
		if(obj){
			obj.style.display = (obj.style.display == 'none') ? '' : 'none';
		}
	}
}

function click_topstories_box(){
	show_hide('topstories_top');
	show_hide('topstories_top_img');
	show_hide('topstories_popular');
	show_hide('topstories_popular_img');
}

function getDateStr() {
	var date = new Date();
	var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	return '<span><strong>' + days[date.getDay()] + ', ' + months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear() + '</strong></span>';
}

function change_elem(text,id){
	if (document.getElementById){
		var x = document.getElementById(id);
		if(x){
			x.innerHTML = '';
			x.innerHTML = text;
		}
	}else if (document.all){
		if(x){
			var x = document.all[id];
			x.innerHTML = text;
		}
	}else if (document.layers){
		var x = document.layers[id];
		if(x){
			x.document.open();
			x.document.write(text);
			x.document.close();
		}
	}
}

//EYEPIECE PART (ONLY on the home page)
window.setTimeout("slide_eyepiece()",20000) //first call

var neyepiece = 1;
var do_slide_eyepiece = true;
function slide_eyepiece(){
	if(do_slide_eyepiece){
		nav_eyepiece();
	}
	window.setTimeout("slide_eyepiece()",20000);
}

function nav_eyepiece(what){
	if(what) do_slide_eyepiece = false;
	show_hide('eyepiece' + neyepiece);
	switch(neyepiece){
		case 1:	neyepiece = what=='-'?3:2;	break;
		case 2:	neyepiece = what=='-'?1:3;	break;
		case 3:	neyepiece = what=='-'?2:1;	break;
	}
	change_elem(neyepiece + '/3','eyepiece_count');
	show_hide('eyepiece' + neyepiece);
}
//end EYEPIECE PART

//STORY IMAGES PART (ONLY on the story-page)
var nstory_img = 1;
var do_slide_story_img = true;
var max_story_img = 1;
function slide_story_img(){
	if(max_story_img>1 && do_slide_story_img){
		nav_story_img();
	}
	window.setTimeout("slide_story_img()",20000);
}

function nav_story_img(what){
	if(what) do_slide_story_img = false;
	show_hide('StoryPhoto' + nstory_img);
	switch(nstory_img){
		case 1:			nstory_img = what=='-'?max_story_img:(nstory_img+1);	break;
		case max_story_img:	nstory_img = what=='+'?1:(nstory_img-1);			break;
		default:		nstory_img = what=='+'?(nstory_img+1):(nstory_img-1);	break;
	}
	change_elem(nstory_img + '/' + max_story_img,'story_img_count');
	show_hide('StoryPhoto' + nstory_img);
}
//end STORY IMAGES PART

// Photo Gallery Enlarge Photo
function openWindow(url, name, properties, openerName) {
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
		winReference = window.open('about:blank', name, properties);
		setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
	} else {
		winReference = window.open(url, name, properties);
	}
	setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);
	if (openerName) self.name = openerName;
	return winReference;
}

function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac") != -1 && agent.indexOf("msie") != -1 && (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1) ) {
		height += (status) ? 17 : 2;
	}
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;
	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
	var reference = openWindow(url, name, properties, openerName);
	return reference;
}

function openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
	var x, y = 0;
	if (screen) {
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}
	if (!status) status = '';
	if (!openerName) openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	return reference;
}

/**
* @package text_resize.js
*
* Functions that increase/decrease text font size based on values retrieved from a cookie.
* This script allows users to change the font size on story pages.
* @version 1.0
* @author Raymond Santucci
* license http://creativecommons.org/licenses/by-sa/2.5/
*
*/

// Set some global variables
origSize = 12; // original font-size if the element does not aleady have one set
minSize = 8; // minimum size we want the user to be able to set
maxSize = 24; // maximum size we want the user to be able to set

/* These are good for relative sizes
origSize = 85; // original font-size if the element does not aleady have one set
minSize = 50; // minimum size we want the user to be able to set
maxSize = 100; // maximum size we want the user to be able to set
*/
elementID = 'storyBody'; // the element to change font-size within
unit = "px"; // unit for font-size [px,ex,em,%,etc]
resize = 2; // amount resize font each time button is hit

// If the element's font-size has been set as an inline style, override the set variable
function getOrigSize() {
	if (!document.all && !document.getElementById) return;  
	var el = document.getElementById ? document.getElementById(elementID) : document.all[elementID];
	if (!el) return;
	el = el.style.fontSize;
	if (el != "") origSize = parseInt(el);
	return;
}

// Change font-size within the element depending on action
function myTextSize(chgsize) {
	if (!document.all && !document.getElementById) return;
	var el = document.getElementById ? document.getElementById(elementID) : document.all[elementID];
	if (!el) return;
	
	var newSize = 0;
	var startSize = parseInt(myGetCookie("my-textsize"));
	if (!startSize) startSize = origSize;
	switch (chgsize) {
		case "incr":	newSize = startSize + resize;		break;
		case "decr":	newSize = startSize - resize;		break;
		case "reset":	newSize = origSize;					break;
		default:		newSize = startSize;				break;
	}  
	if (!newSize) return;
	if (newSize < minSize) newSize = minSize;
	if (newSize > maxSize) newSize = maxSize;
	
	newSize += unit;
	el.style.fontSize = newSize;
	mySetCookie("my-textsize",newSize,365);  
}

// Set cookie to store user-selected font-size
function mySetCookie(name,value,days) {
	var cookie = name + "=" + value + ";";
	if (days) {
		var myDate=new Date();
		myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
		cookie += " expires=" + myDate.toGMTString() + ";";
	}
	cookie += " path=/";
	document.cookie = cookie;
}

// Get cookie containing user-selected font-size
function myGetCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == " ") c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return;
}

// Call the functions that will intialize this script
function init() {
	getOrigSize();
	myTextSize();
	return;
}

window.onload=init;

//This JavaScript contains functions for combining multiple searches into one form
//Created by: Steve Downs (sdowns@gannett.com), Gannett Content Management Team 
//Last modified: 6/24/2004

function newsSearch(frm) {
	var selectedmode = "all";
	var searchpublicus = true;
	//Loop through form fields	
	for (var i = 0;i < frm.elements.length;i++) {
		if ((frm.elements[i].name == "sf_pubsys_story")) {
			if (frm.sf_pubsys_story.value=="") {
				alert ("Please enter a search word");
				frm.sf_meta_site.focus();
				return false;
			}
		}
		if (frm.elements[i].name == "daterange") {
			selectedrange = frm.elements[i].value;
			if (frm.daterange[0].checked != true) {
				searchpublicus = false;
			}
		}
	}
	
	//Submit the form to the archive if the daterange is not 7 days
	if (searchpublicus == false) {
		newURL = "http://nl.newsbank.com/nlsearch.pl?forceTheme=gannett&s_site=" + frm.s_site.value + "&search_mode=" + selectedmode + "&action=search&date_mode=year&year=" + selectedrange + "&sort=d:h&nitems=10&region=" + frm.region.value + "&dbquery=" + frm.sf_pubsys_story.value;
		document.location = newURL;
		return false;
	}
	else {
		return true;
	}
}

// calendar widget

var imgDir = '/static/Idaho2007/images/';

pic1 = new Image(100,21); 
pic1.src = imgDir + "tab_c1_cal.gif"; 
pic2 = new Image(94,21); 
pic2.src = imgDir + "tab_c1_din.gif"; 
pic3 = new Image(106,21); 
pic3.src = imgDir + "tab_c1_mov.gif"; 
pic4 = new Image(100,21); 
pic4.src = imgDir + "tab_m1_cal.gif"; 
pic5 = new Image(94,21); 
pic5.src = imgDir + "tab_m1_din.gif"; 
pic6 = new Image(106,21); 
pic6.src = imgDir + "tab_m1_mov.gif"; 
pic7 = new Image(100,21); 
pic7.src = imgDir + "tab_d1_cal.gif"; 
pic8 = new Image(94,21); 
pic8.src = imgDir + "tab_d1_din.gif"; 
pic9 = new Image(106,21); 
pic9.src = imgDir + "tab_d1_mov.gif"; 

var Days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var today = new Date();
var Year = takeYear(today);
var Month = leadingZero(today.getMonth()+1);
var DayName = Days[today.getDay()];
var Day = leadingZero(today.getDate()); 

function takeYear(theDate) { 
	x = theDate.getYear();var y = x % 100;y += (y < 38) ? 2000 : 1900;return y; 
}

function leadingZero(nr) {
	if (nr < 10) nr = "0" + nr;return nr;
}

var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name) {
	if(document.getElementById){
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style; 
	}else if (document.all){
		this.obj = document.all[name];
		this.style = document.all[name].style; 
	}else if (document.layers){
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function flipeiwidget(num) {
	var imgDir = 'http://cityguide.entertainmentidaho.com/fe/images/';
	eit1.src = imgDir + "tab_" + num + "_cal.gif";
	eit2.src = imgDir + "tab_" + num + "_mov.gif";
	eit3.src = imgDir + "tab_" + num + "_din.gif";
	for(i=1; i<4; i++){
		if (!DHTML) return; 
		var eix = new getObj('eibox'+i);
		eix.style.visibility = 'hidden';
	}
	var eix = new getObj(num);
	eix.style.visibility = 'visible';
}

function flipformbox(catsel) {
	if (catsel != '') {
		if (catsel == 'Sports') {var eivar = 'formbox2';}
		if (catsel == 'Entertainment') {var eivar = 'formbox3';}
		if (catsel == 'Outdoors') {var eivar = 'formbox4';}
		if (catsel == 'Community') {var eivar = 'formbox5';}
	}
	else { var eivar = 'formbox1'; }
	
	for(i=1; i<6; i++){
		if (!DHTML) return; 
		var eiformx = new getObj('formbox'+i);
		eiformx.style.visibility = 'hidden';
	}
	var eiformx = new getObj(eivar);
	eiformx.style.visibility = 'visible';
}

// disallows profane usernames
function returnProfaneList() {
	return 'shit,sh1t,fuck,damn,d4mn,suck,bitch,b1tch,jackass,j4ckass,jack4ss,j4ck4ss';
}

function returnProfaneRegex() {
	var filter = /sh[i1]t|fuck|[a4]s{2,}h[o0]l[e3]|n[i1]g{2,}[a4e3]|j[a4]ck[a4]s|d[a4]m[mn]|suck|b[i1]tch/i;
	return filter;
}

