
var Accordian = Class.create();
Accordian.prototype = {
	initialize : function(element){


	    this.element=$(element);
	    this.parent=$(parent);

	    if (this.element != null){

		    $A($$('#'+this.element.id+' .accordian')).each(function(node){
		        if(!document.all) Element.cleanWhitespace(node);
		        if(!Element.hasClassName(node, 'open')) Element.hide(node.lastChild);
		        if(Element.hasClassName(node, 'open')) this.active = node;
		        Event.observe(node.firstChild, 'click', function(event){ Event.stop(event); this.toggle(node); }.bind(this), false);
		    }.bind(this));
		}

	},
	toggle : function(element){
	    if(element!=this.active){
	    		if(this.active){
	    	    new Effect.Parallel([
  	              new Effect.BlindUp(this.active.lastChild),
    	            new Effect.BlindDown(element.lastChild)
      	      ], {
        	        duration: 0.5
          	  });
          	}else{
	          	new Effect.BlindDown(element.lastChild, {duration: 0.5});
          	}
          	Element.removeClassName(this.active, "open");
          	Element.addClassName(element, "open");
            this.active=element;
        }
	}
}

ToolTip = Class.create();
ToolTip.prototype = {
	initialize : function(element, options){
		this.element = $(element);
		this.id = this.element.id.split('-')[1];
		this.setup();
	},
	setup : function(){
		$('tooltip-'+this.id).hide();
		Event.observe(this.element, 'click', this.show.bindAsEventListener(this));
		Event.observe($('close-'+this.id), 'click', this.hide.bindAsEventListener(this));
	},
	show : function(e){
		Event.stop(e);
		new Effect.Appear($('tooltip-'+this.id));
	},
	hide : function(e){
		Event.stop(e);
		new Effect.Fade($('tooltip-'+this.id));
	}
}

ScreenScroller = Class.create();
ScreenScroller.prototype = {
    initialize : function(element, options) {
  	    this.options = Object.extend({
        }, options || {});
        this.margin = 0;
        this.width = 0;
        this.element = $(element);
        this.scrollLock = false;
        this.setUp();
    },
    setUp : function() {

        Element.cleanWhitespace(this.element);
        $A(this.element.getElementsByTagName('li')).each(function(node){
            this.width = this.width+node.offsetWidth+3;
            Event.observe(node, 'mouseover', function(){ Element.addClassName(node, 'active') }.bind(this));
            Event.observe(node, 'mouseout', function(){ Element.removeClassName(node, 'active') }.bind(this));
        }.bind(this));
        this.height=this.element.offsetHeight;
        this.width=this.width-3;
        this.registerCallBacks();
	},
    registerCallBacks : function(){
        if(this.options.direction=="horizontal"){
            this.scrollLeftListener = this.scrollIt.bindAsEventListener(this);
            this.scrollRightListener = this.scrollIt.bindAsEventListener(this);
            Event.observe($('scrollLeft'), 'click', this.scrollLeftListener, false);
            Event.observe($('scrollRight'), 'click', this.scrollRightListener, false);
        }
        if(this.options.direction=="vertical"){
            this.scrollUpListener = this.scrollIt.bindAsEventListener(this);
            this.scrollDownListener = this.scrollIt.bindAsEventListener(this);
            Event.observe($('scrollUp'), 'click', this.scrollUpListener, false);
            Event.observe($('scrollDown'), 'click', this.scrollDownListener, false);
        }
    },
    scrollIt : function(event){

      Event.stop(event);
    	if(this.scrollLock==false){
    		this.scrollLock = true;
        var direction = Event.element(event).id;
        if(direction == "scrollRight") {
            var incrementY = 0;
            var incrementX = -131;
            this.margin=this.margin+incrementX;
            var max = -(this.width-521);
            if((this.width+this.margin) < 521){
                this.margin=this.margin-incrementX;
                incrementX=0;
            }
        }
        if(direction == "scrollLeft") {
            var incrementY = 0;
            var incrementX = 131;
            this.margin=this.margin+incrementX;
            if(this.margin>0){
                incrementX=0;
                this.margin=0;
            }
        }
        if(direction == "scrollDown") {
            var incrementX = 0;
            var incrementY = -101;
            this.margin=this.margin+incrementY;
            var max = -(this.height-401);
            if((this.height+this.margin) < 401){
                this.margin=this.margin-incrementY;
                incrementY=0;
            }
        }
        if(direction == "scrollUp") {
            var incrementX = 0;
            var incrementY = 101;
            this.margin=this.margin+incrementY;
            if(this.margin>0){
                incrementY=0;
                this.margin=0;
            }
        }
        new Effect.MoveBy(this.element, incrementY, incrementX, {afterFinish : function(){ this.scrollLock = false; }.bind(this)});
      }
    }
}

