// common.js : global javascript functions and jquery variables.

//
// IE css button flicker bug
//
ie = document.all;
if(ie) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

//
// display hint to id/password textboxes
//
$(document).ready(function(){

	// add hints to login boxes
	var ua = navigator.userAgent.toLowerCase();

	if (document.loginform) {
		if(!((ua.indexOf('konqueror')!=-1) && (document.all || (ua.indexOf('khtml/3.4')!=-1))) 
		   && !(((ua.indexOf('safari')!=-1) && !window.print) || (document.defaultCharset && !window.print))) {
			// Set the third value to the text you want to appear in the field.
			changeInputType(document.loginform.password,'text','password...',false,true);
		}
	}
	
	$('input.username').hint();
	//$('input.password').addClass('blur');
	
	// detect screen resolution and change footer font size
	if ((screen.width <= 1024) || (screen.height <= 768)) {
		$('#footer ul.links li.divider').css ({
			'margin': '6px 10px 0 0',
			'padding-right': '10px',
			'*padding-right': '8px'
		});
	}
});

//
// changeInputType function
//
changeInputType = function(
	oldElm, // a reference to the input element
	iType, // value of the type property: 'text' or 'password'
	iValue, // the default value, set to 'password' in the demo
	blankValue, // true if the value should be empty, false otherwise
	noFocus) {  // set to true if the element should not be given focus

	if(!oldElm || !oldElm.parentNode || (iType.length<4) || !document.getElementById || !document.createElement) 
		return;
		
	var newElm = document.createElement('input');
	newElm.type = iType;
	
	if(oldElm.name) newElm.name = oldElm.name;
	if(oldElm.id) newElm.id = oldElm.id;
	if(oldElm.className) newElm.className = oldElm.className;
	if(oldElm.size) newElm.size = oldElm.size;
	if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
	if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
	
	newElm.onfocus = function(){
		return function(){
			if(this.hasFocus) 
				return;
			var newElm = changeInputType(this,'password',iValue,(this.value.toLowerCase()==iValue.toLowerCase())?true:false);
			if(newElm) 
				newElm.hasFocus=true;
		}}();

	newElm.onblur = function(){
		return function(){
			if(this.hasFocus)
				if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
					changeInputType(this,'text',iValue,false,true);
				}
		}}();
		
	// hasFocus is to prevent a loop where onfocus is triggered over and over again
	newElm.hasFocus=false;
	oldElm.parentNode.replaceChild(newElm,oldElm);
	if(!blankValue) newElm.value = iValue;
	if(!noFocus || typeof(noFocus)=='undefined') {
		window.tempElm = newElm;
		setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
	}
	return newElm;
}

//
// jQuery function to display textbox hints
//
jQuery.fn.hint = function () {
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this); 
    // get it once since it won't change
    var title = t.attr('title'); 
    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(title);
          t.addClass('blur');
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
          t.removeClass('blur');
        }
      });

      // clear the pre-defined text when form is submitted
      t.parents('form:first()').submit(function(){
          if (t.val() == title) {
              t.val('');
              t.removeClass('blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
}

//
// submitEnter function : to let a user use 'enter' key on the login box
//
function submitEnter(myfield,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13) {
		document.loginform.submit();
		return false;
	} else
		return true;
}