/*
	Original and full credit to Alasdair McLeay for building the template setup, rocks mate :¬)
	:: Adapted by Toby Brancher 07/2008
*/


(function($) {

    $.holler = $.holler || {};
    
    var c_ready = false;
    var c_self = this;
	var c_members = new Array();
    
    $.fn.holler_navigation = function(options) {
        return this.each(function() {
            
			new $.holler.navigation(this, options);
        });
    };
	

    $.holler.navigation = function(element, options) {
        var i_self = this;
        var i_ready = false;
		
		i_self.container = $(element);
		i_self.containerParent = i_self.container.parent('li');
		i_self.containerParentParent = i_self.containerParent.parent().parent('li'); // Surely a better way to do this
		i_self.containerLink = i_self.container.siblings('a:first');
		i_self.containerImage = i_self.container.siblings('.img:first');
		i_self.outerContainer = $('.id-navigation');
		i_self.i_ready = true;

// Messy...
		if(i_self.containerParentParent.length){
			
			i_self.outerContainer_height = i_self.container.height();
			var hoverIcon = $("<span style=\"vertical-align:middle;margin-right:0.5em; \">&gt;</span>");			
			i_self.containerLink.prepend(hoverIcon);
			i_self.containerLink.hoverIcon = i_self.containerLink.children('span');
			
		} else {
			if(i_self.containerParent.find('ul ul').length){
				//i_self.outerContainer_height = i_self.containerParent.find('ul ul').height();
				i_self.outerContainer_height = 35;
			} else {
				//i_self.outerContainer_height = i_self.containerParent.find('ul').height();
				i_self.outerContainer_height = 35;
			}			
		}	
        
		i_self.options = $.extend({}, $.holler.navigation.defaults, options);
		
		
        i_self.functions = {
	        initialise: function(context) {
		        context.each(function(index) {
		            /*
		                Handle initialisation
		                Should be called on page load and on new content loaded through ajax, so pay attention to the context.
		            */
					
					i_self.functions.closeItem(true);

					i_self.containerLink.click(function(){
						//i_self.functions.closeAllItems();
						this.blur();
						i_self.functions.onClick();
						return false;
						
					});
					/*
					i_self.containerLink.mouseover(function(){
						i_self.functions.onOver();
					}).mouseout(function(){
						i_self.functions.onOut();
					});
					*/
		        });
	        },
			onClick: function() {
				// Disable other interactions
				if(!i_self.containerParent.hasClass('open')){
					i_self.functions.closeAllItems();
					i_self.functions.openItem();
				} else {
					i_self.functions.closeItem();
				}
				
			},
			onOver: function() {
				if(i_self.i_ready){
					i_self.i_ready = false;
					if(i_self.containerParentParent.length){
						i_self.containerLink.hoverIcon.animate({opacity: 1}, 120, 'linear', null, 100);
						i_self.containerLink.animate({left: '0em'}, 120, 'easeOutQuad', function(){
							i_self.i_ready = true;
						});
					}
				}
			},
			onOut: function() {
				if(!i_self.containerParent.hasClass('open')){
					if(i_self.containerParentParent.length){
						i_self.containerLink.hoverIcon.animate({opacity: 0}, 120);
						i_self.containerLink.animate({left: '0em'}, 240, 'easeOutQuad', function(){

						});
					}
				};
				
				
			},
			closeAllItems: function(){
				if(i_self.containerParentParent.length){
					$(c_members).each(function(i){
						// If parent has class open
						
						if(this.containerParent.hasClass('open')){	
							this.functions.closeChild();
							//i_self.functions.trace('closeAll ' + this.containerLink.html() + ' ' + this.containerParent.siblings('li').hasClass('open'));
						};
					});
				} else {
					$(c_members).each(function(i){
						// If parent has class open
						i_self.functions.trace('close_all-parents ' + this.containerParent.attr('href') + ' ' + this.containerParent.hasClass('open'))
						if(this.containerParent.hasClass('open')){
							
							this.functions.closeItem();
							//i_self.functions.trace('closeAll ' + this.containerLink.html() + ' ' + this.containerParent.siblings('li').hasClass('open'));
						};
					});
				}
	        },
			closeItem: function(closeFast){
				// Remove class open (doing this twice at the moment)
				i_self.containerParent.removeClass('open');
				
				if(!i_self.containerParentParent.length){
					if(closeFast){
						i_self.functions.closeParentQuick();
					} else {
						i_self.functions.closeParent();
					};
				// If container child
				} else {
					if(closeFast){
						i_self.functions.closeChildQuick();
					} else {
						i_self.functions.closeChild();
					};
				};
			},
			openItem: function(){				
				if(!i_self.containerParentParent.length){					
					i_self.functions.openParent();
					i_self.removeClass('open');
				// Else container child
				} else {
					i_self.functions.openChild();
				};
			},
			closeParentQuick: function(){
				c_ready = false;
				i_self.functions.trace('closeParentQuick');
				i_self.outerContainer.css({height: '35px'});

				i_self.container.animate({height: 'hide'}, 0, 'linear', function(){
					c_ready = true;
					i_self.containerParent.removeClass('open');
				});
				i_self.container.css({opacity: 0});

	        },
			closeParent: function(){
				c_ready = false;
				i_self.functions.trace('closeParent');
				i_self.outerContainer.animate({height: '35px'}, i_self.options.animationSpeed, i_self.options.easing);
				i_self.container.animate({height: 'hide', opacity: 0}, i_self.options.animationSpeed, i_self.options.easing, function(){
					c_ready = true;
					i_self.functions.closeAllItems();
				});
	        },
			openParent: function(){
				c_ready = false;
				//i_self.functions.trace(i_self.containerParent.attr('class'))
				i_self.outerContainer.animate({height: i_self.outerContainer_height}, i_self.options.animationSpeed, i_self.options.easing);
				i_self.container.animate({height: 'show', opacity: 1}, 500, i_self.options.easing, function(){
					c_ready = true;
					i_self.containerParent.addClass('open');
					
				});
				
	        },
			closeChildQuick: function(){
				c_ready = false;
				i_self.functions.trace('closeChildQuick');
				i_self.containerImage.animate({height: 'hide', opacity: 0}, 0, 'linear', function(){
					i_self.container.find('li').each(function(){
						$(this).animate({opacity: 0}, 0);
					});
					i_self.container.animate({width: 'hide', opacity: 0},  0, 'linear', function(){
						c_ready = true;
						i_self.functions.onOut();
					});
				});
	        },
			closeChild: function(){
				//i_self.functions.trace("closeChild");
				c_ready = false;
				
				i_self.containerImage.animate({height: 'hide', opacity: 0}, i_self.options.animationSpeed, i_self.options.easing, function(){
					i_self.container.find('li').each(function(){
						$(this).animate({opacity: 0}, 300);
					});
					i_self.container.animate({width: 'hide', opacity: 0}, i_self.options.animationSpeed, i_self.options.easing, function(){
						c_ready = true;
						i_self.containerParent.removeClass('open')
						i_self.functions.trace('CLOSE_CHILD ' + i_self.containerLink.attr('href') + ' ' + i_self.containerParent.hasClass('open'))
						i_self.functions.onOut();
					});
				});
	        },
			openChild: function(){
				c_ready = false;
				i_self.functions.trace(i_self.containerParent.attr('class'))
				i_self.container.animate({width: 'show', opacity: 1}, i_self.options.animationSpeed, i_self.options.easing, function(){
					i_self.container.find('li').each(function(){
						$(this).animate({opacity: 1}, 300);
						i_self.containerParent.addClass('open');
					});
					i_self.containerImage.animate({height: 'hide', opacity: 1}, i_self.options.animationSpeed, i_self.options.easing, function(){
						c_ready = true;
						//i_self.containerParent.addClass('open');
						i_self.functions.onOver();
					});
				});
	        },			
	        preload: function() {
		        i_self.functions.debuglog('preload');
	            i_self.functions.preloadImages(/*insert array of images here*/);
            },
	        preloadImages: function() {
		        for(var i = 0; i<arguments.length; i++) {
			        $('<img>').attr('src', arguments[i]);
		        }
	        },
            trace: function(logtext) {
	            try	{
		            //console.log(logtext);
	            }
	            catch(err) {
	            }
            }
	    };
        
		c_members.push(this);
        i_self.functions.initialise($('body'));

        i_ready = true;
    };
    
    // defaults
    $.holler.navigation.defaults = {
        easing: 'easeOutQuart',
        animationSpeed: '600'
        //intervalFrequency: '10000'
    };
	
})(jQuery);



