/*
* Javascript implementations for Planet website
* (c) Papercut Media. No reproduction without permission. Feel free to ask.
* Author: Patrick Pittman
* Date: September 2009
* Version: 2.0
*/

var baseHref = "http://www.planetvideo.com.au/";
var ajaxBase = baseHref + "js/ajax/";

	function Planet() {
		this.loaderlock = false;
		this.baseHref = "http://www.planetvideo.com.au/";
		this.ajaxBase = this.baseHref+"js/ajax/";
		this.ajaxProgress = new Image();
		this.ajaxProgress.src = this.baseHref+"images/icons/ajax-loader.gif";
	}

	Planet.prototype.init = function(e) {
		
			Element.extend(document.body);
						
			$$('a.confirm').each(function(element) {
					element.observe('click',function(e) {
						Event.stop(e);
						if (confirm("Are you sure you wish to do this?")) {
							document.location.href = element.href;
						}
					});
			});
			
			if ($('sectionImage')) {
				
				if (!document.body.hasClassName('editmode')) {
					this.setupSectionImage();
					url = ajaxBase+'getSectImage/rand/';
					this.slide = new Ajax.PeriodicalUpdater('sectionImageContainer', url,
					{	
						frequency: 10
					});
				}

			}
				
				
			if ($('searchwords')) {
				this.defKeyword = 'enter keywords';
				if (!$('searchwords').getValue()) {
					$('searchwords').value=this.defKeyword;					
				}
				Event.observe($('searchwords'),'focus',function(e) {
					element = Event.element(e);
					if (element.value==planet.defKeyword) {
						element.value = '';
					}
				});
				Event.observe($('searchwords'),'blur',function(e) {
					element = Event.element(e);
					if (element.value=='') {
						element.value = planet.defKeyword;
					}
				});			
			}	
			
			if ($('searchform')) {
			
				Event.observe($('searchform'),'submit',function(e) {
					Event.stop(e);
					$('searchform').getInputs('text').each(function(element) {
						planet.keywords = element.getValue();
					})
					if ($('searchDept').getValue() == 'all') {
						planet.searchLoc = "search";
					} else {
						planet.searchLoc = "library/"+$('searchDept').getValue()+"/search";
					}
					$('searchform').action = planet.baseHref+planet.searchLoc;
					$('searchform').submit();
				});
			}

			
			this.loginSetup();
			

	}


	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];
	}

	Planet.prototype.setupSectionImage = function() {
			$('sectionImageContainer').observe('click',function(e) {
				
				thisElement = e.findElement('A');
				if (thisElement && thisElement.hasClassName('paginate')) {
					e.stop();
					if (planet.slide) {
						planet.slide.stop();						
					}
					thisItem = getURLVal('SectionImages%5Bstart%5D',thisElement.href);
					req = ajaxBase+'getSectImage/'+parseInt(thisItem);
					new Ajax.Updater('sectionImageContainer', req);								
				}
			});
			
	}


	/*
	This is a lot of code just to switch out values in a couple of forms, but we need to change the password field's
	input type from 'text' to 'password'. IE doesn't let you do this on existing elements, so instead we have to
	destroy and recreate the element and rebind event listeners -- also have to make sure we unbind listeners before
	destruction or IE complains about type mismatches. Ouch, thanks MS.
	*/

	Planet.prototype.loginSetup = function() {
		if ($('username')) {
			Event.observe($('username'),'focus',function(e) {
				element = Event.element(e);
				if(element.value=='username') {
					element.value='';
				}
			});

			Event.observe($('username'),'blur',function(e) {
				element = Event.element(e);
				if(element.value=='') {
					element.value='username';
				}
			});	

			this.fPass = this.focusPass.bindAsEventListener(this);
			this.bPass = this.blurPass.bindAsEventListener(this);

			$('password').remove();
			element = $('loginForm').insertBefore(Builder.node('input',{type: 'text', value: 'password', name: 'password', id: 'password',className: 'password'}), $('login'));
			Event.observe(element,'focus',this.fPass);			
		}

	//	element.blur();
	}

	Planet.prototype.focusPass = function(e) {

		element = Event.element(e);
        if(element.type=='text') {
				Event.stopObserving(element,'focus',this.fPass);
                element.remove();
				element = $('loginForm').insertBefore(Builder.node('input',{type: 'password', name: 'password', id: 'password',className: 'password'}), $('login'));	
				Event.observe(element,'blur',this.blurPass);  
				element.focus();      
			}		
	}

	Planet.prototype.blurPass = function(e) {
		element = Event.element(e);
		
        if(element.type=='password' && !(element.value)) {
				Event.stopObserving(element,'blur',this.bPass);
                element.remove();
				element = $('loginForm').insertBefore(Builder.node('input',{type: 'text', value: 'password', name: 'password', id: 'password',className: 'password'}), $('login'));	
				Event.observe(element,'focus',this.fPass);
        }
	}


function initPapercut() {
	planet.init();
}
var planet = new Planet();

document.observe('dom:loaded',initPapercut);
