// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var countdowns = [];

function resizeImage(id,maxwidth,i) {
	var pic=$(id);
	dimensions = pic.getDimensions();

	w=dimensions.width;
	h=dimensions.height;

	if ((w<=0 || h<=0) && i==0) {
		setTimeout('resizeImage("'+id+'",'+maxwidth+',1);',1000);
		return;
	}
	
	if (w > maxwidth) {
	  f=1-((w - maxwidth) / w);
	  pic.width=w * f;
	  pic.height=h * f;
	}
	pic.style.display = "inline";
}

function convertToHMS(seconds) {
	// returns DAYS if greater than 5 days. otherwise returns HMS
	
	if (seconds==0) {
		return "*ENDED*";
	}
	
	if (seconds<60) {
		return "Less than 1 minute";
	}
	
	hours = Math.floor(seconds/3600);
	minutes = Math.floor((seconds-hours*3600)/60);
	secs = seconds - hours*3600 - minutes * 60;

  timeLeft = "";

	if (hours) {
		timeLeft += hours + "h ";
	}
	if (minutes) {
		timeLeft += minutes + "m ";
	}

	timeLeft += secs + 's';
	
	days = Math.floor(hours/24);
	if (days >= 1) { 
		if (days == 1) {
		timeLeft = days + ' day';
	  }
	  else {
		timeLeft = days + ' days';
		}
	}
	
	return timeLeft;
}

function convertToDHMSfull(seconds) {
	s_in_d = 3600*24;
	s_in_h = 3600;
	s_in_m = 60;

	if (seconds==0) {
		return "Drawing closed.";
	}
	if (seconds<60) {
		return "Less than a minute remaining.";
	}

	days = Math.floor(seconds/s_in_d);
	seconds -= s_in_d * days;
	hours = Math.floor(seconds/s_in_h);
	seconds -= s_in_h * hours;
	minutes = Math.floor(seconds/s_in_m);
	seconds -= s_in_m * minutes;

  timeLeft = "Drawing ends in ";

	if (days) { 
		if (days==1) {
			timeLeft += days + " day ";
		}
		else {
			timeLeft += days + " days ";			
		}
	}
	
	if (hours) {
		if (hours==1) {
			timeLeft += hours + " hour ";
		}
		else {
			timeLeft += hours + " hours ";			
		}
	}

	if (minutes) {
		if (minutes==1) {
			timeLeft += minutes + " minute ";
		}
		else {
			timeLeft += minutes + " minutes ";			
		}
	}
	
	timeLeft += seconds + " seconds.";

  if(days > 1) {
		timeLeft = "Drawing ends in " + days + " days."
	}
	
	return timeLeft;
}

function clearCountdowns() {
	countdowns = [];
}

function addCountdown(name,seconds_left,type) {
	countdowns.push({'name':name,'seconds_left':seconds_left,'ctype':type});
}

function runCountdowns() {
	for (var i=0; i<countdowns.length; i++) {
    seconds = countdowns[i].seconds_left; 
		if (countdowns[i].ctype=="long") {
			timeLeft = convertToDHMSfull(seconds);
		}
		else if (countdowns[i].ctype=="short"){
			timeLeft = convertToHMS(seconds);
		}
		else {
			timeLeft = "unrecognized countdown type.";
		}
		countdowns[i].span.innerHTML = timeLeft;
		if (countdowns[i].seconds_left > 0) {
  		countdowns[i].seconds_left-=1; // knock off 1 second!
		}
		// another way to do it--project out what the end date is based on server seconds, and then go off that
	}
	setTimeout("runCountdowns()", 1000);
}

function startCountdowns(){
	for (var i=0; i<countdowns.length; i++) {
 	 countdowns[i].span = document.getElementById(countdowns[i].name);
	 countdowns[i].span.innerHTML = "";
	}	
	runCountdowns();
}


// ticket hover javascript code
function hideFrame() {
	document.getElementById("tooltip1").style.display = "none";
}	
function showFrame(obj,name,comment,site,id) {
	var pos = findPos(obj);
	var curleft = pos[0];
	var curtop = pos[1];
		
	$("held_by").innerHTML = name;
	$("site").innerHTML = site;
	$("ticket_id").innerHTML = id;

	if(comment) {
	$("comment").innerHTML = ": " + comment;
	}
	else {
		$("comment").innerHTML = "";		
	}
	moveTo(document.getElementById("tooltip1"),curleft+obj.getWidth()-12,curtop-Element.getHeight('tooltip1')+7);
	document.getElementById("tooltip1").style.display = "block";
}	
// from http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function moveTo(obj, left, top){
  obj.style.position = "absolute";
  obj.style.left = left + "px";
  obj.style.top = top + "px";
}

/* cookie management from http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(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 null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// css theme switchers
function switchThemeCSS(url){
	$('theme').href = url;
}

function cssThemeURL(id){
	var url = "/templates/themes/" + id + ".css";
	return url;
}

function switchToTheme(id){
	switchThemeCSS( cssThemeURL(id) );
}

function switchFrameCSS(url){
	// frame may not have loaded yet
	try{
		window.frames[0].document.getElementById("theme").href = url;	
		return true;
	}
	catch(e){
		return false;
	}
}

function switchFrameTheme(id){
	var url = cssThemeURL(id);
	switchFrameCSS(url);
	return url;
}
