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


if(typeof(console) == 'undefined') {var console = {info:function(mes) {}}}




function toggleOpenID() {
	if($('openid').style.display == 'none') {
		$('openid').show();
		$('normal').hide();
	} else {
		$('openid').hide();
		$('normal').show();
	}	
}




function simulatePing(pHash) {
	new wl_WorldLogger(pHash, {'ping_location':Settings.ping_location});
}

function selectText(el)
{

	el.focus();
	el.select();
}



function showNotice(pMessage) {
	
}

var moviePopup;
function showMoviePopup() {
	if(moviePopup == null)
		moviePopup = new MoviePopup();
	moviePopup.toggle();
}


var PopupWindow = Class.create({
	initialize:function() {
		this.holder = new Element('div', {'class':'popup', 'style':'display:none;'})
		this.overlay = new Element('div', {'class':'overlay'})
		this.container = new Element('div', {'class':'container'});
		this.menu = new Element('div', {'class':'menu'});
		this.closeButton = new Element('a', {'href':'javascript:void(0)'}).update('(close)');
		this.menu.appendChild(this.closeButton)
		this.closeButton.observe('click', this.close.bind(this));
		
		this.content = new Element('div', {'class':'content'});
		
		this.container.appendChild(this.menu);
		this.container.appendChild(this.content);
		this.active = false;
		this.overlay.appendChild(this.container);
		this.holder.appendChild(this.overlay);
	},
	
	open:function() {
		window.document.body.appendChild(this.holder);
		this.active = true;
		this.holder.show();
	},
	close:function() {
		this.holder.hide();
		window.document.body.removeChild(this.holder);
		this.active = false;
		
	},
	toggle:function() {
		if(!this.active) {
			this.open();
		} else {
			this.close();
		}
	}
	
});


var MoviePopup = Class.create(PopupWindow, {
	initialize:function($super) {
		$super();

		this.content.update('<object width="500" height="400"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=10923404&amp;server=vimeo.com&amp;show_title=0&amp;autoplay=1&amp;show_byline=0&amp;show_portrait=0&amp;color=ffee00&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=10923404&amp;server=vimeo.com&amp;show_title=0&amp;autoplay=1&amp;show_byline=0&amp;show_portrait=0&amp;color=ffee00&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="500" height="400"></embed></object>');
	}
})





function getLabels(pLabels, pAmt) {
	var lRealLabels = [];
	var lIndexesPerLabel = Math.floor(pLabels.length / pAmt);
	for(var i = 0; i < pLabels.length; i++) {
		if(i % lIndexesPerLabel == 0) {
			lRealLabels.push(pLabels[i]);
		} else {
			lRealLabels.push('')
		}
	}
	return lRealLabels;
}

function drawGraph(holder, lValues, lLabels) {
	new Grafico.AreaGraph(holder, { revenue: lValues },
	{
	  grid :                  true,
	  plot_padding :          0,
	  curve_amount :          0,
	  area_opacity : 					0.5,

	  start_at_zero :         true,
	  stroke_width :          3,
	  label_rotation :        0,
	  font_size :             11,

	  colors :                {revenue: '#00aeff'},
	  background_color :      "#fff",
	  label_color :           "#444",
	  grid_color :            "#ccf",
	  markers :               "value",

	  draw_axis :             false,
	  labels :                getLabels(lLabels, 4),
	  hide_empty_label_grid : true,
	  datalabels :            {revenue: function(idx, value) { return lLabels[idx]}.bind(this)},
	});
}




function prettyDate(date){
	//var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}



function preloadImage(pUrl) {
	var el = document.createElement('img');
	el.setAttribute('src', pUrl);
	el.setAttribute('style', 'display:none');
	Event.observe(window, 'load', function() {
		document.body.appendChild(el);
	});
}
	

