// *** start: handle popup links ***

function doPopups()
{
	if (document.getElementById('my_popup_link')) //example
	{
		var my_popup_link = document.getElementById('my_popup_link');
		var my_popup_link_Href = my_popup_link.getAttribute('href');
		my_popup_link.onclick = function()
		{
			return(popuplink(my_popup_link_Href, this, 600, 400, false));
		}
	}

}

// *** end: handle popup links ***



// *** start: popup window ***

// usage: popuplink(['js-only url',] this[, w[, h[, scroll[, extras]]]])
// basic usage: <a href="popup.html" target="_blank" onclick="return(popuplink(this));">new pop</a>
// advanced usage: <a href="popup_nojs.html" target="_blank" onclick="return(popuplink('popup_yesjs.html', this, 200, 100, false));">new pop</a>
// site-wide defaults:
popup_w = 400;
popup_h = 300;
popup_scroll = true;
popup_extras = 'location=0,statusbar=0,menubar=0';
function popuplink() {
	var undef, i=0, args=popuplink.arguments;
	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');
	var target = args[i++].getAttribute('target') || '_blank';
	var w = args[i++];
	var h = args[i++];
	var s = (args[i]===undef) ? popup_scroll : args[i++];
	var features = 'width=' + (w || popup_w)
				 + ',height=' + (h || popup_h)
				 + ',scrollbars=' + (s ? 'yes,' : 'no,')
				 + (args[i] || popup_extras);
	var win = window.open(url, target, features);
	win.focus();
	return false;
}

// *** end: popup window ***


/*********************************************************************************************************
In Place Input Label - used to place label text into an input field and clear it again when it is selected
*********************************************************************************************************/

var InPlaceInputLabel = Class.create();
InPlaceInputLabel.prototype = {
	initialize: function(sInputID) {
		this.input = $(sInputID);
		this.lbl = $$('label[for='+ sInputID +']')[0];
		if (this.input && this.lbl) {
			this.lbl.addClassName('overlay');
			this.input.setAttribute('label',this.lbl.innerHTML.strip());
			if (this.input.getAttribute('type') == 'password') {
				this.ispassword = true;
				this.input.setAttribute('ispassword','true');
				this.input.setAttribute('type','text');
			}
			this.form = this.input.up('form');
			
			Event.observe(this.input,'focus',this.__Focus.bindAsEventListener(this));
			Event.observe(this.input,'blur',this.__Blur.bindAsEventListener(this));
			Event.observe(this.form,'submit',this.__Submit.bindAsEventListener(this));

	    	this.updateLabel();

		}
	},
	/**
	 * Hides the 'label' text for the field, if the text has not changed
	 */
	hideLabel: function() {
		if (this.input.value.strip() == this.input.getAttribute('label')) {
			this.input.value = ''; // removes label text
			this.input.removeClassName('label');
			if (this.ispassword) {
				this.input.setAttribute('type','password');
			}
		}
	},
	/**
	 * Sets the value of the label to the LABEL text, if no othe value is in place.
	 */
	updateLabel: function() {
	    if (this.input.value.strip().length == 0) {
			this.input.value = this.input.getAttribute('label');
			this.input.addClassName('label');
			if (this.ispassword) {
				this.input.setAttribute('type','text');
			}
	    }
	},
	__Focus: function(e) {
		this.hideLabel();
	},
	__Blur: function(e) {
		this.updateLabel();
	},
	__Submit: function(e) {
		this.hideLabel();
	}
}

/*************************************************************************************
Form Enter-catch - used to submit forms on enter, despite a global form-runat="server"
*************************************************************************************/

function checkEnter(e,buttonid){
	var characterCode;
	if(typeof buttonid == 'undefined') {
		return false;
	}
	if (!e) { var e = window.event; }
	if (e.keyCode) { 
		characterCode = e.keyCode;
	} else if (e.which) {
		characterCode = e.which;
	}
	if (characterCode == 13) {
		var temp = document.getElementById(buttonid);
		if (temp) { 
			temp.click();
		}
		return false;
	} else {
		return true;
	}
}

function FixHeight(floatedElement, stretchedElement) {
	if((typeof floatedElement == 'undefined')||(typeof stretchedElement == 'undefined')) {
		return false;
	}
	if (!e) { var e = window.event; }
	
	var floater = document.getElementById(floatedElement);
	if (!floater) {
		floater = $$("." + floatedElement)[0];
	}
	
	var stretcher = document.getElementById(stretchedElement);
	if (!stretcher) {
		stretcher = $$("." + stretchedElement)[0];
	}
	
	var floaterHeight = floater.getHeight();
	
	stretcher.style.height = floaterHeight + 'px';
}


// *** start: global load events ***
Event.observe(window, "load", function(){
	var labelPlaceFormSearch = new InPlaceInputLabel('quick_search');
});
// *** end: global load events ***
