/*
 * jQuery Raptorize Plugin 1.0
 * www.ZURB.com/playground
 * Copyright 2010, ZURB
 * Free to use under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
*/


(function($) {

    $.fn.raptorize = function(options) {

        //Yo' defaults
        var defaults = {  
            enterOn: 'click', //timer, konami-code, click
            delayTime: 5000 //time before raptor attacks on timer mode
            };  
        
        //Extend those options
        var options = $.extend(defaults, options); 
	
        return this.each(function() {

			var _this = $(this);
			var audioSupported = false;
			//Stupid Browser Checking which should be in jQuery Support
			if ($.browser.mozilla && $.browser.version.substr(0, 5) >= "1.9.2" || $.browser.webkit) { 
				audioSupported = true;
			}
			
			//Raptor Vars
			var jabbaImageMarkup = '<img id="jabba" style="display: none" src="jabba.png" />';
			var raptorImageMarkup = '<img id="elRaptor" style="display: none" src="jarjar.gif" />';
			
			var jabbaAudioMarkup = '<audio id="elJabbaShriek" preload="auto"><source src="jabba.mp3" /><source src="jabba.ogg" /></audio>';
			var raptorAudioMarkup = '<audio id="elRaptorShriek" preload="auto"><source src="jarjar.mp3" /><source src="jarjar.ogg" /></audio>';	
			
			var locked = false;
			
			//Append Raptor and Style
			$('body').append(jabbaImageMarkup);
			$('body').append(raptorImageMarkup);
 			if(audioSupported) { $('body').append(jabbaAudioMarkup); }
 			if(audioSupported) { $('body').append(raptorAudioMarkup); }
			var raptor = $('#elRaptor').css({
				"position":"fixed",
				"bottom": "-800px",
				"right" : "0",
				"display" : "block",
				"z-index":"99999"
			})
			
			var jabba = $('#jabba').css({
				"position":"fixed",
				"bottom": "-800",
				"right" : "-100px",
				"width" : "0",
				"height": "0", 
				"display" : "block",
				"opacity":"1",
				"z-index":"99999"
			})
			
			
			
			// Animating Code
			function init() {
				locked = true;
			
				//Sound Hilarity
				if(audioSupported) { 
					function playSound() {
						document.getElementById('elRaptorShriek').play();
					}
					playSound();
				}
				
				
				setTimeout( function(){
					//Sound Hilarity
					if(audioSupported) { 
						function playSound() {
							document.getElementById('elJabbaShriek').play();
						}
						playSound();
					}
				}
					,5400);
				
								
				// Movement Hilarity	
				raptor.animate({
					"bottom" : "0"
				}, function() { 			
					$(this).animate({
						"bottom" : "-140px"
					}, 100, function() {
						var offset = (($(this).position().left)+400);
						$(this).delay(300).animate({
							"right" : offset
						}, 3700, 'easeInSine', function() {
							raptor = $('#elRaptor').css({
								"position":"fixed",
								"bottom": "-800px",
								"right" : "0",
								"display" : "block"
							})
	
							
							locked = false;
						})
					});
				});
				
				
					jabba.delay(5400).animate({
						"height" : "739px",
						"width" : "1676px",
						"opacity":"1",
						"bottom" : "0"
					}, 3000, 'easeOutBounce', function() {	
						$(this).animate({
							"bottom" : "-140px",
							"width" : "1257px",
							"height" : "595px"
						},
						2000,
						function() {
							$(this).delay(300).animate({
								"bottom" : "0px",
								"right" : "500px",
								"height": "155px", 
								"width" : "328px"
							}, 1500, function() {
								jabba = $('#jabba').css({
									"position":"fixed",
									"bottom": "0",
									"right" : "500px",
									"width" : "328px",
									"height": "155px", 
									"display" : "block",
									"opacity":"1"
								})
								
								
								locked = false;
							})
						});
						
					})
					
				
			}
			
			
			//Determine Entrance
			if(options.enterOn == 'timer') {
				setTimeout(init, options.delayTime);
			} else if(options.enterOn == 'click') {
				_this.bind('click', function(e) {
					e.preventDefault();
					if(!locked) {
						init();
					}
				})
			} else if(options.enterOn == 'konami-code'){
			    var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
			    $(window).bind("keydown.raptorz", function(e){
			        kkeys.push( e.keyCode );
			        if ( kkeys.toString().indexOf( konami ) >= 0 ) {
			        	init();
			        	$(window).unbind('keydown.raptorz');
			        }
			    }, true);
	
			}
			
        });//each call
    }//orbit plugin call
})(jQuery);


