//This makes it easy to call an element by id
//just use the '$' symbol instead of 'document.getElementById'
//ie  $('element').src = 'whatever.jpg';
function $(id) {
	return document.getElementById(id);	
}

//This makes it easy to change the class of something
//without having to use an if block every time
function changeClass(theElem, theClass) {
	if(document.all) {
		$(theElem).setAttribute("className", theClass);
	} else {
		$(theElem).setAttribute("class", theClass);
	}
}

//------------------------------
function writeMovie() {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="main" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="flash/main.swf" /><param name="menu" value="false" /><param name="salign" value="t" /><param name="wmode" value="window" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="bgcolor" value="#FFFFFF" /><embed src="flash/main.swf" menu="false" quality="high" scale="noscale" bgcolor="#ffffff" width="100%" wmode="window" height="100%" salign="t" name="main" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

//------------------------------
function menuRoll(id) {/*
	if(document.all) {
		$('menu_' + id).setAttribute("className", "menuButtonRightOver");
	} else {
		$('menu_' + id).setAttribute("class", "menuButtonRightOver");	
	}
	*/
	var buttonID = $(id);
	buttonID.src = "../images/buttons/" + id +"_rollover.gif";
}

//------------------------------
function menuRollOut(id) {
	/*

	if(document.all) {
		$('menu_' + id).setAttribute("className", "menuButtonRight");
	} else {
		$('menu_' + id).setAttribute("class", "menuButtonRight");	
	}
	*/
	var buttonID = $(id);
	buttonID.src = "../images/buttons/" + id +".gif";
}

//------------------------------
function headline(copy) {
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="800" height="50"> <PARAM name="movie" value="/flash/headline.swf?headlinecopy=' + copy + '"> <PARAM name="quality" value="high"> <EMBED src="/flash/headline.swf?headlinecopy=' + copy + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="800" height="50"></embed></object>');	
}

//------------------------------
function headlinesm(copy, id) {
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="480" height="30"> <PARAM name="movie" value="/flash/headlinesm1.swf?headlinecopy=' + copy + '&storyid=' + id + '&random=' + Math.random(999999) + '"> <PARAM name="quality" value="high"> <PARAM name="wmode" value="transparent"> <EMBED src="/flash/headlinesm1.swf?headlinecopy=' + copy + '&storyid=' + id + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" width="480" height="30"></embed></object>');	
}

//------------------------------
function headlinesmall(copy, id, subtext) {
	document.getElementById("head_" + id).innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="480" height="30"> <PARAM name="movie" value="/flash/headlinesm1.swf?headlinecopy=' + copy + '&storyid=' + id + '&random=' + Math.random(999999) + '"> <PARAM name="quality" value="high"> <PARAM name="wmode" value="transparent"> <EMBED src="/flash/headlinesm1.swf?headlinecopy=' + copy + '&storyid=' + id + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" width="480" height="30"></embed></object><div id="subtext_<?=$row->id ?>" style="padding-left:10px;">' + subtext + '</div>';	
}

//------------------------------
function calday(month,day,total) {
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="106" height="95"> <PARAM name="movie" value="/flash/calday.swf?month=' + month + '&day=' + day + '&total=' + total + '"> <PARAM name="quality" value="high"> <EMBED src="/flash/calday.swf?month=' + month + '&day=' + day + '&total=' + total + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="106" height="95"></embed></object>');	
}

//------------------------------
function ajax() {
	var ro;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ro = new XMLHttpRequest();
	}
	return ro;
}

//------------------------------
function vote(num, art) {
	if(voted == 0) {
		var ajx = new ajax();
		ajx.open('get','/includes/sitedata.php?action=vote&num=' + num + '&art=' + art,true);
		ajx.onreadystatechange = function() {
			if(ajx.readyState == 4) {
				// Vote Submitted	
				reloadVote(art);
				voted = 1;
			}
		}
		ajx.send(null);
	} else {
		alert("You have already voted on this story");	
	}
}

//------------------------------
function reloadVote(art) {
	$('votestatus').innerHTML = "Loading Votes....";
	var ajx = new ajax();
	ajx.open('get','/includes/sitedata.php?action=reload&art=' + art,true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			// Vote Submitted	
			resetall(1);
			var voteinfo = ajx.responseText.split("|");
			
			for(i=1;i<=Math.round(voteinfo[0]);i++) {	
				$('vote_' + i).src='/images/star.jpg';
			}
			if(voteinfo[1] == "1") {
				$('votestatus').innerHTML = voteinfo[1] + " vote";
			} else if(voteinfo[1] == "0") {
				$('votestatus').innerHTML = "No votes";	
			} else {
				$('votestatus').innerHTML = voteinfo[1] + " votes";
			}
		}
	}
	ajx.send(null);
}

//------------------------------
function resetall(bypass) {
	if(voted == 0 || bypass == 1) {
		for(i=1;i<=5;i++) {	
			$('vote_' + i).src='/images/nostar.jpg';
		}
	}
}
voted = 0;

//------------------------------
function rvote(num) {
	if(voted == 0) {
		resetall();
		for(i=1;i<=num;i++) {
			$('vote_' + i).src='/images/star.jpg';		
		}
	}
}

