var GayaDesign = {
	
	smokeAnimate: new Array(),
		
	init: function() {	
		//header effects
		$(".heading_logo a img").mouseenter(function() {
			
			$(this).stop().animate({
				marginTop: '-40px'
			}, 200);
			
		});
		
		$(".heading_logo a img").mouseleave(function() {
			
			$(this).stop().animate({
				marginTop: '-179px'
			}, 200);
			
		});
		
		//tab effects
		$(".tab_item").mouseover(function() {
			$(this).css({
				fontWeight: "bold"
			});
		});
		$(".tab_item").mouseout(function() {
			$(this).css({
				fontWeight: "normal"
			});
		});
		
		//navigation effects
		$(".sky .pages a").mouseover(function() {
			$(this).stop().animate({
				paddingTop: "30px"
			},{
				duration: 100
			});
		});
		$(".sky .pages a").mouseout(function() {
			$(this).stop().animate({
				paddingTop: "10px"
			},{
				duration: 100
			});
		});
		
		$(".tab_item").click(function() {
			
			var background = $(this).parent().find(".moving_bg");
			
			$(background).stop().animate({
				left: $(this).position()['left']
			}, {
				duration: 200
			});
			
			GayaDesign.tabContent($(this).text());
			
		});
		
		//chimney pulling
		GayaDesign.makeChimney('chimney1', 270, 81);
		GayaDesign.makeChimney('chimney2', 380, 81);
	},
	
	makeChimney: function(id, posX, posY) {
		//set CSS
		$('#' + id).css({
			top: posY + "px",
			left: ($("#chimpos").offset()['left'] + posX) + "px",
			display: "block"
		});
		
		//set draggable
		$('#' + id).draggable({
			containment: 'parent',
			stop: function(event, ui) { GayaDesign.dropChimney($('#' + id)) }
		});
		
		//init smoke animations
		GayaDesign.smokeAnimate[id] = true;
		var time = (Math.floor(Math.random()*3001));
		setTimeout("GayaDesign.smokeAnimation('" + id + "')", time);
	},
	
	smokeAnimation: function(id) {
		
		if (GayaDesign.smokeAnimate[id] == true) {
			var puff = document.createElement("IMG");
			
			$(puff).attr("src", "http://www.gayadesign.com/wp-content/themes/gdblocked/images/smoke.png");
			$(puff).attr("alt", "puffffff");
			$(puff).attr("class", "puff");
			var tempId = "puff" + Math.floor(Math.random()*1001);
			$(puff).attr("id", tempId)
			
			$(puff).appendTo("#header");
			
			var chimneyPos = $('#' + id).offset();
			
			$(puff).css({
				top: (chimneyPos['top'] + 12) + "px",
				left: (chimneyPos['left'] + 24) + "px",
				zIndex: 25,
				opacity: 0.4
			});
			$(puff).animate({
				width: "80px",
				height: "45px",
				marginLeft: "-40px",
				marginTop: "-60px",
				opacity: 0.9
			},{
				duration: 1500
			}).animate({
				marginTop: "-160px",
				opacity: 0.0
			},{
				duration: 2500
			});
			
			var time = 5500 + (Math.floor(Math.random()*4501));
			
			setTimeout("GayaDesign.smokeAnimation('" + id + "')", time);
			setTimeout("$('#" + tempId + "').remove()", 4200);
		}
	},
	
	dropChimney: function(obj) {
		
		var beyondLeft = ((($(obj).offset()['left'] + ($(obj).outerWidth() - 25)) - $("#content").offset()['left']) < 0);
		var beyondRight = $(obj).offset()['left'] - $("#content").offset()['left'] - $("#content").outerWidth() > 0; 
		
		if (beyondLeft || beyondRight) {
			var fallDist = ($("#grass").offset()['top'] + 100) - $(obj).outerHeight();
			var time = 3000;
			
			//stop smoke + draggable
			$(obj).css({
				cursor: "default"
			})
			GayaDesign.smokeAnimate[$(obj).attr("id")] = false;
			$(obj).draggable('destroy');
		} else {
			var fallDist = ($("#header").outerHeight() - 16) - $(obj).outerHeight();
			var time = 700;
		}
		
		$(obj).stop().animate({
			top: fallDist+"px"
		}, {
			duration: time,
			easing: "easeOutBounce"
		});
		
	},
	
	tabContent: function(text) {
		text = jQuery.trim(text);
		
		switch(text) {
			case "Latest posts":
				$("#posts_tabslider").stop().animate({
					marginLeft: "0px"
				}, {
					duration: 200
				});
			break;
			case "Top posts":
				$("#posts_tabslider").stop().animate({
					marginLeft: "-290px"
				}, {
					duration: 200
				});
			break;
		}
	},
   
   showNinjas: function () {
      
      var basic_url = "http://www.gayadesign.com/wp-content/themes/gdblocked/images/";
      
      var width = $(window).width();
      var height = $(window).height();
      
      var scrollTop = $(window).scrollTop();
      
      for (var i = 0; i < 30; i++) {
         var random = Math.floor(Math.random()*6);
         var top = Math.floor(Math.random()*height) + scrollTop;
         var left = Math.floor(Math.random()*(width - 40));
         
         var src = basic_url + "ninja" + (random + 1) + ".png";
         var ninja = $("<img />");
         $(ninja).attr("src", src);
         $(ninja).css({
            top: top,
            left: left,
            position: "absolute",
            zIndex: "6666"
         }).appendTo("html");
      }
      
   }
		
}

$(document).ready(function() {
	GayaDesign.init();
   
   $(window).konami(function () {
      GayaDesign.showNinjas();
   });
});

(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);
				}
			}, true);
		});
	}

})(jQuery);
