/**
 * @author Roman Schmid
 */

iConcerts.UI.MainNav = Class.create();
iConcerts.UI.MainNav.elements = new Array();
iConcerts.UI.MainNav.closeAll = function(callback){
	var elems = $A(iConcerts.UI.MainNav.elements);
	var enabled = false;
	elems.each(function(elem){
		if(elem._enabled){
			enabled = elem;
		}
	});

	if(enabled){
		enabled.disable(callback);
	} else {
		callback();
	}
	elems[0].enable();//PV
};
iConcerts.UI.MainNav.closeAllB = function(){
	var elems = $A(iConcerts.UI.MainNav.elements);
	var enabled = false;
	elems.each(function(elem){
		if(elem._enabled){
			enabled = elem;
		}
	});

iConcerts.UI.MainNav.closeAll = function(callback){
	var elems = $A(iConcerts.UI.MainNav.elements);
	var enabled = false;
	elems.each(function(elem){
		if(elem._enabled){
			enabled = elem;
		}
	});

	if(enabled){
		enabled.disable(callback);
	} else {
		callback();
	}
	elems[0].enable();//PV
};

};
iConcerts.UI.MainNav.itemListeners = new Array();
iConcerts.UI.MainNav.addItemCallBack = function(callback){
	if(typeof callback == 'function')
		iConcerts.UI.MainNav.itemListeners.push(callback);
};

iConcerts.UI.MainNav.removeItemCallBack = function(callback){
	var items = iConcerts.UI.MainNav.itemListeners;
	var len = items.length-1;
	for(var i=len; i>=0; i--){
		if(items[i] == callback){
			items.splice(i,1);
		}
	}
};

iConcerts.UI.MainNav.openItem = function(category, index){
	var navlist;
	if(category == 'genres'){
		navlist = iConcerts.UI.MainNav.elements[0];
	} else {
		navlist = iConcerts.UI.MainNav.elements[1];
	}
	
	navlist.locationItem.activate(navlist.navItems[index], iConcerts.UI.MainNav.fireEvent, index);
};

iConcerts.UI.MainNav.closeItem = function(category){
	var navlist;
	if(category == 'genres'){
		navlist = iConcerts.UI.MainNav.elements[0];
	} else {
		navlist = iConcerts.UI.MainNav.elements[1];
	}
	
	navlist.locationItem.deactivate();
};

iConcerts.UI.MainNav.fireEvent = function(action, category, item, href, index){
	var items = $A(iConcerts.UI.MainNav.itemListeners);
	items.each(function(func){
		func(action, category, item, href, index);
	});
};

iConcerts.UI.MainNav.prototype = {
	// all mainnav elements
	initialize: function(elemId) {
		this.element = $(elemId);
		if(this.element == null){
			alert("Element does not exist");
			//TODO: Error handling. throw some error
		}
		
		$$('.location').each(function(elem){
			elem.remove();
		});
		
		var listElements = iConcerts.UI.MainNav.elements;
		var self = this.element;
		// iterate through all navigation points
		this.element.immediateDescendants().each(function(elem, index){
			if(index > 0){ // home will be ignored
				var elemItems = elem.select('.items')[0];
				var wrapper = elemItems.iWrap('div', {useClass:'outer'});
				var list = new iConcerts.UI.MainNavList(wrapper);
				// store all sub-lists in an Array
				listElements.push(list);
				//iConcerts.resizeHandler.addListener(list);
				// observe klick on first <a> element
				elem.select('a')[0].onclick = function(){
					// disable all "others"
					listElements.each(function(lItem){
						if(lItem == list){
							lItem.enable();
						} else {
							lItem.disable();
						}
					});
					return false;
				}
				//walid
/*PV Commenter ce block pour ne pas avoir la chienli du truc qui s'ouvre tout le temps */
/*PV Encore un 2me block plus bas à faire aussi */
/*PV*//*				elem.getElementsBySelector('a')[0].onmouseover = function(){
					// disable all "others"
					listElements.each(function(lItem){
						if(lItem == list){
							lItem.enable();
						} else {
							lItem.disable();
						}
					});
					return false;
				}
*//**/				//end walid
				
			}
		}.bind(this));
		listElements[0].enable();//PV
	},
	
	getElements: function(){
		return this.elements;
	}.bind(this)
};

