var getURLVal = function(key, url, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	reg = "[\\?&]"+key+"=([^&#]*)";
  var regex = new RegExp(reg);

  if(url == null) url = window.location.href;
  var qs = regex.exec(url);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function Papercut() {
	this.loaderlock = false;
	this.baseHref = "http://www.sppt.asn.au/";
	this.ajaxBase = this.baseHref+"js/ajax/";
	this.activeLink = null;
}

Papercut.prototype.init = function(e) {
	this.suckerFish();
	if ($('frontProduction')) {
		this.setupFrontProductions();
	}

	if ($('MailingList_Email')) {
		var emailwords = "enter your email";
		if ($('MailingList_Email').value=='') {
			$('MailingList_Email').value = emailwords;
		}
		$('MailingList_Email').observe('focus',function(e){
			if (this.value==emailwords) {
				this.value = '';
			}
		});
		$('MailingList_Email').observe('blur',function(e){
			if (this.value=='') {
				this.value = emailwords;
			}
		});		
	}
	
}


Papercut.prototype.suckerFish = function() {
	
	
	$$("#mainNav li").each(function(el){
		el.observe('mouseover',function(e) {
			$$("#mainNav li").each(function(elm){
				if (elm.hasClassName('sfhover')) {
					elm.removeClassName('sfhover');				
				}
				if (elm.hasClassName('active')) {
					elm.removeClassName('active');
					Papercut.activeLink = elm;				
				}				
			});
			if (!this.hasClassName('sfhover')) {
					this.addClassName('sfhover');
			}
		});

		el.observe('mouseout',function(e) {
			if (this.hasClassName('sfhover')) {
				this.removeClassName('sfhover');				
			}
			if (Papercut.activeLink) {
				Papercut.activeLink.addClassName('active');
			};			
		});
	});
		/*
	$("mainNav").getElementsBySelector("li").each(
		function(el) {
			el.observe('mouseover',
				function(e) {
					element = Event.element(e);
					$$("#mainNav .active").each(function(el){
						el.removeClassName('active');
						Papercut.activeLink = el;
					});
					element.addClassName('sfhover');
				}
			);
			
			el.observe('mouseout',
				function(e) {
					element = Event.element(e);
					element.removeClassName('sfhover');
					if (Papercut.activeLink) {
						Papercut.activeLink.addClassName('active');
					};
				}
			);			
		}
	); */
}

Papercut.prototype.setupFrontProductions = function() {
		$$('#frontProduction .stepThrough a').each(function(el){
				el.observe('click',function(e){
					Event.stop(e);
					element = Event.element(e);
					thisItem = getURLVal('Productions%5Bstart%5D',element.href);
					new Ajax.Updater('frontProduction', Papercut.ajaxBase+'getProd/'+parseInt(thisItem),
					{	
						onComplete: 
							Papercut.setupFrontProductions
					});
			})
		});
}

function initPapercut() {
	Papercut.init();
}

var Papercut = new Papercut();
document.observe('dom:loaded',initPapercut);