var replace_images = {
	colours : {
		black : {colour: 'ffffff', background: '000000'},
		red : {colour: 'ffffff', background: 'ea0000'},
		white: {colour: '333333', background: 'ffffff'},
		grey: {colour: '333333', background: 'ebebeb'},
		featured_game : {colour: 'cc0000', background: '242424'}
	},
	replacements : {
		'news' : {'div.header h2' : {font: 1, size: 8},'div.content h3' : {font: 1, size: 8}},
		'events' : {'div.header h2' : {font: 1, size: 8},'div.content h3' : {font: 1, size: 8}},
		'screenshots' : {'div.header h2' : {font: 1, size: 8},'div.content h3' : {font: 1, size: 8}},
		'polls' : {'div.header h2' : {font: 1, size: 8},'div.content h3' : {font: 1, size: 8}},
		'benefits' : {'div.header h2' : {font: 1, size: 8},'div.content h3' : {font: 1, size: 8}},
		'wallpapers' : {'div.header h2' : {font: 1, size: 8},'div.header h2' : {font: 1, size: 7},'div.content ul li ul li a' : {font: 1, size: 7}},
		'links' : {'div.header h2' : {font: 1, size: 8}},
		'survey' : {'div.header h2' : {font: 1, size: 8}},
		'faqs' : {'div.header h2' : {font: 1, size: 8}},
		'competition' : {'div.header h2' : {font: 1, size: 8}},
		'podcast' : {'div.header h2' : {font: 1, size: 8}}
	},
	getElements : function(selector, root){
		if(root==null){
			return [];
		}
		var elements = [];
		elements.push(root);
		var temp = [];
		var selectors = selector.split(' ');
		var klen = selectors.length;
		for(var k=0; k<klen; k++){
			var temp = [];
			var tlen = elements.length;
			for(var t=0; t<tlen; t++){
				if(selectors[k].indexOf('.') > -1){
					var test = selectors[k].split('.');
					var tagName = test[0];
					var className = test[1];
					if(!tagName){
						var tagName = "*";
					}
					var nodes = elements[t].getElementsByTagName(tagName);
					var mlen = nodes.length;
					var reg = new RegExp("(^|\\s)"+className+"(\\s|$)");
					for(var m=0; m<mlen; m++){
						if(reg.test(nodes[m].className)){
							temp.push(nodes[m]);
						}
					}
				}else{
					var tagName = selectors[k];
					var nodes = elements[t].getElementsByTagName(tagName);
					var mlen = nodes.length;
					for(var m=0; m<mlen; m++){
							temp.push(nodes[m]);
					}
				}
			}
			elements = temp;
		}
		return elements;
	},




	init : function(options){

		return; // SKIP THIS

		var content = document.getElementById('content');
		this.url = "/headings/heading.php";
		if(options){
			this.replace(this.getElements(options.selector, document), {color: options.colour, background: options.background, font: options.font, size: options.size});
			return;
		}
		var divs = document.getElementById('subwrapper').getElementsByTagName('div');
		var ilen = divs.length;
		var reg = new RegExp("(^|\\s)module(\\s|$)");
		for(var i=0; i<ilen; i++){
			if(reg.test(divs[i].className)){
				var cls = divs[i].className.split(' ');
				var type = cls[1];
				if(this.replacements[type]){
					var colour = cls[2].split('_')[0];
					for(selector in this.replacements[type]){
						var elements = this.getElements(selector, divs[i]);
						var jlen = elements.length;
						for(var j=0; j<jlen; j++){
							this.replace(elements[j], {color: this.colours[colour].colour, background: this.colours[colour].background, font: this.replacements[type][selector].font, size: this.replacements[type][selector].size});
						}
					}
				}
      		}
		}

		this.replace(this.getElements('h1.l1 span', content), {color: 'ffffff', background: '000000', font: '1', size: '11'});
		this.replace(this.getElements('h1.l2 span', content), {color: 'ffffff', background: 'de0b0b', font: '1', size: '11'});
		this.replace(this.getElements('h1.l3 span', content), {color: '333333', background: 'f2f2f2', font: '1', size: '11'});
		this.replace(this.getElements('h1.l4 span', content), {color: '333333', background: 'f2f2f2', font: '1', size: '11'});
		this.replace(this.getElements('div.description div.header h2', content), {color: '333333', background: 'ffffff', font: '4', size: '11'});
		this.replace(this.getElements('div.subnav h2', document.getElementById('sidebar')), {color: 'ffffff', background: '000000', font: '1', size: '9'});
		this.replace(this.getElements('h2', document.getElementById('browser')), {color: 'ffffff', background: '000000', font: '2', size: '10'});
		this.replace(this.getElements('h4 a', document.getElementById('loggedin')), {color: '333333', background: '000000', font: '2', size: '9'});
		this.replace(this.getElements('.benefits h3 a', document.getElementById('loggedout')), {color: 'ffffff', background: '000000', font: '1', size: '7'});
		this.replace(this.getElements('label.replace', document), {color: '333333', background: 'ffffff', font: '2', size: '9'});
		this.replace(this.getElements('span.replace', document), {color: 'ffffff', background: 'ffffff', font: '2', size: '9'});
		this.replace(this.getElements('div.exclusives .header h2', document), {color: '333333', background: 'ffffff', font: '1', size: '8'});
		this.replace(this.getElements('div.extras .header h2', document), {color: 'ffffff', background: '000000', font: '1', size: '8'});
		this.replace(this.getElements('ul.tabs li.selected span', content), {color: 'ff0000', background: 'ffffff', font: '2', size: '9'});
		this.replace(this.getElements('ul.tabs li span', content), {color: '333333', background: 'ffffff', font: '2', size: '9'});
		this.replace(this.getElements('div.result div.details h2', content), {color: '333333', background: 'f2f2f2', font: '1', size: '9'});
		this.replace(this.getElements('.registration div.feature h2', content), {color: '333333', background: 'ffffff', font: '2', size: '9'});
		this.replace(this.getElements('div.banner h2 a', content), {color: '333333', background: 'ffffff', font: '1', size: '9'});
		this.replace(this.getElements('div.featured_game div.header h3', content), {color: '000000', background: 'ffffff', font: '1', size: '8'});
		this.replace(this.getElements('div.game .header h3', content), {color: 'ffffff', background: '000000', font: '1', size: '8'});
		this.replace(this.getElements('div.game ul.platforms a', content), {color: '333333', background: 'ffffff', font: '1', size: '7'});
		this.replace(this.getElements('div.podcast_panel div.header h3', document), {color: 'ffffff', background: '000000', font: '1', size: '8'});
		this.replace(this.getElements('div.podcast_panel div.content a', document), {color: '333333', background: 'ffffff', font: '1', size: '7'});
		this.replace(this.getElements('div.description ul.platforms li a', content), {color: '333333', background: 'ffffff', font: '1', size: '7'});
		this.replace(this.getElements('div.description ul.platforms li.selected', content), {color: '333333', background: 'ffffff', font: '1', size: '7'});
	},





	replaceArray : function(element, options){
		for(var i=0; i<element.length; i++){
			this.replace(element[i], options);
		}
	},
	replace : function(element, options){
		if(element.constructor == Array){
			this.replaceArray(element, options);
		}else{
			var text = this.getText(element).split(' ');
			for(var i=0; i<element.childNodes.length; i++){
				element.removeChild(element.firstChild);
			}
			for(i=0; i<text.length; i++){
				var url = this.url+"?text="+escape(text[i]+" ")+"&colour="+options.color+"&background="+options.background+"&font="+options.font+"&size="+options.size;
				var image = document.createElement("img");
				image.className = "replacement";
				image.src = url;
				image.alt = text[i];
				element.appendChild(image);
			}
		}
	},
	getText : function(element){
    if(typeof element == "string"){
    	return element;
    }else{
    	if(typeof element == "undefined"){
    		return element;
    	}
    }
    var text = "";
    var kids = element.childNodes;
    for(var i=0;i<kids.length;i++) {
    	if(kids[i].nodeType == 1){
    		text += this.getText(kids[i]);
	    }else{
  	  	if(kids[i].nodeType == 3){
    			text += kids[i].nodeValue;
    		}
    	}
	  }
	  return text;
	}
}


