/**
 * TabView is the class used for creating panels that are shown and
 * Common Javascript that appears on all pages in the EA mobile site
 * should be instantiated here. An example of common javascript is initialization of tracking code,
 * or javascript related to global navigation.
 * 
 * @author John Kakoulides (Fi)
 * 
 * @constructor
 */

function TabView(listId, lastSelected){
this.listId = listId;
this.lastSelected = lastSelected;
this.addListeners();
}

TabView.prototype.addListeners = function(){
	var instance = this;
	jQuery("#" + this.listId + " li a").each(function(iterator){
		jQuery(this).bind('click', function(e){
			
			jQuery("#" + instance.listId + " li").each(function(){
				this.className = "";
			});
			
			var clicked = jQuery(e.target);
			if (!clicked.is("li")){
				clicked = instance.find(clicked,"li");					
			}
			
			clicked[0].className = "selected";
			
			if(instance.lastSelected != iterator){
				
				jQuery("#" + instance.listId + "TabsContent" + instance.lastSelected).addClass("hidden");
				jQuery("#" + instance.listId + "TabsContent" + iterator).removeClass("hidden");
				instance.lastSelected = iterator;
				
			}
			
			// Reset scrolling div
			jQuery('div.prodDetails').scrollTop(0);
			
			e.preventDefault();
			
		});
	});
}

TabView.prototype.find = function(clicked,el){
	while(!clicked.is(el)){
		clicked = clicked.parent();
	}
	return clicked;
}