/**
 * Resize Events
 * @version 0.7
 *
 * This extension is based on the 'text resize' events work of Lawrence Carvalho <http://www.alistapart.com/articles/fontresizing/>.
 *
 * @author Lawrence Carvalho <carvalho@uk.yahoo-inc.com>
 * @author Andrew Ramsden <http://irama.org/>
 * @see http://irama.org/web/dhtml/resize-events/
 * @license GNU GENERAL PUBLIC LICENSE (GPL) <http://www.gnu.org/licenses/gpl.html>
 * @requires jQuery (tested with 1.4.2) <http://jquery.com/>
 */
var ResizeEvents={baseTextHeight:null,currentTextHeight:null,baseWindowWidth:null,baseWindowHeight:null,currentWindowWidth:null,currentWindowHeight:null,initialised:false,intervalReference:null,textSizeTestElement:null,eventElement:$(document),conf:{textResizeEvent:'x-text-resize',windowResizeEvent:'x-window-resize',windowWidthResizeEvent:'x-window-width-resize',windowHeightResizeEvent:'x-window-height-resize',initialResizeEvent:'x-initial-sizes',pollFrequency:500,textSizeTestElId:'text-resize'}};(function($){ResizeEvents.bind=function(events,handler){$(function(){if(ResizeEvents.initialised!==true){ResizeEvents.initialise();}});ResizeEvents.eventElement.bind(events,handler);};ResizeEvents.initialise=function(){if(ResizeEvents.initialised===true){return;} ResizeEvents.textSizeTestElement=$('<span id="'+ResizeEvents.conf.textSizeTestElId+'" style="position: absolute; left: -9999px; bottom: 0; '+'font-size: 100%; font-family: Courier New, mono; margin: 0; padding: 0;">&nbsp;</span>').get(0);$('body').append(ResizeEvents.textSizeTestElement);windowWidthNow=$(window).width();windowHeightNow=$(window).height();textHeightNow=getTextHeight();ResizeEvents.baseTextHeight=textHeightNow;ResizeEvents.currentTextHeight=textHeightNow;ResizeEvents.baseWindowWidth=windowWidthNow;ResizeEvents.currentWindowWidth=windowWidthNow;ResizeEvents.baseWindowHeight=windowHeightNow;ResizeEvents.currentWindowHeight=windowHeightNow;if(ResizeEvents.intervalReference==null){ResizeEventsPoll();ResizeEvents.intervalReference=window.setInterval('ResizeEventsPoll()',ResizeEvents.conf.pollFrequency);} ResizeEvents.eventElement.trigger(ResizeEvents.conf.initialResizeEvent,[emPixelNow,textHeightNow,windowWidthNow,windowHeightNow]);ResizeEvents.initialised=true;};ResizeEventsPoll=function(){windowWidthNow=$(window).width();windowHeightNow=$(window).height();textHeightNow=getTextHeight();emPixelNow=windowWidthNow/textHeightNow;widthChanged=false;if(ResizeEvents.currentWindowWidth!=windowWidthNow){ResizeEvents.eventElement.trigger(ResizeEvents.conf.windowWidthResizeEvent,[emPixelNow,textHeightNow,windowWidthNow,windowHeightNow]);ResizeEvents.eventElement.trigger(ResizeEvents.conf.windowResizeEvent,[emPixelNow,textHeightNow,windowWidthNow,windowHeightNow]);ResizeEvents.currentWindowWidth=windowWidthNow;widthChanged=true;} if(ResizeEvents.currentWindowHeight!=windowHeightNow){ResizeEvents.eventElement.trigger(ResizeEvents.conf.windowHeightResizeEvent,[emPixelNow,textHeightNow,windowWidthNow,windowHeightNow]);if(!widthChanged){ResizeEvents.eventElement.trigger(ResizeEvents.conf.windowResizeEvent,[emPixelNow,textHeightNow,windowWidthNow,windowHeightNow]);} ResizeEvents.currentWindowHeight=windowHeightNow;} if(ResizeEvents.currentTextHeight!=textHeightNow){ResizeEvents.eventElement.trigger(ResizeEvents.conf.textResizeEvent,[emPixelNow,textHeightNow,windowWidthNow,windowHeightNow]);ResizeEvents.currentTextHeight=textHeightNow;}};getTextHeight=function(){return ResizeEvents.textSizeTestElement.offsetHeight+'';};})(jQuery);

