function clog(v,k) {
	//if(console.log(v));	if(console.log('=> '+k));	if(console.log());
}
function alog(v,k) {
	alert(v);
	alert(k);
}

var Tabber = Class.create();
var rendered = [];
Tabber.prototype = {
	initialize : function(element) {
		this.links = $A(document.getElementsByTagName('a'));
		this.links.each(function(link){
			var hash = this.getHash(link.href);
			if(hash != 0) {
        this.setupExternal(link);
			}
    }.bind(this));
		//var options = Object.extend({}, arguments[1] || {});
		this.element = $(element);
		//clog(this.element,'Tabbed Element');
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.menu.each(this.setupTab.bind(this));
		clog(this.menu,'Menu');
		this.toggle(this.getInitialTab());
	},
	setupExternal : function(elm) {
		clog(elm,'Setup External');
		Event.observe(elm,'click',this.exToggle.bindAsEventListener(this),false);
	},
	setupTab : function(elm) {
		clog(elm,'Setup Tab');
		Event.stopObserving(elm,'click',this.exToggle.bindAsEventListener(this),false);
		Event.observe(elm,'click',this.tabToggle.bindAsEventListener(this),false);
		if(elm.hasClassName('active')){
			//clog(elm,'Active Tab');
		}
	},
	exToggle :  function(ev) {
		clog(ev,'clicked');
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		window.scrollTo(0,750);
		this.toggle(elm);
	},
	tabToggle :  function(ev) {
		clog(ev,'clicked');
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.toggle(elm);
	},
	toggle : function(tab) {
		clog(tab,' Toggle');
		//var content = $(this.tabID(tab));
		this.menu.each(this.deactivate.bind(this));
		this.activate(tab);
		if (window.sIFR && sIFR.replace && typeof sIFR.replace == "function") {
			if (!rendered[tab]) {
				var lhs = {
					src: '/skin/frontend/default/musicline/flash/lhs.swf'
				};
				sIFR.replace(lhs, {
					selector: '.tabs-content h2',
					css: ['.sIFR-root { color: #E6000A; font-size: 30px; }', 'a { text-decoration: none; }', 'a:link { color: #0E3A80; }', 'a:hover { color: #0E3A80; }'],
					wmode: 'transparent'
				});
				rendered[tab] = true;
			} else {
				var movies = $(this.tabID(tab)).select("object.sIFR-flash");  
				for (var i = 0; i < movies.length; i++) {
					for (var j = 0; j < sIFR.replacements.length; j++) {
						if ( sIFR.replacements[j].id == movies[i].id )
							sIFR.replacements[j].resetMovie();
					}  
				}  
			}
		}
		//this.activate(this.menu[tab]);
		//this.menu.without(tab).each(this.deactivate.bind(this));
//		this.menu.without(tab).each( function(el) {
//			//var content = $(this.tabID(el));
//			this.deactivate(el,'');//content);
//		});
		//this.menu.without(elm).each( function(el) { clog(el,'Menu item without element '+elm.id) });
		//clog(this.menu.without(elm),'Full menu without element '+elm.id);
		//clog(elm,'Show');
		//        elm.addClassName('active');
	},
	activate : function(tab) {
		var content = $(this.tabID(tab));
		this.menu.each(function(el){ //jsvlu o,[;r,rmy
			if(el==tab.href){
				el.addClassName('active');
			}
		});
		content.removeClassName('hide');
		//content.style.display='';
		clog(tab,'activated');
		clog(content,'displayed');
		//$(this.tabID(elm)).show();
	},
	deactivate : function(tab) {
		var content = $(this.tabID(tab));
		tab.removeClassName('active');
		content.addClassName('hide');
		//content.style.display='none';
		clog(tab,'deactivated');
		clog(content,'hidden');
		//$(this.tabID(tab)).style.display='none';
		//clog($(this.tabID(tab)),'hidden');
		//        elm.removeClassName('active');
		//        contents.hide();
	},
	tabID : function(elm) {
		var hash = this.getHash(elm.href)
		if(hash != 0) {
			//clog(elm.href,'TabID');
			return hash;
		} else {
			return 0;
		}
//		var tabid = hash.replace(/#/,'');
	},
	getHash : function(linkUrl) {
		var hash = 0; // null;
		if(linkUrl.indexOf('#') != -1) {
			var fullUrl = document.location.href; //hashed if applicable
			//var pageUrl = document.location.href.slice(0, document.location.href.lastIndexOf("#")); //unhashed
			//document.location.href.slice(document.location.href.lastIndexOf("#"), document.location.href.length); //gets hash
			var pageUrl = fullUrl.slice(0, fullUrl.lastIndexOf("#")); //unhashed
		  if(linkUrl.indexOf(pageUrl) != -1) {
				var arr = linkUrl.match(/#(\w.+)/);
				if(null != arr) {
					var hash = arr[1];
					//clog(linkUrl,'linkUrl');
					//clog(pageUrl,'pageUrl');
					clog(hash,'linkHash');
				}
			}
		} // else hash still == 0
		return hash;
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			 var loc = RegExp.$1;
				var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
				clog(elm,'Initial is Hash');
				return elm || this.menu.first();
//		if(window.location.hash) {
//			var hash = document.location.href.slice(document.location.href.lastIndexOf("#"), document.location.href.length); //gets hash
//			var urlHash = window.location.hash;
//			var initialTab = urlHash.replace(/#/,'');
//			return($(initialTab));
		} else {
			clog(this.menu.first(),'Initial is First');
			return this.menu.first();
		}
//	},
//	dump: function(arr) {
//		var dumped_text = "";
//		var level = 0;
//		
//		//The padding given at the beginning of the line.
//		var level_padding = "";
//		for(var j=0;j<level+1;j++) level_padding += "    ";
//		
//		if(typeof(arr) == 'object') { //Array/Hashes/Objects
//		 for(var item in arr) {
//			var value = arr[item];
//		 
//			if(typeof(value) == 'object') { //If it is an array,
//			 dumped_text += level_padding + "'" + item + "' ...\n";
//			 dumped_text += dump(value,level+1);
//			} else {
//			 dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
//			}
//		 }
//		} else { //Stings/Chars/Numbers etc.
//		 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
//		}
//		clog(dumped_text);
	}
}

new Tabber('product_tabs');