//CALENDAR STUFF ************************************************************
function calendarSectionClicked(eventType) {
	calendarEventType = eventType;
	$("section1Header").innerHTML = calendarEventType;
	calendarGetEvents(calendarEventType);
}

//------------------------------
function calendarUpdateListHeader() {
	document.getElementById("eventListHeader").innerHTML = "Events happening on " + selectedDateParts[1] + "-" + selectedDateParts[2] + "-" + selectedDateParts[0];
}

//------------------------------
function calendarGetEvents(eventType) {
	var ajx = new ajax;
	ajx.open('get', 'includes/sitedata.php?action=calendar&date=' + selectedDate + '&type=day&eventType=' + eventType, true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			if(ajx.responseText != 'bad') {
				document.getElementById('eventList').innerHTML = ajx.responseText;
			} else {
				alert("error");
			}
		}

	}
	ajx.send(null);
}

//------------------------------
function calendarSelectDate(theDate) {
	if(document.all) {
		if(document.getElementById(selectedDate).getAttribute('className') == "day today") {
			document.getElementById(selectedDate).setAttribute("className","day today");
		} else {
			document.getElementById(selectedDate).setAttribute("className","day");
		}
		if(document.getElementById(theDate).getAttribute('className') != "day today") {
			document.getElementById(theDate).setAttribute("className","day selected");
		}
	} else {
		if(document.getElementById(selectedDate).getAttribute('class') == "day today") {
			document.getElementById(selectedDate).setAttribute("class","day today");
		} else {
			document.getElementById(selectedDate).setAttribute("class","day");
		}
		if(document.getElementById(theDate).getAttribute('class') != "day today") {
			document.getElementById(theDate).setAttribute("class","day selected");
		}
	}
	selectedDate = theDate;
	selectedDateParts = selectedDate.split("-");
	calendarUpdateListHeader();
	calendarGetEvents(calendarEventType);
}

//------------------------------
function calendarSelectEvent(id) {
	var ajx = new ajax;
	ajx.open('get', 'includes/sitedata.php?action=event&id=' + id, true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			if(ajx.responseText != 'bad') {
				document.getElementById('eventDetails').innerHTML = ajx.responseText;
			} else {
				alert("blah!");
			}
		}

	}
	ajx.send(null);
}

//------------------------------
function writeCalendarSections() {
	document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"670\" height=\"250\" id=\"calendarSections\" align=\"middle\">");
		document.write("<param name=\"allowScriptAccess\" value=\"sameDomain\" />");
		document.write("<param name=\"movie\" value=\"flash/calendarSections.swf\" /><param name=\"quality\" value=\"high\" />");
		document.write("<param name=\"bgcolor\" value=\"#ffffff\" />");
		document.write("<param name=\"wmode\" value=\"transparent\" />");
		document.write("<embed src=\"flash/calendarSections.swf\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#ffffff\" width=\"670\" height=\"250\" name=\"calendarSections\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />");
	document.write("</object>");
}

//------------------------------
function writeCalendarSectionsDancer() {
	document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"200\" height=\"275\" id=\"calendar_dancer\" align=\"middle\">");
		document.write("<param name=\"allowScriptAccess\" value=\"sameDomain\" />");
		document.write("<param name=\"movie\" value=\"flash/calendar_dancer.swf\" />");
		document.write("<param name=\"quality\" value=\"high\" />");
		document.write("<param name=\"bgcolor\" value=\"#ffffff\" />");
		document.write("<param name=\"wmode\" value=\"transparent\" />");
		document.write("<embed src=\"flash/calendar_dancer.swf\" wmode=\"transparent\" quality=\"high\" bgcolor=\"#ffffff\" width=\"200\" height=\"275\" name=\"calendar_dancer\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />");
	document.write("</object>");
}

//------------------------------
function pullSto(section)	{
	$("comunidad").src = "images/tabs/comunidad.gif";
	$("deportes").src = "images/tabs/deportes.gif";
	$("vida").src = "images/tabs/vida.gif";
	$("opinion").src = "images/tabs/opinion.gif";
	$("nacion").src = "images/tabs/nacion.gif";
	$(section).src =  "images/tabs/" + section + "_selected.gif";
	
	$("storyList").innerHTML = "<div class=\"loadingIndicator\"><img src='images/indicator.gif'> Buscando Articulos...</div>";
	var ajx = new ajax;
	ajx.open('get', 'includes/ajax.php?action=pullstor&section=' + section + '&random='+Math.random(10000), true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			
			var stories = ajx.responseText.split("|");
			$("storyList").innerHTML = '';	
			
			for(i=0;i<stories.length-1;i++) {
				$("storyList").innerHTML += stories[i];
				var headlinecomp = stories[i+1].split("^");
				//headlinesmall(headlinecomp[0], headlinecomp[1], headlinecomp[2]);
				document.getElementById('head_' + headlinecomp[1]).innerHTML = '<a class="articleLink" href="articles.php?id=' + headlinecomp[1] + '"><span class="title">' + headlinecomp[0] + '</span><br /><span class="subTitle">' + headlinecomp[2] + '</span></a>';
				i++;
			}
			
			//document.getElementById("articlepre").innerHTML = ajx.responseText;
		}

	}
	ajx.send(null);
}
//--------------------------------------
function underConstruction() {
	alert("En construcción");
}