/*
 * StickyBar
 * Created: 2009 Jeffrey Way (http://www.jeffrey-way.com)
 */



/*
 * Cookie plugin
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery.cookie=function(a,b,c){if(typeof b!='undefined'){c=c||{};if(b===null){b='';c.expires=-1}var d='';if(c.expires&&(typeof c.expires=='number'||c.expires.toUTCString)){var e;if(typeof c.expires=='number'){e=new Date();e.setTime(e.getTime()+(c.expires*24*60*60*1000))}else{e=c.expires}d='; expires='+e.toUTCString()}var f=c.path?'; path='+(c.path):'';var g=c.domain?'; domain='+(c.domain):'';var h=c.secure?'; secure':'';document.cookie=[a,'=',encodeURIComponent(b),d,f,g,h].join('')}else{var j=null;if(document.cookie&&document.cookie!=''){var k=document.cookie.split(';');for(var i=0;i<k.length;i++){var l=jQuery.trim(k[i]);if(l.substring(0,a.length+1)==(a+'=')){j=decodeURIComponent(l.substring(a.length+1));break}}}return j}};


/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */		

(function(d){d.each(['backgroundColor','borderBottomColor','borderLeftColor','borderRightColor','borderTopColor','color','outlineColor'],function(i,b){d.fx.step[b]=function(a){if(a.state==0){a.start=getColor(a.elem,b);a.end=getRGB(a.end)}a.elem.style[b]="rgb("+[Math.max(Math.min(parseInt((a.pos*(a.end[0]-a.start[0]))+a.start[0]),255),0),Math.max(Math.min(parseInt((a.pos*(a.end[1]-a.start[1]))+a.start[1]),255),0),Math.max(Math.min(parseInt((a.pos*(a.end[2]-a.start[2]))+a.start[2]),255),0)].join(",")+")"}});function getRGB(a){var b;if(a&&a.constructor==Array&&a.length==3)return a;if(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))return[parseInt(b[1]),parseInt(b[2]),parseInt(b[3])];if(b=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(a))return[parseFloat(b[1])*2.55,parseFloat(b[2])*2.55,parseFloat(b[3])*2.55];if(b=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(a))return[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],16)];if(b=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(a))return[parseInt(b[1]+b[1],16),parseInt(b[2]+b[2],16),parseInt(b[3]+b[3],16)];return e[d.trim(a).toLowerCase()]}function getColor(a,b){var c;do{c=d.curCSS(a,b);if(c!=''&&c!='transparent'||d.nodeName(a,"body"))break;b="backgroundColor"}while(a=a.parentNode);return getRGB(c)};var e={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);		

(function($) {

	$.fn.stickyBar = function(options) {

		var options = $.extend({
			backgroundColor : '#292929',
			closeButtonText : 'Hide Me',
			color : 'white',
			cookie : true,
			hoverBG : '#3e3e3e',
			imagesFolderPath : 'http://newsbees@newsbees.net/newsbees_nl/images/',
			pos : 'bottom',
			displayHideButton : true
		}, options);
		
		return this.each(function() {
			var $this = $(this),
				listItems = $this.find('li'),
				liAnchors = $(listItems).children('a'),
				subUL = $(listItems).children('ul, ol, dl')[0] ? $(listItems).children('ul, ol, dl') : null;
			
			if( $.cookie('bottomBar')) {
				$this.remove();
			}
			
			else {
				$this.css({
					'background' : options.backgroundColor,
					'zIndex' : 10000
				});
			
				// set position of sub-menu
				if ( subUL ) { 
					var subHeight = $(subUL).height();		
					if ( options.pos === 'bottom' ) {			
				   	$(subUL).css('top', '-' + subHeight + 'px');
				   }
				   else if ( options.pos === 'top' ) {
				      $(subUL).css('top', $this.height() + 'px');
				   }
				}
				
				// set color of anchor tags
				if ( options.color) {	
					$(liAnchors).css({
						'color' : options.color,
						'textDecoration' : 'none'
					});
				}
				
				if ( options.pos === 'top' ) {
				   $this.css('top', '0');
				}
		
				
			
			
				$(listItems)
					.css('background', options.backgroundColor) // Css needed for Safari issue, when working with John's color animation plugin. Weird.
					.hover(function() {
						// over
						$(this).stop(true, false)
							.animate({
								backgroundColor : options.hoverBG
								}, 200)
						.css('cursor', 'pointer');
						
					if ( $(this).children()[0] ) {	
						$(this)
							.children('ul, ol, dl')
								.hide()
								.fadeIn(200);
					}
						
				}, function() {
						// out
						$(this).stop(true, false).animate({
							backgroundColor : options.backgroundColor
						}, 200)
						.children('ul, ol, dl')
							.hide();
				});
				
				$this.hover(function() {
					// add close button	
					var $bar = $(this);
               if ( options.displayHideButton ) {
   					if( !$('#closeButton')[0] ) {
   						$('<img />')
   							.attr('src', options.imagesFolderPath + '/shutdown.png');
   								$bar
   									.append(
   										'<li style="cursor: pointer; float: right;">' +
   											'<a href="#">' + 
   												'<img src="' + options.imagesFolderPath + '/shutdown.png" alt="' + options.closeButtonText + '" id="closeButton" alt="Close Me" />' +
   											 	options.closeButtonText + 
   											'</a>' + 
   										'</li>'	
   									)
   									.children('li:last')
   									.click(function() {	
   										closeBar($bar);
   										return false;
   									});
   					} // end if closeButton
					}

				}, function() {	
					 //hover off bar
					 if ( options.displayHideButton ) {
   					 if ( $('img#closeButton')[0] ) {
   					 	$('img#closeButton')
   							.parents('li')
   							.remove();
   					 }
					 }
				});				
			
			} // end else
			
		}); // end each
		
		function closeBar($bar) {
			$bar.animate({
				'height' : 0
			}, 850, 'swing', 
				function() {
					$(this).remove();
					if ( !!options.cookie ) {
						$.cookie("bottomBar", "closed");
					}
				});
		}	
	
	}
	
})(jQuery);
