/*----------------------------------------------------------------------
[Global JavaScript ] 
Desc: site wide javascript

Copyright:		Adam Foster 2011
Client:			Klei Entertainment
Project:		Klei Entertainment v2
Created by:		Adam Foster		
-----------------------------------------------------------------------*/

/* Global > replace fonts ---------------------------------------------*/

Cufon.replace('h2,h3,.cufon')('h1, .button, #nav a, .arrow-link, .cufon-a, #post-nav li a', { hover: true });

/* Not looking for any trouble here -----------------------------------*/

var $j = jQuery.noConflict();


/* Global > run some functions  ---------------------------------------*/

$j(document).ready(function() {
	
	horizontalScroller();
		
	if($j("#twitter").html()){
		$j("#twitter").getTwitter();
	};
	
	fixHeight($j('.eq-height'), '.middle');
	
});

/* Global > horizontal scroller ------------------------------------*/

function horizontalScroller(){
	
	$j('.game-scroller').each(function() { var o = new scrollMe($j(this)); });
	function scrollMe(e) {
		
		var o = {
		  init : function() {
				o.wrapper = $j(e);
				o.scroller = $j('#mask > ul', o.wrapper);
				
				//horizontal
				if(o.wrapper.hasClass('h-scroller')){
					o.left = $j('.scroll-left', o.wrapper);
					o.right = $j('.scroll-right', o.wrapper);
					o.width = 307;
				}else {
					o.left = $j('.scroll-up', o.wrapper);
					o.right = $j('.scroll-down', o.wrapper);
					o.width = 172;
				};

				o.current = 0;
				o.total = $j('.game',o.scroller).length;
				if(o.total <=3 ) o.right.addClass('disabled');
				
				o.events();
			},
			events : function() {
				
				o.left.click(function (e) {
					e.preventDefault();
					if (o.current - 1 < 0) return false;
					o.current--;
					o.updateCurrent();
				});	
					
				o.right.click(function (e) {
					e.preventDefault();
					if (o.current + 1 >= (o.total-2)) return false;
					o.current++;
					o.updateCurrent();
				});

			},
			updateCurrent : function() {
				if(o.wrapper.hasClass('h-scroller')){
					o.scroller.stop().animate({left: -(o.current * o.width) + 'px'}, 500, 'easeInOutQuad');
				}else {
					o.scroller.stop().animate({top: -(o.current * o.width) + 'px'}, 500, 'easeInOutQuad');
				};
				if (o.current - 1 < 0){
					o.left.addClass('disabled');
				}else if(o.left.hasClass('disabled')){
					o.left.removeClass('disabled');
				};
				if (o.current + 1 >= (o.total-2)){
					o.right.addClass('disabled');
				}else if(o.right.hasClass('disabled')){
					o.right.removeClass('disabled');
				};
			}
		};
		o.init();		
	};
	
};

/* Home + Games > parrelax character ----------------------------------*/

function parrelax(){
	
	$j('#feature').each(function() { var o = new parrelaxMe($j(this)); });
	function parrelaxMe(e) {
		
		var o = {
			init : function() {
			  					
				/* cache */
				o.wrapper = $j(e);
				
				o.oldx = 0;
				o.moving = false;
				
				o.img = $j("#featured-img img",o.wrapper);
				o.img.orgWidth = 909;
				o.img.myWidth = 909;
				o.img.imgLeft = ((o.img.myWidth - o.img.orgWidth) / 2) * -1;
				o.img.w = parseInt(o.img.orgWidth / 2, 10);
				
				
				o.bg = $j("#feature-bg div",o.wrapper);
				o.bg.orgWidth = o.bg.parent().width();
				o.bg.myWidth = o.bg.parent().width();
				o.bg.imgLeft = ((o.bg.myWidth - o.bg.orgWidth) / 2) * -1;
				o.bg.w = parseInt(o.bg.orgWidth / 2, 10);
				
				o.move();
			  
			},
			
			move : function() {
								
				o.wrapper.mousemove( function (e) {
											   
					if(o.moving || o.img.parent().is(':animated')) return false;
										
					var offset = $j(this).offset();
							
					// Mouse co-ordinates
					var x = parseInt(e.pageX - offset.left, 10);
									
					// Treat the mouse position relative to the middle of the image
					x = (x - o.img.w);
					var x2 = (x - o.bg.w);
					
					// Calculate movement, arbitrarily divided by 12 to reduce the speed
					var myX = parseInt(o.img.imgLeft + ((x) / 35), 10);
					var myX2 = parseInt(o.bg.imgLeft + ((x2) / 120), 10);
					
					if(x > (o.oldx + 400) || x < (o.oldx - 400) ) {
						
						o.moving = true;		
						o.img.stop().animate({left: myX + 'px'}, 100, function(){
							o.moving = false;
						});
						o.bg.stop().animate({left: myX2 + 'px'}, 100);
						
					}else{
					
						if(!o.moving){
							o.img.css({ left: myX + "px" });
							o.bg.css({ left: myX2 + "px" });
						};	
					
					};
				
					o.oldx = x;
						
				});

			
			}
		
		};
		
		o.init();
	
	};
};

/* Global > equal heights -----------------------------------------------*/

function fixHeight(e, element) {
	
	e.each(function(){
		
		var currentTallest = 0;
		$j(this).find(element).each(function(i) {
			if ($j(this).height() > currentTallest) { currentTallest = $j(this).height(); }
		}).css({ 'height': currentTallest });	
		
	});
	
};

/* Blog > Comment count -----------------------------------------------*/

function commentCount(){
	$j('#klei-replies').html( $j('#comments .byuser:not(.depth-1)').length + ' Klei Replies');	
};

function hideShowDates(){
	$j('.news-open').click(function (e) {
		e.preventDefault();
		if($j(this).hasClass("news-is-open")){
			$j(this).removeClass("news-is-open").parent().next('.news-hidden').slideUp("fast");
		}else{
			$j(this).addClass("news-is-open").parent().next('.news-hidden').slideDown("fast");
		};
	}); 
};

/* Global > custom easing ---------------------------------------------*/

$j.easing.quad = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
};

$j.easing.easeInOutQuad = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
};

/* Twitter */

(function($) {

	$.fn.getTwitter = function(options) {
		var o = $.extend({}, $.fn.getTwitter.defaults, options);
	
		$(this).empty().hide();
		$(this).append('<ul id="twitter_update_list"></ul>');
		$("ul#twitter_update_list").hide();
		var pl = $('<p id="'+o.preloaderId+'">'+o.loaderText+'</p>');
		$(this).append(pl);
		$(this).show();
		
		$.getScript("http://twitter.com/javascripts/blogger.js", function() {
			$.getScript("http://api.twitter.com/1/statuses/user_timeline.json?screen_name="+o.userName+"&count="+o.numTweets+"&include_rts=true&callback=twitterCallback2", function() {
				// remove preLoader from container element
				$(pl).remove();
				$("ul#twitter_update_list").show();
			});
		});
	};

	// plugin defaults
	$.fn.getTwitter.defaults = {
		userName: "klei",
		numTweets: 1,
		preloaderId: "preloader",
		loaderText: "Loading tweet...",
		slideIn: false,
		showHeading: false,
		headingText: "",
		showProfileLink: false
	};
})(jQuery);

/* cookie */

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=/";
}


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;
}