iConcerts.UI.MainNavList = Class.create();
iConcerts.UI.MainNavList.prototype = {
	initialize: function(element){
		this.element = $(element);
		this.element.hide();
		this._enabled = false;
		this._animating = false;
		this.locationItem = new iConcerts.UI.LocationIcon(this.element.up().id);
		this.navItems = new Array();
		var self = this.element;
		var items = this.element.select('a');
		if(items.length > 0){
			items.each(function(item, index){
				item.observe('click', function(event){
					Event.stop(event);
					//this.disable( item.getAttribute('href'));
					this.locationItem.activate(item, iConcerts.UI.MainNav.fireEvent ,index);
//PV					this.disable();
				}.bind(this));
				this.navItems.push(item);
			}.bind(this));
		}
		
		this.eventPointer = this.checkMouse.bindAsEventListener(this);
		this.wi = this.element.getWidth();
		this.he = this.element.getHeight();
	},
	
	enable: function(){
		if(!this._animating){
			if(!this._enabled){
				this._enabled = true;
				this._animating = true;
//PV				this._animating = false;
				this.element.up().select('a')[0].className = 'active';
				Effect.SlideDown(this.element, {afterFinish: function(obj){
					this._animating = false;
					this.watchMouse();
//PV					setTimeout("iConcerts.UI.MainNav.closeAllB",4000); //walid
				}.bind(this)});
//PV				this.element.show();
			} else { // toggle
//				this.disable();
			}
		}
	},
	
	disable: function(follow){
		if(!this._animating){
			if(this._enabled){
				this._enabled = false;
				this._animating = true;
//PV				this._animating = false;
				this.element.up().select('a')[0].className = '';
				this.unwatchMouse();
				//Effect.SlideUp(this.element);
				Effect.SlideUp(this.element, {afterFinish: function(obj){
					this._animating = false;
					if(follow){
						if(typeof follow == 'function'){
							follow();
						} else {
							window.location.href = follow;
						}
					}
				}.bind(this)});
/*PV				this.element.hide();
					if(follow){
						if(typeof follow == 'function'){
							follow();
						} else {
							window.location.href = follow;
						}
					}*/
			}
		}
	},
	
	watchMouse: function(){
		var dim = Position.cumulativeOffset(this.element);
		this.x = dim[0];
		this.y = dim[1];
		this.element.observe('mouseout', this.eventPointer);
	},
	
	checkMouse: function(event){
		var mousex = Event.pointerX(event);
		var mousey = Event.pointerY(event);
		if(mousex <= this.x || mousey <= this.y || mousex >= this.x + this.wi || mousey >= this.y + this.he){
			//Event.stop(event);
//PV			this.disable();
		}
	},
	
	unwatchMouse: function(){
		this.element.stopObserving('mouseout', this.eventPointer);
	}
};

iConcerts.UI.LocationIcon = Class.create();
iConcerts.UI.LocationIcon.prototype = {
	initialize: function(category){
		this.category = category == 'genres' ? 'genres' : 'stages';
		var node = $(document.createElement('div'));
		node.className = 'location ' + this.category;
		//var a1 = document.createElement('a');
		var a1 = document.createElement('span');
		//a1.setAttribute('href', '#');
		var a2 = document.createElement('a');
		a2.setAttribute('href', '#');
		a2.setAttribute('title', 'close');
		a2.className = 'close';
		node.appendChild(a1);
		node.appendChild(a2);
		this.index = -1;
		
		$('header').insertBefore(node,$('mainNav'));
		this.linkNode = $(a1);
		this.closeNode = $(a2);
		this.element = node;
		this.element.hide();
		
		this.element.observe('mouseover', function(event){
			Event.stop(event);
			this.closeNode.show();
		}.bind(this));

		this.element.observe('mouseout', function(event){
			Event.stop(event);
			this.closeNode.hide();
		}.bind(this));
			
		this.closeNode.hide();
	},
	
	activate: function(refItem, callback, index){
		if(!refItem)
			return;
		
		this.index = index;
		this.title = refItem.down().innerHTML;
		this.linkNode.className = refItem.className;
		this.linkNode.setAttribute('title', this.title);
		this.element.show();
		this.closeNode.onclick = this.deactivate.bindAsEventListener(this);
		this.callback = callback;
		this.ref = refItem.getAttribute('href');
		if(typeof callback == 'function')
			this.callback('open', this.category, this.title, this.ref, this.index);
	},
	
	deactivate: function(event){
		if(event)
			Event.stop(event);
		this.element.hide();
		if(typeof this.callback == 'function')
			this.callback('close', this.category, this.title, this.ref, this.index);
	}
}