HW.UI.TabSet.prototype.currentLink = null;
HW.UI.TabSet.prototype.requests = [];
HW.UI.TabSet.prototype.callbackLocks = [];

HW.UI.TabSet.prototype.getLinks = function() {
		var obj = this;
		
		this.links = [];
		if(this.list) {
			var elms = this.list.getElementsByTagName('a');
			for(var i=0,j=elms.length;i<j;i++) {
				elms[i].href = elms[i].href+'#'+i;
				this.links.push(elms[i]);
				(function() {
					var k=i;
					var link = elms[i];
					HW.attachEvent(link, 'click', function(){obj.change(k,link);}, false);
				})()
			}
		}
	}

HW.UI.TabSet.prototype.change = function(n,link) {

		n = typeof(n)=='undefined'?this.state:n;
		if(link && link.hash && this.options.backButton) {
			self.location.hash = link.hash;
			this.current = link.hash;
		}else{
			if(!link){link = this.links[n];}
		}
		if(this.currentLink == link){return;};
	
		for(var i=0,j=this.links.length;i<j;i++) {
			var li = null, o = this.links[i];
			while(o && o.tagName != 'LI') {
				o = o.parentNode;
			}			
			if(o && o.tagName == 'LI') {
				if(i != n) {
					HW.removeClass(o,this.options.highlight);
				}
				else {
					HW.addClass(o,this.options.highlight);
				}
			}
		}
		this.currentLink = link;
		var linkLoc = link.href;
		this.loadContent(linkLoc);

		this.links[n].blur();
			
	};

HW.UI.TabSet.prototype.loadContent = function(path){
	var obj = this;
	this.clearRequests();
	var lock = true;
	setTimeout(function(){lock = false}, obj.options.loadDelay);
	var loader = new Image();
	var doOnlyOnce = false;
	loader.onload = function(){
		if (obj.lastPath == path || doOnlyOnce) {
			loader.onload = null;
			return;
		}
		doOnlyOnce = true;
		obj.lastPath = path;
		
		obj.panel = HW.removeAllChildNodes(obj.panel);
		obj.panel.appendChild(loader);
		obj.callbackLocks.push(true);
		var key = obj.callbackLocks.length-1;
		function callback(res){
			if(obj.callbackLocks[key]){ 
				obj.lockCallbacks();
				if(!lock){
					try{
						var newDocument = res.text.replace(/<script.*?>/g, '').replace(/<\/script>/g, '');//.match(/<body.*?>(.|\n|\r)*(?=(<\/body>))/m)[0].replace(/<body.*?>/, '');	
						var div = document.createElement('div');
						div.innerHTML = newDocument;
						var divs = div.getElementsByTagName('div');
						var counter = 0;
						for(var i = 0; i < divs.length; i++){
							if ((divs[i].className == 'jvsPanel')||(divs[i].className == 'contentStyle06b jvsPanel')) {
								var panelDiv = divs[i];
							}
							counter++;
						}
						var text = panelDiv.innerHTML;
					}catch(e){
						var text = 'Content unavailable';
					}finally{
						obj.panel.innerHTML = text;
						if(typeof(obj.options.callback) == 'function'){obj.options.callback.call(obj,obj.panel);}
					}
				}else{
					var callee = arguments.callee;
					setTimeout(function(){callee(res)}, 200);		
				}	
			}
		}
		if(obj.options.path){
			path = obj.options.path+path.substr(path.lastIndexOf('/')+1)	
		}
		path = path.replace(/#[^\?.]+/, '');
		obj.requests.push(new HW.Ajax(path, function(res){
			callback(res);
		}, null));
	}
	loader.src = '/1/themes/html/hsbcuk-wrs-common/general/images/graphics/loader.gif';
	loader.className = 'loader';
};

HW.UI.TabSet.prototype.lockCallbacks = function(){
	for(var i = 0; i < this.callbackLocks.length-1; i++){
		this.callbackLocks[i] = false;
	}
}

HW.UI.TabSet.prototype.clearRequests = function(){
	for(var i = 0; i < this.requests.length; i++){
		this.requests[i].cancel();	
	}
}

HW.UI.TabSet.prototype.checkTab = function() {
	if(this.isTab(self.location) && self.location.hash != this.current) {
		this.current = self.location.hash;
		this.getInitialState();
		this.change();
	}
}

HW.UI.TabSet.prototype.isTab = function(newLocation){
	if(newLocation.hash == ''){
		return false;
	}
	for(var i = 0; i < this.links.length; i++){			 
		if(newLocation.hash == this.links[i].hash){
			return true;
		}
	}
	return false;
}

HW.extendObject(HW.UI.TabSet.Options.prototype, {path: null, loadDelay: 500});

HW.Ajax.prototype.cancel = function(){
	this._passResponse = function(){};
}

HW.removeAllChildNodes = function(elem){
	if (elem.hasChildNodes()){
		while (elem.childNodes.length >= 1){
			elem.removeChild(elem.firstChild);       
		} 
	}
	return elem;
}	

/**************************************************************************************************/

HW.onload(function(){
	var options = {
		highlight: 'selected',
		path: null,
		callback:function(panel){
			_hw$$('div.flash_container',panel).each(function(a){
				var swf = _hw$('a.flash_location',a).getAttribute('href'),
					f = new HW.Flash(),
					id = a.id,
					versions = [8,10];
				
				f.width = 562;
				f.height = 236;
				f.src = [];
				for(var i=0;i<versions.length;i++) {
					f.src.push(versions[i]);
					f.src.push(swf.replace(/\[ver\]/g,versions[i]));
				}
				f.load(id);
			});
		}
	}
	new HW.UI.TabSet(_hw$('.jvsTabs'), _hw$('.jvsPanel'), options);			   
});

