
	/**
	 * Lock the control bar in place when the user scrolls beyond the fold
	 **/
	// Calculator the offset of the control bar to determine the threshold 

	var controlBar = {
		mainControlOffsetTop: 0,
		mainControlHeight: 0,
		ignoreNextHover: false,
		timeOut:null,
		isHiddenFlag:true,
		hasSubnav:false,
		
		init: function() {						
			
			// Disable scroll effect
			if(jQuery('#control-bar').hasClass("no-scroll"))	{
				return;
			}	
			var that = this;
			
			
			this.mainControlOffsetTop = 0;
			this.mainControlHeight = jQuery('#control-bar').height();
			
					
			// Look for user state in cookie 0 or null is normal, 1 is expanded 
					
			// Place the bar on load
			this.setPosition();

			jQuery('#control-bar').bind('mouseleave', function() {
				if(that.isHidden()) { return; }
				clearTimeout(that.timeOut);
				that.timeOut = setTimeout(function() {
					that.toggle();
				},400);
			}).bind('mouseenter', function(e) { 
				if(that.isHidden()) { return; }
				clearTimeout(that.timeOut);
			});

			// Hover on mini bar
			jQuery('#control-bar-mini').hover(function() {
				if(!that.ignoreNextHover) {
					that.toggle();
					// when the mouse leaves the bar set a timeout to close it again 
				} else {
					that.ignoreNextHover = false;
				}
			});
			
			// Fire onscroll
			jQuery(window).scroll(function() {
				that.setPosition();
			});
		},
		
		isHidden: function() {
			return this.isHiddenFlag;			
		},
		
		setHidden: function(hidden) {
			return this.isHiddenFlag = hidden;			
			
		},
			
		toggle: function() {

			var that = this;
			if(this.isHidden()) {
				// If in hidden state, slide main bar down and hide mini
				jQuery('#control-bar-mini').slideUp('fast', function() {
					that.setHidden(false);
					jQuery('#control-bar').hide();
					that.attachMiniControlBar();
					that.detachMainControlBar();
					if(that.hasSubnav) {
						that.cloneSubnav();
					}
				});			
			} else {
				// Main controls are not hidden, let's hide them and show mini
				jQuery('#control-bar').slideUp('fast', function() {
					that.setHidden(true);
					that.detachMiniControlBar();
					that.attachMainControlBar();
					if(that.hasSubnav) {
						that.removeSubnav();						
					}

				});
			}
		},
		
		cloneSubnav: function() {
			// Cache parent
			if(!this.subnavParentElement) {
				this.subnavParentElement = jQuery("#control-bar-subnav").parent();
			}
			//console.log(this.subnavParentElement);
			jQuery("#control-bar-subnav").appendTo('#control-bar').hide().slideDown(400);
		},
		
		removeSubnav: function() {
			jQuery("#control-bar-subnav").appendTo(this.subnavParentElement);
		},

		setPosition: function() {
			if(!this.isHidden()) {
				if (jQuery(window).scrollTop() > this.mainControlOffsetTop) {
					this.detachMainControlBar();
				} else {
					this.attachMainControlBar();
				}		
			} else {

				if (jQuery(window).scrollTop() > (this.mainControlOffsetTop + this.mainControlHeight)) {
					this.detachMiniControlBar();
				} else {
					this.attachMiniControlBar();
				}
 			} 
		},

		detachMainControlBar: function() {
			jQuery('#control-bar').slideDown('fast').css({'position' : 'fixed', 'top' : '0', 'border-top' : '1px solid #ccc' });
			jQuery('#toggle-control').fadeIn('fast');
		},
		
		attachMainControlBar: function() {
			jQuery('#control-bar').slideDown('fast').css({'position' : 'absolute', 'top' :this.mainControlOffsetTop + 'px', 'border-top' : '1px solid #ccc' });
			jQuery('#toggle-control').fadeOut('fast');	
			this.setHidden(true);
		},
		
		detachMiniControlBar: function() {
			jQuery('#control-bar-mini').slideDown('fast').css({'position' : 'fixed', 'top' : '0','border-top' : '1px solid #ccc' });
		},
		
		attachMiniControlBar: function() {
			jQuery('#control-bar-mini').css("display","none");
			//jQuery('#control-bar-mini').slideUp('fast');			
			//console.log("x");
		}
				
	};

	// Fire onload
	controlBar.init();


	

