jQuery.fn.extend({

	manageMenu: function(options){

		/* Options */
		var _options = {
			collapseTime: 800,
			fadeTime: 300,
			flyTime: 150,
			dropWidth: 192,
			parentWidth: 1000
		}

		jQuery.extend(_options, options);

		/* Helper functions */
		function animateMenu(href,type){
			// Determine the animation type
			if(href!=null){
				var _currentLocation = window.location.protocol+'//'+window.location.host+window.location.pathname;
				_currentLocation = _currentLocation.replace(_options.baseUrl, '');
				var _newLocation = href.replace(_options.baseUrl,'');

				// If the link is clicked where we at, do nothing
				if(_currentLocation==_newLocation){ return false; }

				_currentLocation = _currentLocation.split("/");
				_newLocation = _newLocation.split("/");
				console.log(_currentLocation.toSource(),_newLocation.toSource());

				var _totalIterations = _newLocation.length < _currentLocation.length ? _newLocation.length : _currentLocation.length;

				// Iterate over href strings
				for(var _iteration=0;_iteration<_totalIterations;_iteration++){
					if(_currentLocation[_iteration]!=_newLocation[_iteration]){
						if((_iteration==0 && _newLocation[0].length>2) || (_iteration==1 && _newLocation[0].length==2)){
							type='long';
						}else{
							type='short';
						}
						break;
					}else if(_currentLocation[_iteration]==_newLocation[_iteration] && _iteration==_totalIterations-1 && _newLocation.length>_currentLocation.length){
						type='short';
						break;
					}
				}

			}

			var cookie = new Cookie();

			switch(type){
				case 'open':
					var animationType = cookie.read('animationType');

					if(!animationType){ animationType='long'; }

					if(animationType=='long'){
						jQuery('#menu a.collapsable').animate({width: '182px', paddingLeft: '10px', paddingRight: '20px'}, _options.collapseTime, function(){
							if(jQuery('#content-animation').length>0){
								var _height = jQuery('#content-animation').height() > jQuery('#page-image').height() ? jQuery('#content-animation').height() : jQuery('#page-image').height();
								jQuery('#content').animate({height: _height+'px'},_options.collapseTime/2,function(){
									jQuery('#menu li.tab-2 > ul').fadeIn(_options.fadeTime)
									jQuery('#content-animation').animate({top:0,opacity:1},_options.collapseTime);
									jQuery('#page-image').animate({marginLeft: '-225px'},_options.collapseTime/2);
								});
							}
							/*jQuery('#content-animation').animate({marginLeft: 0},_options.collapseTime)*/
							jQuery('#menu li.tab-1 > ul').fadeIn(_options.fadeTime);
						});
					}else if(animationType=='short'){
						jQuery('#menu a.collapsable').css({width: '182px', paddingLeft: '10px', paddingRight: '20px'});
						jQuery('#page-image').css('margin-left','-225px');
						var _height = jQuery('#content-animation').height() > jQuery('#page-image').height() ? jQuery('#content-animation').height() : jQuery('#page-image').height();
						jQuery('#content').animate({height:_height},_options.collapseTime/2,function(){
							jQuery('#content-animation').animate({top:0,opacity:1},_options.collapseTime)
							jQuery('#menu li.tab-2 > ul').fadeIn(_options.fadeTime);
						})
					}
					break;
				case 'long':
					cookie.write('animationType','long');

					// Remove mouseenter bind from menu items, so that dropdowns wouldn't show, when the menu is animating under the mouse.
					jQuery('.droppable', _objectList).unbind('mouseenter');

					// Fade out menus
					var _objectList = jQuery('#menu li.first-level ul');
					if(_objectList.length > 0){
						var _iterations = 0;

						jQuery(_objectList).fadeOut(500, function(){
							_iterations++;

							if(_iterations==_objectList.length){
								// Animation case for content pages
								if(jQuery('#content-animation').length>0){
									jQuery('#content-animation').animate({top:'100%',opacity:0},_options.collapseTime,function(){});
									jQuery('#page-image').animate({marginLeft: '-450px'},_options.collapseTime/2,function(){});
									jQuery('#content').animate({height: '225px'},_options.collapseTime/2, function(){
										// Collapse main menu
										var _subIterations = 0;
										var _subItemCount = jQuery('#menu a.collapsable').length;
										jQuery('#menu a.collapsable').animate({width: 0, paddingLeft: 0, paddingRight: 0}, _options.collapseTime, function(){
											_subIterations++;
											if(_subIterations==_subItemCount){
												window.location = href;
											}
										});
									});
								// Animation case for splash page
								}else{
									jQuery('#menu a.collapsable').animate({width: 0, paddingLeft: 0, paddingRight: 0}, _options.collapseTime, function(){
										window.location = href;
									});
								}
							}
						});
					}else{
						jQuery('#menu a.collapsable').animate({width: 0, paddingLeft: 0, paddingRight: 0}, _options.collapseTime, function(){
							window.location = href;
						});
					}
					break;
				case 'short':
					cookie.write('animationType','short');

					var _objectList = jQuery('#menu li.first-level ul');
					if(_objectList.length > 0){
						// Remove mouseenter bind from menu items, so that dropdowns wouldn't show, when the menu is animating under the mouse.
						jQuery('.droppable', _objectList).unbind('mouseenter');
						
						if (jQuery('#content-animation').length > 0) {
							jQuery('#content').animate({height:'225px'},_options.collapseTime/2, function(){
								jQuery('#content-animation').animate({top: '100%',opacity: 0}, _options.collapseTime);
								jQuery(_objectList).fadeOut(_options.fadeTime, function(){
									window.location = href;
								});
							});
						}
					}
					break;
				default:
			}
		}
		
		function bindEvents(_objectList){
			jQuery('a', _objectList).bind('click', function(event){
				animateMenu(jQuery(this).attr('href'),null);
				return false;
			});

			jQuery('.droppable', _objectList).bind('mouseenter', function(){
				var _drop = jQuery(this).children('ul');
				jQuery(this).addClass('hover-trail');
				jQuery(_drop).fadeIn(_options.fadeTime);
			}).bind('mouseleave', function(){
				var _drop = jQuery(this).children('ul');
				jQuery(this).removeClass('hover-trail');
				jQuery(_drop).fadeOut(_options.fadeTime);
			});

			var _left;

			jQuery('ul li','.first-level').bind('mouseenter',function(){
				var _offset = jQuery(this).offset();

				if(_offset.left+2*_options.dropWidth<_options.parentWidth){
					var _dropPosition = _isIE7 ? _options.dropWidth-40 : _options.dropWidth;
					_position = _dropPosition+'px';
				}else{
					var _dropPosition = _isIE7 ? _options.dropWidth+41 : _options.dropWidth+2;
					_position = '-'+_dropPosition+'px';
				}

				jQuery(this).addClass('hover-trail');
				jQuery('ul',jQuery(this)).css('left',_position).fadeIn(_options.flyTime);
			}).bind('mouseleave',function(){
				jQuery(this).removeClass('hover-trail');
				jQuery('ul',jQuery(this)).fadeOut(_options.flyTime);
			});

		}

		return this.each(function(){
			var _this = jQuery(this);

			animateMenu(null,'open');

			bindEvents(_this);
		})

	},
	
	fixToScreen: function(){

		_options = {
			offsetTop: 122,
			offsetBottom: 90,
			picHeight: 395
		}


		function bindEvents(_element){
			jQuery(window).bind('scroll',function(){
				var _scroll = jQuery(window).scrollTop();
				var _maxScroll = jQuery(document).height()-_options.offsetBottom-_options.picHeight;
				var _position;
				
				if(_scroll<_options.offsetTop){
					_position = 0;
				}else if(_scroll>_maxScroll){
					_position = _maxScroll-_options.offsetTop+'px';
				}else{
					_position = (_scroll-_options.offsetTop)+'px'
				}

				jQuery(_element).stop().animate({marginTop: _position},100)
			})

		}

		return this.each(function(){
			var _this = jQuery(this);

			bindEvents(_this);
		})

	}

})



function Cookie(){

	this.write = function(_name,_value){
		document.cookie = _name+"="+_value+"; path=/";
	}

	this.read = function(_name){
		var nameMatch = _name+"=";
		var crumbs = document.cookie.split(';');

		for(var i=0;i<crumbs.length;i++){
			var crumb=crumbs[i];

			while(crumb.charAt(0)==' '){
				crumb=crumb.substring(1,crumb.length);
			}

			if(crumb.indexOf(nameMatch)==0){
				return crumb.substring(nameMatch.length,crumb.length);
			}
		}
		return null;
	}

	this.erase = function(_name){
		this.create(_name,'',-1);
	}

}

