Prototype.Browser.IE6 = (Prototype.Browser.IE && navigator.appVersion.indexOf("MSIE 6")!=-1);

var Global = Class.create();

Global.prototype = {
	applicationPath: '/',
	// Add elements to this array
	// to replace SIFR elements
	// Simply set the element and the SWF location
	// SWF file is relative to the root folder
	sifrElements: [
		{
			element: "#content h1",
			swf: "type.swf",
			colour: "#00AEF0",
			transparent: true
		},
		{
			element: "#home-toolbox h1",
			swf: "type.swf",
			colour: "#00AEF0",
			transparent: true
		}
	],

	pngElements: [
		'#header',
		'#header *',
		'#header form input',
		'#navigation li',
		'#core',
		'#sitemap',
		'#base a.boystown',
		'#home-toolbox',
		'#home-toolbox *',
		'div.support *',
		'strong.pinned',
		'#footer',
		'a.optus',
		'a.donate',
		'a.grownups',
		'a.name-dog',
		'#navigation ul li a',
		'#content h4.poster-competition a',
		'div.search-results div.your-search a'
	],

	initialize: function() {
		// Event listeners
		this.navMouseOver = this.onNavMouseOver.bindAsEventListener(this);
		this.navMouseOut = this.onNavMouseOut.bindAsEventListener(this);

		this.externaliseLinks();
		this.png();
		this.sifrReplacement();
		this.horizontalRules();
		this.navigation();
		this.defaultInputs();
		this.shadowBox();
	},

	shadowBox: function() {
		var print = $$('a.shadowbox-print');
		if (print.length>0) {
			print = print[0];
			Event.observe(print, 'click', function() { window.print(); });
		}
	},

	defaultInputs: function() {
		var elements = $$('input.default-value');
		for ( var x = 0 ; x < elements.length ; x++ ) {
			var input = elements[x];
			Event.observe(input, 'focus', this.onInputFocus.bindAsEventListener(this, input.value) );
			Event.observe(input, 'blur', this.onInputBlur.bindAsEventListener(this, input.value) );
		}
	},

	onInputFocus: function(e, value) {
		var element = Event.element(e);
		
		if (element.value == value) {
			element.removeClassName('default-value');
			element.value = '';
		}
	},

	onInputBlur: function(e, value) {
		var element = Event.element(e);

		if (element.value == '') {
			element.value = value;
			element.addClassName('default-value');
		}
	},

	onNavMouseOver: function(e) {
		var element = Event.element(e);
		if (element) {
			element.up().addClassName('selected');
		}
	},

	onNavMouseOut: function(e) {
		var element = Event.element(e);
		if (element) {
			element.up().removeClassName('selected');
		}
	},

	navigation: function() {
		if (Prototype.Browser.IE6) {
			var navlinks = $$('#navigation ul li a');
			for ( var x = 0 ; x < navlinks.length ; x++ ) {
				var link = navlinks[x];

				if (link) {
					if (!link.up().hasClassName('selected')) {
						Event.observe(link, 'mouseover', this.navMouseOver);
						Event.observe(link, 'mouseout', this.navMouseOut);
					}
				}
			}
		}
	},

	horizontalRules: function() {
		var hrs = $$('#content hr');
		for ( var x = 0 ; x < hrs.length; x++ ) {
			var hr = hrs[x];
			var div = document.createElement('div');
			div.className = 'hr';
			hr.parentNode.replaceChild(div, hr);
			div.appendChild(hr);
		}
	},

	png: function() {
		// Add the classname "png" to the necessary elements
		for ( var x = 0 ; x < this.pngElements.length ; x++ ) {
			var elements = $$(this.pngElements[x]);
			for ( var y = 0 ; y < elements.length ; y++ ) {
				elements[y].addClassName('png');
			}
		}

		if (typeof DD_belatedPNG != "undefined" && Prototype.Browser.IE6) {
			DD_belatedPNG.fix('.png');
		}
	},

	/* 
	This method replaces all necessary headings with a sIFR SWF 
	*/
	sifrReplacement: function() {
		if (typeof sIFR == "function") {
			for ( var x = 0 ; x < this.sifrElements.length ; x++ ) {
				sIFR.replaceElement(this.sifrElements[x].element,
					named(
						{
							sFlashSrc: this.getSiteType() + this.sifrElements[x].swf, 
							sColor: this.sifrElements[x].colour,
							sWmode: (this.sifrElements[x].transparent ? "transparent" : "")
						}
					)
				);
			}
		}
	},

	/* 
	This method finds all anchor links with the rel="external" and sets the target to _blank 
	*/
	externaliseLinks: function() {
		var links = $$("a"); 
		for ( var x=0; x<links.length; x++ ) { 
			var link = links[x];
			if (link.getAttribute("href")) {
				var rels = link.getAttribute("rel");
				if (rels != null) {
					rels = rels.split(" ");
					for (var i=0; i<rels.length; i++) {
						if (rels[i] == "external") {
							link.target = "_blank"; 
							break;
						}
					}
				}
			}
		} 
	},

	getSiteType: function() {
		var url = URL.parse(location.href);
		
		if (url.directory.startsWith(this.applicationPath + 'teens/')) {
			return this.applicationPath + 'teens/';
		}

		if (url.directory.startsWith(this.applicationPath + 'kids/')) {
			return this.applicationPath + 'kids/';
		}

		if (url.directory.startsWith(this.applicationPath + 'grownups/')) {
			return this.applicationPath + 'grownups/';
		}

		return this.applicationPath;
	}
};


//Event.observe(window, 'load', function() { 
document.observe("dom:loaded", function() {
	var global = new Global();

	if (typeof(Shadowbox) != "undefined") {
		Shadowbox.init();
	}
});