(function($){
	$.fn.navigatify = function(options) {		
		return this.each(function() {
			
			var primaryNavigation = $(this),
				primaryNavigationBottomPadding = parseInt(primaryNavigation.css('margin-bottom'), 10),
				primaryNavigationActive = $('ul.horizontalNavigation > li.active', primaryNavigation);
			
			if(!$(this).hasClass('active')) {
				
				$(this).addClass('active');		
				$('ul.horizontalNavigation > li > a', primaryNavigation).each(function() {
//					$(this).parent().find('div.panel').prepend('<div class="arrow" /><div class="title"><a /></div>');
//					$(this).parent().find('div.panel div.title a').text($(this).text()).attr('href', $(this).attr('href')).attr('class', $(this).attr('class'));
			
					$(this).unbind('click');
					$(this).click(function() {
						var panel = $(this).parent().find('div.panel');

						$(".arrow").hide();
						$(".title").hide();
						
						panel.prepend('<div class="arrow" /><div class="title"><a /></div>');
						panel.find('div.title a').text($(this).text()).attr('href', $(this).attr('href')).attr('class', $(this).attr('class'));

						if(panel.find(".verticalNavigation li").size() == 0) return true;
						if(panel.css('display') == 'none') {
							$('ul.horizontalNavigation > li', primaryNavigation).removeClass('active open');
							$(this).parent().addClass('active open');
							$(this).parent().find('.mheader').attr("href","");
							$('div.panel', primaryNavigation).hide();
							var container = primaryNavigation.find('#primaryNavigationWrapper');
							panel.css({
								'position': 'absolute',
								'top': Math.floor(container.offset().top + container.outerHeight()),
								'left': container.offset().left,
								'width' : container.outerWidth() - parseInt(panel.css('padding-left'), 10) - parseInt(panel.css('padding-right'), 10)
							});
							$('div.arrow', $(this).parent()).css({
								'position': 'relative',
								'left': $(this).offset().left + ($(this).outerWidth() / 2) - ($('div.arrow', $(this).parent()).width() / 2) - container.offset().left - parseInt(panel.css('padding-left'), 10)
							});
							primaryNavigation.animate({marginBottom: panel.outerHeight()}, 'fast');
							panel.slideDown('fast');
						} 
						return false;
					});
			
				});
			
			} else {
				
				$('ul.horizontalNavigation > li.open > a', primaryNavigation).each(function() {
					var panel = $(this).parent().find('div.panel'),
						container = primaryNavigation.find('#primaryNavigationWrapper');
					panel.css({
						'top': Math.floor(container.offset().top + container.outerHeight()),
						'left': container.offset().left,
						'width' : container.outerWidth() - parseInt(panel.css('padding-left'), 10) - parseInt(panel.css('padding-right'), 10)
					});
					$('div.arrow', $(this).parent()).css({
						'position': 'relative',
						'left': $(this).offset().left + ($(this).outerWidth() / 2) - ($('div.arrow', $(this).parent()).width() / 2) - container.offset().left - parseInt(panel.css('padding-left'), 10)
					});
					primaryNavigation.animate({marginBottom: panel.outerHeight()}, 'fast');
				});
				
			}
		
		});
		
	};
})(jQuery);

$(window).load(function() {
	
	ResizeEvents.eventElement.bind('x-text-resize x-window-resize', function() {	
		$('#primaryNavigation').navigatify();			
	});
	
	ResizeEvents.initialise();
	$(document).trigger('x-text-resize');			
});

