/**
 * Public Creative
 * Common JavaScript functions
 * @author Tim Whitlock
 * @version $Id: common.js,v 1.1.2.2.2.2 2009/01/29 18:59:04 tim Exp $
 */


/**
 * Puts a form element under JS control
 */
function JSForm( formId ){ 
	this.elForm = document.getElementById(formId);
	if( ! this.elForm ){
		throw new Error('Failed to get form '+formId );
	}
}


/**
 * Appends a rollover submit link to the end of the form
 */
JSForm.prototype.appendSubmitLink = function( label, id ){
	var elForm = this.elForm;
	var elParent = id ? document.getElementById(id) : elForm;
	var elLink = document.createElement('a');
	elLink.onclick = function(){ elForm.submit(); return false; };
	elLink.href = 'javascript:void(0);';
	elLink.className = 'submit';
	elLink.appendChild( document.createTextNode( label ) );
	elParent.appendChild( elLink );
}


/**
 * Makes magic tip text appear in text fields
 * @todo append class when dull/active?
 */
JSForm.prototype.convertTextTips = function(){
	var fields = this.elForm.getElementsByTagName('input');
	for( var i = 0; i < fields.length; i++ ){
		var elField = fields[i];
		if( elField.type !== 'text' ){
			continue;
		}
		var title = elField.getAttribute('title');
		if( ! title ){
			continue;
		}
		if( ! elField.value ){
			elField.value = title;
		}
		elField.onfocus = function(){
			if( this.value === this.getAttribute('title') ){
				this.value= '';
			}
		}
		elField.onblur = function(){
			if( this.value === '' ){
				this.value= this.getAttribute('title');
			}
		}
	}
}





/**
 * IE-safe innerHTML setter
 */
function setElementHTMLById( id, html ){
	try {
		var El = document.getElementById(id);
		if( El.attachEvent ){
			var elTmp = document.createElement('div');
			elTmp.innerHTML = html;
			El.innerHTML = '';
			for( var i = 0; i < elTmp.childNodes.length; i++ ){
				El.appendChild( elTmp.childNodes[i] );
			}
		}
		else {
			El.innerHTML = html;
		}
	}
	catch( Er ){
		return false;
	}
	return El.innerHTML;
}


/**
 * Menu initializer
 */
function initMenu( id ){
	var path = document.location.pathname;
	var i = path.indexOf('/admin/preview.php');
	if( i !== -1 ){
		path = path.substr(18);
	}
	var prefix =  document.location.protocol+'//'+ document.location.host;
	var elUl = document.getElementById(id);
	var items = elUl.getElementsByTagName('li');
	for( var p, i = 0; i < items.length; i++ ){
		var elLi = items[i];
		// add rollover/rollout
		elLi.onmouseover = function(){
			addElementClassName( this, 'over' );
		}
		elLi.onmouseout = function(){
			removeElementClassName( this, 'over' );
		}
		// highlight active element
		var elLink = elLi.childNodes[0];
		if( ! elLink || ! elLink.href ){
			continue;
		}
		p = elLink.href;
		// get path from link as it probably has full URL
		if( p.indexOf(prefix) === 0 ){
			p = p.split(prefix).pop().split('#').shift();
		}
		if( p.indexOf('/') !== 0 ){
			// off-site link; should already have trackExt inline
			continue;
		}
		// if the links path begins the pages path, then it is active
		if( path.indexOf(p) === 0 ){
			// check special case of home page, because everything is under the home page
			if( /^\/[a-z]{0,2}$/.test(p) && p !== path ){
				continue;
			}
			addElementClassName( elLi, 'active' );
		}
	}
}


/**
 * @return bool whether class needed to be added
 */
function addElementClassName( El, c ){
	if( El.className.indexOf(c) !== -1 ){
		return false;
	}
	El.className = El.className ? El.className+' '+c : c;
	return true;
}


/**
 * @return bool whether class needed to be removed
 */
function removeElementClassName( El, c ){
	if( El.className.indexOf(c) === -1 ){
		return false;
	}
	var Reg = new RegExp(' ?'+c, 'gi');
	El.className = El.className.replace(Reg,'');
	return true;
}



/**
 * Initialize the send to a friend form. 
 */
var sendFriendPanel;
function initSendFriendForm( divId, formId ) {
	sendFriendPanel = document.getElementById(divId);
	// keep panel open if "sendfriend" anchor present
	if( location.hash === '#sendfriend' ){
		null;
	}
	// else hide panel until activated
	else {
		sendFriendPanel.style.display = 'none';
	}
}



/**
 * Open the send to friend panel
 */
function openSendFriend(){
	sendFriendPanel.style.display = 'block';
}




/** ---------------  Extra GA calls ------------------------- */
function trackExt( elA ){
	try {
		var href = typeof elA === 'object' && elA.href ? elA.href : elA.toString();
		pageTracker._trackPageview( '/external/' + href );
	}
	catch( Er ){
		// ignore
	}
	return true;
}	








































