/*
* Author: Reading Room
* Website: http://www.readingroom.com
*
* Revisions:
* 0.1 
*
* Default usage:
* $('body').weCookie();
* With options:
* $('body').weCookie({
*     'url' : 'http://www.website.com/updated-term-of-use/',* 		
*	  'text' : 'www.website.com uses cookies, read out updated T&Cs here',
*	  'close_icon_path' : '/path/to/image/my_custom_icon.gif',
*	  'fade_in' : '1000'
*	});
* To position at bottom of screen:
* $('body').weCookie({
*	  'top' : 'auto',
*	  'bottom' : '0'
*	});
*
* See below for plugin default options 
*/
(function($) {

    $.fn.weCookie = function(options) {
		
		//plugin default options
        var defaults = {
			'text': 'We use cookies on this site , click this message to find out more', //link text
			'url': 'http://www.aboutcookies.org/Default.aspx?page=3', //url of link
			'fade_in': '5000', //fade in speed
			'weCookie_class': 'weCookie', //class of main div
			'expires': '365', //sets when the cookie will expire in days
			'top': '0', //top position of main div
			'bottom': 'auto', //bottom position of main div
			'padding': '1px', //padding of main div
			'background': '#F0F0F0', //background color
	        'border': '#DFDFDF', //border color
	        'font_size': '1em', //font size
	        'font_family': 'arial,verdana,sans-serif', //font family
			'link_padding': '7px', //padding on link 
	        'link_color': '#003399', //color of link
			'close_class': 'closeCookie', //class of close link
			'close_top': '10px', //top position of close link
			'close_bottom': 'auto', //bottom position of close link
			'close_left': 'auto', //left position of close link
			'close_right': '10px', //right position of close link
	        'close_icon_path' : '/images/close_icon.gif' //path to close image
        };
		//end plugin default options

		//extends defaults with options provided
        if (options) {
			$.extend(defaults, options);
		}
		
		//shorten classes
		var wc_class = '.'+defaults.weCookie_class;
		var wc_close = '.'+defaults.close_class;
		
		//check for cookie
		var x = readCookie('weCookie')
		if (x != 'meLikeCookie'){
			//append weCookie 
			this.prepend('<div class="'+defaults.weCookie_class+'">\
						 	<a href="'+defaults.url+'">'+defaults.text+'</a>\
							<a class="'+defaults.close_class+'" href="#na" title="Close"><img src="'+defaults.close_icon_path+'" alt="Close"/></a>\
						 </div>');
			$(wc_class).hide()
			setTimeout(function(){ $(wc_class).fadeIn() }, defaults.fade_in);
		};
		
		//div styles
		$(wc_class).css({
			'position': 'fixed',
			'width': '100%',
			'text-align': 'center',
			'z-index': '99999',
			'top': defaults.top,
			'bottom': defaults.bottom,
			'padding': defaults.padding,
			'background': defaults.background,
			'border-bottom': '1px solid '+defaults.border+'',
			'border-top': '1px solid '+defaults.border+'',
			'font-size': defaults.font_size,
			'font-family': defaults.font_family
		});
		
		//link styles
		$(wc_class+' a').css({
			'display': 'block',
			'padding': defaults.link_padding,
			'color': defaults.link_color			
		});
		
		//close link styles
		$(wc_close).css({
			'position': 'absolute',
			'top': '10px',
			'right': '10px',
			'padding': '0'		
		});
				
		$(wc_class+' a').click(function(e) {
			
			//Stop the link returning while we set a cookie
			e.preventDefault();
			
			if ($(this).hasClass(defaults.close_class) == true){
				$(wc_class).fadeOut();
				createCookie('weCookie','meLikeCookie',defaults.expires);
				return false;
			} else{
				createCookie('weCookie','meLikeCookie',defaults.expires);
				window.location = defaults.url;
			}
		});
		
		
		//create cookie function  
		function createCookie(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		};
		
		//read cookie function
		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		};

    };

})(jQuery);

/* * *
* KonamiCode Awesomeness
* <http://www.gethifi.com/blog/konami-code-jquery-plugin-pointlessly-easy>
*
*/
(function($) {

	$.fn.konami = function(callback, code) {
		if(code == undefined) code = "38,38,40,40,37,39,37,39,66,65";

		return this.each(function() {
			var kkeys = [];
			$(this).keydown(function(e){
				kkeys.push( e.keyCode );
				if ( kkeys.toString().indexOf( code ) >= 0 ){
					$(this).unbind('keydown', arguments.callee);
					callback(e);
				}
			});
		});
	}

})(jQuery);
