
$(document).ready(function(){
	Ep_init();
	if(typeof Ep_init_extra == 'function') {
		Ep_init_extra();
	}
});
window.onload = function(){
	Ep_onload();
	if(typeof Ep_onload_extra == 'function') {
		Ep_onload_extra();
	}
};

function Ep_init()
{

	var General = {
		setHandlers : function() {
			if ($('#statusMessage')) {
				$('#statusMessage').animate({'opacity':0.5}, 300);  
				$('#statusMessage').animate({'opacity':1}, 300);
				$('#statusMessage').animate({'opacity':0.5}, 100);  
				$('#statusMessage').animate({'opacity':1}, 100); 
				$('#statusMessage').animate({'opacity':0.5}, 100);  
				$('#statusMessage').animate({'opacity':1}, 100);
				setTimeout("hide('#statusMessage')", 10000);
			}
			if ($('.hpAlert')) {
				$('.hpAlert').animate({'opacity':0.5}, 300);  
				$('.hpAlert').animate({'opacity':1}, 300);
				$('.hpAlert').animate({'opacity':0.5}, 100);  
				$('.hpAlert').animate({'opacity':1}, 100); 
				$('.hpAlert').animate({'opacity':0.5}, 100);  
				$('.hpAlert').animate({'opacity':1}, 100);
			}
		}
	}
	$('img#tresize').click(function() { 
    	$('#tutorial .tcontent').slideToggle(300, function() {
			if($('#tutorial .tcontent').is(':visible')) {
				$('img#tresize').attr('src', '/images/magnifier_zoom_out.png');
			} else {
				$('img#tresize').attr('src', '/images/magnifier_zoom_in.png');
			}
		});
	});


	$('a[rel=flowplayer]').each(function() {
		var id = $(this).attr('id');
		flowplayer(id, '/swf/flowplayer-3.1.1.swf',	{
			clip:  { 
				autoPlay: false, 
				autoBuffering: false
			}
		});
	});
	$('div.preview').each(function() {
		var url = $(this).html();
		var loader = '<img class="loadingimg" border="0" /><br /><p>Loading preview...</p>';
		$(this).html(loader);
		$('img.loadingimg').attr('src', '/images/loader_big.gif');
		var target = $(this);
		$.get('/ajax/urlpreview?url=' + url, function(data) {
			target.html(data);
		});
	});
	$('span.countdown').each(function() {
		var endtime = $(this).html();
		$(this).countdown({
			until:  endtime,
			format: 'dhmS',
			layout: '{d<}{dn} days {d>}{h<}{hn} hours {h>}{m<}{mn} mins {m>}{sn} secs',
			expiryUrl: self.location.href
		});
	});
}

function Ep_onload()
{
	// Preload the loading image
	loader = new Image();
	loader.src = '/images/loader_big.gif';
}

function show(obj)
{
	$(obj).animate({ height: 'show', opacity: 1 }, 'slow');
}
function hide(obj)
{
	$(obj).animate({ height: 'hide', opacity: 0 }, 'slow');
}
function quickshow(obj)
{
	$(obj).animate({ height: 'show' }, 'fast');
}
function quickhide(obj)
{
	$(obj).hide();
}
function vslidein(obj)
{
	$(obj).show();
}
function vslideout(obj)
{
	$(obj).animate({ height: 'hide' }, 'slow');
}
function imgreload(obj, file)
{
	document.getElementById(obj).src = file + '?' + microtime();
}
function microtime()
{
	var now = new Date().getTime() / 1000;
    var s = parseInt(now);
    return (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
function showhide(obj)
{
	$(obj).slideToggle(300);
}
function setVal(obj, data)
{
	document.getElementById(obj).value = data;
}
function setBg(obj, data)
{
	document.getElementById(obj).style.background = '#' + data;
}
function setCol(obj, data)
{
	document.getElementById(obj).style.color = '#' + data;
}
function enableSort(obj)
{
	$(obj).sortable({
		items: '.sortableitem',
		opacity: 0.75,
		placeholder: 'placeholder',
		tolerance: 'pointer'
	});
	$(obj).disableSelection();
	$(obj).sortable('enable');
}
function disableSort(obj)
{
	$(obj).sortable('disable');
}
function saveSort(obj, url, hideobj, showobj)
{
	var order = $(obj).sortable('serialize');
	$.post(url, order, function(){
		// Hide save animation after 1 second and reveal the button
		setTimeout("quickhide('" + hideobj + "')", 1000);
		setTimeout("quickshow('" + showobj + "')", 1000);
	});
}
function ajaxreplace(url, obj)
{
	$.get(url, function(data){
		$(obj).html(data);
	});
}
function centerObject(obj)
{
	winheight = $(window).height();
	winwidth = $(window).width();
	objheight = obj.height();
	objwidth = obj.width();
	if (self.pageYOffset) {
		scrollTop = self.pageYOffset;
		scrollLeft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrollTop = document.documentElement.scrollTop;
		scrollLeft = document.documentElement.scrollLeft;
	} else if (document.body) {
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;	
	}
	obj.css({
			 'top': ((winheight - objheight) / 2) + scrollTop,
			 'left': ((winwidth - objwidth) / 2) + scrollLeft
			});
}
function playVideo(file)
{
	var path = '/attachments/' + file;
	var code = '<a id="vplink" href="' + path + '" style="display: block; width: 640px; height: 480px;"></a>';
	$('#videoplayer').html(code);
	flowplayer('vplink', '/swf/flowplayer-3.1.1.swf', {
		clip:  { 
			autoPlay: true, 
			autoBuffering: true
		}
	});
	// Hack for iframed versions
	if(top !== self) {
		if(typeof resizeIframe == 'function') {
			var pos = $('#footer').position();
			resizeIframe(950, pos.top + 80);
		}
	}
}