// iiNavigation version: 1.7


(function($) {
	
	$.iiNavigation = {
		stuck: "",
		defaults: { overLabel: "_over", activeClass: "active", hoverClass: "hover" },
		stick: function(id) {
			unstick();
			stick(id);
		},
		unstick: unstick,
		isCurrentPage: isCurrentPage
	}
	
		
	// create the new functions
	$.fn.extend({
		iiNavigation: function(settings) {
			
			// use either the defaults or the passed in settings
			settings = $.extend({}, $.iiNavigation.defaults, settings);


			// return this object so it can be chained
			return $(this).each(function() {
				var currentNav = this;

				// store the settings so we can get them later
				$.data(currentNav, 'iiNavigation', settings);
				
				// loop through and setup each list element
				$("li", this).each(function() {
				
					
					// if we are using an image, save it's info
					if($("a img", this).length > 0) {
						var img = $("a img", this).attr("src");
						var ext = img.substr(img.length-3, 3)
						var base = img.substr(0, img.length-4);
						var props = { 	currentNav: currentNav,
										isImage: true, 
										over: base + settings.overLabel + "." + ext, 
										out: img
									};
									
						// remember the image info for this image
						$.data(this, 'props', props);
						
						// preload the over state
						var temp = new Image();
						temp.src = props.over;
					} else {
						$.data(this, 'props', { currentNav: currentNav, isImage: false });
					}
				
					
					// strip the anchor tags and make the li's clickable
					var href = $(this).children("a").attr("href");
					var title = $(this).children("a").attr("title");
					$(this).attr("title", title);
					if(href != undefined) {
						$(this).click(function() { window.location.href=href; });
					}
//					$(this).children("a").replaceWith($(this).children("a").html());
					
					
					// init the subnav if there is one
					var subs = $("ul:first", this).eq(0);
					subs.each(function() {
						var ul = $(this);

						// make sure the parent object to this is positioned relatively
						$(this).parent().css("position", "relative");
						
						// make sure it has a border defined
						if(!parseInt($(this).parent().css("borderTopWidth")))
							$(this).parent().css("borderTopWidth", "0px");
						if(!parseInt($(this).parent().css("borderBottomWidth"))) {
							$(this).parent().css("borderBottomWidth", "0px");
						}

						
						// stretch the li's to this ul's width
						var li = ul.children("li").eq(0);
						var uw = ul.width();
						var offset = li.outerWidth() - li.innerWidth();
						var ml = parseInt(li.css("margin-left"));
						var mr = parseInt(li.css("margin-right"));
						var pl = parseInt(li.css("padding-left"));
						var pr = parseInt(li.css("padding-right"));

						// safari bug with right margin, just use left margin
						if($.browser.safari) {
							mr = ml;
						}
						ul.children("li").css("width", uw-ml-mr-pl-pr);
						

						// wrap the subnav in a div element
						$(this).wrap("<div></div>");
						
						// figure out what the full width and height are
						var fullWidth = ($.browser.safari) ? (ul.outerWidth() + parseInt(ul.css("margin-left"))*2) : (ul.outerWidth() + parseInt(ul.css("margin-left")) + parseInt(ul.css("margin-right")));
						var fullHeight = ($.browser.safari) ? (ul.outerHeight() + parseInt(ul.css("margin-top"))*2) : (ul.outerHeight() + parseInt(ul.css("margin-top")) + parseInt(ul.css("margin-bottom")));

						// wrap up the subnav
						var wrapper = $(this).parent();
						wrapper.css({	width: fullWidth,
										height: 0,
										overflow: "hidden",
										position: "absolute",
										top: parseInt(wrapper.parent().css("height")) + parseInt(wrapper.parent().css("borderBottomWidth")) + parseInt(wrapper.parent().css("borderTopWidth")),
										zIndex: 1000
									});

						// align it right if it's the last one
						if(ul.parent().parent().next().get(0) == undefined) {
							wrapper.css("right", 0);
						} else {
							wrapper.css("left", 0);
						}
									
						// add a bg container
						$(this).after('<div class="bg"></div>');
						var bg = $(this).parent().children("div.bg").eq(0);
						bg.css({	width: fullWidth,
									height: fullHeight,
									position: "absolute",
									top: -fullHeight,
									left: 0,
									zIndex: 1
								});
						
						
						// position it right above the "window"
						$(this).css({	position: "absolute",
										top: -bg.height(),
										left: 0,
										zIndex: 2
									});



						// if we're in firefox mac, swap the bg opacity for a bg png
						if(ffmac()) {
							if(parseInt(bg.css("opacity")) != 1) {
								bg.css({	opacity: 1,
											backgroundColor: "transparent",
											backgroundImage: 'url("images/bg_sub.png")'
										});
							}
							
						}
						
						
						// prevent the ul from clicking
						$(this).click(function(e) { 
							
							if(!$.browser.msie || $.browser.version > 6.0) {
								e.stopPropagation(); 
							}
						});
					});
					
					if(subs.length != 0) {
						$(this).bind("mouseenter", {nav: this}, showSub);
						$(this).bind("mouseleave", {nav: this}, hideSub);//function() { hideTimer = setTimeout("hideSub("+e+")", 200); });
					}
					
					
					// stick the sub if we're on that page or we've been set to active
					if(isCurrentPage(href) || $(this).hasClass('active')) {
						stick($(this).attr("id"));
					}
				})
				.bind("mouseenter", navOver)
				.bind("mouseleave", navOut);
				

			});
		}
		
	});


	// ******************** Public Functions *********************//
	// stick the nav with the id
	function stick(id) {
		var nav = $("#"+id);
		
		// if the object exists
		if(nav.length > 0) {
			var elm = nav.get(0);
			var props = $.data(elm, 'props');

			nav.addClass($.data(props.currentNav, 'iiNavigation').activeClass);
			highlight(elm);

			// remember we're stuck
			$.iiNavigation.stuck = elm;
			
			
			// if it's a child element of a li, we stick that one too
			if(nav.parents("li").length > 0) {
				var parent = nav.parents("li").get(0);
				$(parent).addClass($.data(props.currentNav, 'iiNavigation').activeClass);
				highlight(parent);
			}
		}
		
	};


	// unstick the current image
	function unstick() {
		
		if($.iiNavigation.stuck != "") {
			
			// unstick it
			var props = $.data($.iiNavigation.stuck, 'props');
			$($.iiNavigation.stuck).removeClass($.data(props.currentNav, 'iiNavigation').activeClass);
			unhighlight($.iiNavigation.stuck);
			
			// remember that we're not stuck
			$.iiNavigation.stuck = "";
			
		}

	};
	



	// ******************** Private Functions *********************//


	// log function for testing
	function log(s) {
		window.console.log(s);
	};


	// change to the over state
	function navOver(e) {

		var props = $.data(this, 'props');

		// add the hover class
		highlight(this);
		
				
	};
	
	
	// change to the off state
	function navOut(e) {

		// check to see if it's a necessary call
		var stuckid = $($.iiNavigation.stuck).attr("id");
		if(stuckid == $(this).attr("id") || $(this).find("#"+stuckid).length > 0) {
			return;
		}


		// if it isn't suck, change it back
		var props = $.data(this, 'props');
		if(props.currentNav.stuck != this) {
	
			unhighlight(this);
		}
		
	};


	// highlight the object
	function highlight(target) {
		var props = $.data(target, 'props');
		$(target).addClass($.data(props.currentNav, 'iiNavigation').hoverClass);
		
		// if it's an image
		if(props.isImage == true) {
			var img = $("img", target).get(0);

			// change it to the over state
			img.src = props.over;

			// fix thepng action
			$(img).ifixpng();
		}
	}
	
	// unhighlight the object
	function unhighlight(target) {
		var props = $.data(target, 'props');
		$(target).removeClass($.data(props.currentNav, 'iiNavigation').hoverClass);
		
		// if it's an image
		if(props.isImage) {
			var img = $("img", target).get(0);

			// change it to the out state
			img.src = props.out;

			// fix thepng action
			$(img).ifixpng();
		}
		
	}
	
	// swap the image to the over state
	function imageOver() {
	
	}
	
	// swap the image to the out state
	function imageOut() {
	
	}
	
	
	
	// show the sub navigation
	function showSub(e) {

		var sub = $("ul", e.data.nav).eq(0);
		var bg = $("div.bg", e.data.nav).eq(0);

		sub
			.siblings()
			.andSelf()
			.stop()
			.show()
			.animate({top: "0px"}, 500)
			.siblings()
			.show()
			.parent()
			.stop()
			.animate({	height: bg.outerHeight() + 5,
						width: bg.outerWidth()}, 500);
		
		currentSub = sub;
		
	};
	
	// hide the sub navigation
	function hideSub(e) {
		
		var sub = $("ul", e.data.nav).eq(0);
		var bg = $("div.bg", e.data.nav).eq(0);
		sub
			.siblings()
			.andSelf()
			.stop()
			.animate({top: -bg.outerHeight()}, 500, function() { $(this).hide().siblings().hide(); })
			.parent()
			.stop()
			.animate({height: "0px", width: bg.outerWidth()}, 500);
		

	};

	
	// figure out if we're ie6
	function ffmac() {
		return ($.browser.firefox && $.browser.mac);
	}


	// test to see if we're on the page passed in
	function isCurrentPage(link, includeQuery) {
		var includeQuery = (includeQuery == null) ? false : includeQuery;
		var url = window.location.href;
		if(link == undefined) {
			return false;
		}
		
		// if we don't want to include the query, trim it off
		if(!includeQuery) {
			url = (url.indexOf("?") == -1) ? url : url.substr(0, url.indexOf("?"));
		}
		
/*
		// trim index off of both
		if(link.indexOf("index.") != -1)
			link = link.substr(0, link.indexOf("index."));
		
		if(url.indexOf("index.") != -1) {
			console.log(url);
			url = url.substr(0, url.indexOf("index."));
			console.log(url);
		
		// trim down the url to the length of the string
		var urlEnd = url.substr(url.length-link.length);


		// if they're the same, we're on it
		return urlEnd == link;
*/
		return (url.indexOf(link) != -1);
	}		


})(jQuery);
