/**
 * Includes a required Javascript file dynamically
 */
function include_js(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

// do includes
include_js('/js/lib/class.PopupWindow.js');

// some common popup configurations
var POPUP_GENERIC = {
}
var POPUP_ACCESSORIES = {
	width:			745,
	height:			416,
	id:				'popup_accessories',
	deployLayer:	true,
	centered:		false,
	flashBG:		'#000'
}
var POPUP_WESBANK = {
	width:			740,
	height:			580,
	title:			'Volkswagen Instant Finance',
	deployLayer:	false,
	scrollbars:		true
}
var POPUP_360 = {
	width:			440,
	height:			296,
	left:			570,
	top:			250,
	id:				'popup_ext360',
	deployLayer:	false,
	centered:		false,
	flashBG:		'#fff'
}

// Site-wide onload handler
function global_onload()
{
	processRemoteLinks();
	activatePopups();
	fixRoundedCorners();
}

/**
 * Finds all links in the page with rel="popup" and activates their
 * on mousedown event to trigger a popup via the PopupWindow class
 */
function activatePopups() {
	var els = $A(document.getElementsByClassName('.popup'));
	els.each(function(el) {
		var popupConfig = POPUP_GENERIC;
		if (Element.hasClassName(el, 'wesbank')) 		popupConfig = POPUP_WESBANK;
		if (Element.hasClassName(el, 'accessories'))	popupConfig = POPUP_ACCESSORIES;
		if (Element.hasClassName(el, '360')) 		popupConfig = POPUP_360;
		var _processAnchor = function(e)
		{
			Event.element(e).onclick = function() {return false;}
			popupConfig.url = this.href;
			popupConfig.title = this.title;
			var p = new PopupWindow(popupConfig);
			p.open();
		}
		Event.observe(el, 'mousedown', _processAnchor.bindAsEventListener(el));
	});
}

/**
 * Injects non-semantic div elements into elements with .rb classnames 
 * to allow for CSS-controlled rounded corners
 */
function fixRoundedCorners() {
	var els = $A(document.getElementsByClassName('rb'));
	els.each(function(el) {
			// remove any padding
			el.style.padding = '0';
			
			// top right element
			var tr = document.createElement('div');
			Element.addClassName(tr, 'rbtop');
			
			// top left element
			var tl = document.createElement('div');
			tr.appendChild(tl);
			
			// bottom right element
			var br = document.createElement('div');
			Element.addClassName(br, 'rbbot');
			
			// bottom left element
			var bl = document.createElement('div');
			br.appendChild(bl);
			
			// content
			var c = document.createElement('div');
			Element.addClassName(c, 'rbcontent');
			// move nodes from the existing element into the new content element
			while (el.childNodes.length > 0) {
				c.appendChild(el.removeChild(el.firstChild));
			}
			
			// inject new elements into the host
			el.appendChild(tr);
			el.appendChild(c);
			el.appendChild(br);
		}
	);
}

/**
 * Loops through all anchors in the document and if remote links are found,
 * sets target="_blank" on these links
 */
function processRemoteLinks() {
	var hostname = getHostname();
	var links = $A(document.getElementsByTagName('a'));
	links.each(function(obj) {
		// if the link points to an external site, set the target to '_blank'
		var href = obj.href;
		if (href.indexOf('javascript:') != 0 
			&& href.indexOf(hostname) != 0 
			&& Element.hasClassName(obj, 'popup') == false) {
			obj.target = '_blank';
		}
		
	})
}

// returns the current site hostname
function getHostname() {
	var loc = document.location;
	var regex = new RegExp('(http://[^\/]+)\/?');
	var result = regex.exec(loc);
	if (result[1]) {
		return result[1]
	} else {
		return false;
	}
}

// attach events
document.observe('dom:loaded', global_onload);