replaceImages = replace_images;

function loadScript(url){
   document.write('<script type="text/javascript" src="'+url+'"></script>');
}

function init() {
  if (arguments.callee.done) return;
  arguments.callee.done = true;
	if(document.all) {
		window.focus();
	}
  myLightbox = new Lightbox();
	replace_images.init();
	new Accordian('browser');
	if($('screensH')) new ScreenScroller('screensH', {direction: 'horizontal'});
	if($('screensV')) new ScreenScroller('screensV', {direction: 'vertical'});
	if($('homepageBanner')) {
		var HFO = { movie:"/flash/base.swf?loc=uk", width:"738", height:"300", majorversion:"7", build:"0", wmode:"transparent" };
		UFO.create2(HFO, "homepageBanner");
	}
	if(typeof FO!="undefined") {
		UFO.create2(FO, FO.DomId);
	}
	if($('help-email')){ new ToolTip('help-email');	}
	if($('help-username')){ new ToolTip('help-username');	}
	if($('help-passwd')){ new ToolTip('help-passwd');	}
	if($('help-mobile')){ new ToolTip('help-mobile');	}
};

/* for Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, null);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script defer src=/javascript/ie_onload.js><"+"/script>");
/*@end @*/
/* for other browsers */
window.onload = init;

loadScript("/javascript/effects.js");
loadScript("/javascript/lightbox.js");
loadScript("/javascript/ufo.js");
var tracker = {}
