// (C) Isaac Rivera, 2011

// declare domain space:
if(!window.com) window.com = {};
if(!window.com.weisslink) window.com.weisslink = {};
if(!window.com.weisslink.Navigation) {
	
	window.JW_NAV = window.com.weisslink.Navigation = function(navRootId, targetId) {
		if(!navRootId) throw new Error("Navigation(navRootId), navRootId can't be undefined: " + navRootId);
		this.navRootId = this.normalizeId(navRootId);
		
		if(!targetId) throw new Error("Navigation(targetId), targetId can't be undefined: " + targetId);
		this.targetContainer = $(this.normalizeId(targetId));
		
		this.currentItem = null;
		this.pathToEmebbeddedUL = this.navRootId + " > li > ul";
		
		this.BASE_URL = $("base").attr("href");
		this.MAIN_CSS = this.BASE_URL + "css/main.css";
		this.INVERTED_CSS = this.BASE_URL + "css/inverted.css";
		this.PLAYER_PATH = "jwplayer/player.swf";
	}

	window.com.weisslink.Navigation.prototype.getSelectedItemFromURL = function(){
		var hash = window.location.hash;
		if(hash == null || hash.length == 0) return null;
		var hashId = hash.substring(1, hash.indexOf("/"));
		return this.normalizeId(hashId);
	};
	
	window.com.weisslink.Navigation.prototype.selectItem = function(id, slide) {
		if(!id) return;
		id = this.normalizeId(id);
		
		var style = $(id).attr("rel");
		console.info("selectItem(), $(id): " + $(id));
		console.info("selectItem('" + id + "'), style: " + style);
		if(style == "main") {
			this.setCSS(this.MAIN_CSS);
		} else if(style == "inverted") {
			this.setCSS(this.INVERTED_CSS);
		}
		
		var item = this.getListItemFromChild(id);
		if(item && slide) {
			$(this.pathToEmebbeddedUL).not(item).slideUp(100);
			item.slideDown(100);
		} else if(item) {
			$(this.pathToEmebbeddedUL).not(item).hide();
			item.show();
		}
		if(this.currentItem) this.currentItem.removeClass("selected");
		this.currentItem = $(id).addClass("selected");
		window.document.title = "Jeff Weiss: " + this.currentItem.attr("title");
		
		var URL = "./sections" + this.currentItem.attr("href").substring(this.currentItem.attr("href").lastIndexOf('/'));
		
		$.get(URL);
	};
	
	window.com.weisslink.Navigation.prototype.getListItemFromChild = function(id){
		if(!id) return null;
		id = this.normalizeId(id);
		var item = $(id);
		if (!item) return null;
		item = item.closest(this.pathToEmebbeddedUL);
		return item;
	};
	
	window.com.weisslink.Navigation.prototype.normalizeId = function(id){
		return id.charAt(0) == "#" ? id : ("#" + id);
	};
	

	window.com.weisslink.Navigation.prototype.getFullURLFromRootPath = function(path) {
		if(path.indexOf("http://") == 0) return path;
		if(path.charAt(0) == "/") path = path.substring(1);
		return this.BASE_URL + path;
	};

	window.com.weisslink.Navigation.prototype.makeVideo = 
			function(container, width, height, videoPath, posterPath, bgcolor, buffer, controlbar, volume, provider) {
		
		if(buffer == null) buffer = 1;
		if(buffer < 1) buffer = 1;
		
		if(bgcolor == null) bgcolor = "000000";
		else if(bgcolor.charAt(0) == "#") bgcolor = bgcolor.substring(1);
		
		window.jwplayer.BG_COLOR = "#" + bgcolor;
		
		if(controlbar == null) controlbar = "none";
		
		if(volume == null) volume = 100;
		if(volume < 0) volume = 0;
		
		if(provider == null) provider = "http";
		
		jwplayer(container).setup({
			'id':"videoPlayer", 
			'flashplayer':this.getFullURLFromRootPath(this.PLAYER_PATH), 
			'file':this.getFullURLFromRootPath(videoPath), 
			'image':this.getFullURLFromRootPath(posterPath), 
			'provider':provider,
			'screencolor':bgcolor, 
			'controlbar':controlbar, 
			'autostart':true, 
			'bufferlength':buffer, 
			'icons':false, 
			'width':width, 
			'height':height, 
			'volume':volume, 
			'http.startparam':'position'
		});
	};

	window.com.weisslink.Navigation.prototype.setCSS = function(css) {
		
		if(css == $("link").attr("href")) return;
		
		$("link").attr("href", css);
		if(css == this.INVERTED_CSS) {
			$("#logoImg").attr("src", "img/jeffweiss_999.png");
		} else {
			$("#logoImg").attr("src", "img/jeffweiss.png");
		}
	};
	
	window.JW_NAV_INIT = window.com.weisslink.initNavigation = function(navRootId, targetId){
		window.JW_NAV_INIT = window.com.weisslink.initNavigation = null;
		
		window.JW_NAV = window.com.weisslink.Navigation = new window.com.weisslink.Navigation(navRootId, targetId);
		
		$(JW_NAV.navRootId + " > li > div").click(function(evt){
			var selectedUL = $(".selected").closest(JW_NAV.pathToEmebbeddedUL);
			$(JW_NAV.pathToEmebbeddedUL).not($(this).next()).not(selectedUL).slideUp(100);
			$(this).next().slideDown(100);
		});
		
		JW_NAV.targetContainer.ajaxComplete(function(e, xhr, settings) {
			$(this).html(xhr.responseText);
		});
		
		$("ul > li > a").click(function(evt){
			JW_NAV.selectItem($(this).attr("id"), true);
		});
		
		var selected = JW_NAV.getSelectedItemFromURL();
		if(selected != null) {
			JW_NAV.selectItem(selected, false);
		} else {
			$("#section").css("visibility", "visible");
		}
	};
}

