/**
 * @author Keiran
 */
 

$(document).ready(function(){
	$("a.icon").hover(
      function () {
        $('#iconBubble').html($(this).attr('name')).css('left',$(this).attr('alt')+"px").show();
      }, 
      function () {
        $('#iconBubble').hide();
      }
    );
	
	if(checkHash()) {
		setSlider();
		roundCards();
	}
});

$(window).bind('resize', function() {
	updateCards();
});

function checkHash() {
	hash = (window.location.hash).replace("#","");

	if(hash && hash != "") {
		var pieces = hash.split("/");

		$('#cards').fadeOut('fast');
		$('#loader').fadeIn('fast');
		$('#currentPageIcon').fadeOut('fast');
		$('#currentPageName').fadeOut('fast');
		$('#slider').slider('destroy');
		if(pieces[0] == "featured") {
			url = "/";
		} else {
			url = "/" + pieces[0];
		}
		$.ajax({
			type: "POST",
			url: url,
			data: {'cards':'1'},
			success: function(msg){
				$("#cards").html(msg);
				$("#cards").fadeIn('fast');
				$('#currentPageIcon').removeClass().addClass('icon').addClass(pieces[0]).fadeIn('fast');
				$('#currentPageName').html(pieces[0].toUpperCase()).fadeIn('fast');
				
				if(pieces[1] && pieces[1] != "") {
					var value = $('.cardHolder').index($('.cardHolder[alt="' + pieces[1] + '"]'));
		
					var windowWidth = $(window).width();
					var windowCenter = windowWidth/2;
					var selectedCardLeft = windowCenter - 463;
					var newLeft = (value * -927) + selectedCardLeft;
					$('#cards').animate({ left:newLeft+"px" },400);
					$('#slider').slider('value',(value*927));
					
					cards = $("#cards").children(".cardHolder");
					$('#cardTitle').html($(cards[value]).attr('name').toUpperCase());
					$('#indexLabel').html(value+1 + " / " + $(cards).length);
					
					window.location.hash = hash;
				}
			}
		});
		
		return false;
	} else {
		return true;
	}
}

function setSlider() {
	length = $("#cards").children(".cardHolder").length;
	$("#cards").css('width',(length * 927)+'px');
	
	$("#slider").slider({ max:((length-1) * 927),min:0 });
	$('#slider').bind('slide', function(event, ui) {
		updateCards();
	});
	$('#slider').bind('slidestop', function(event, ui) {
  		roundCards();
	});
}

function updateCards() {
	//$('#cardTitle').html('');
	value = $('#slider').slider('value');
	windowWidth = $(window).width();
	windowCenter = windowWidth/2;
	selectedCardLeft = windowCenter - 463;
	newLeft = (value * -1) + selectedCardLeft;
	$('#cards').css('left',newLeft+"px");
}

function roundCards() {
	$('#loader').fadeOut('fast');
	var value = $('#slider').slider('value');
	value = Math.round(value/927);
	var windowWidth = $(window).width();
	var windowCenter = windowWidth/2;
	var selectedCardLeft = windowCenter - 463;
	var newLeft = (value * -927) + selectedCardLeft;
	$('#cards').animate({ left:newLeft+"px" },400);
	$('#slider').slider('value',(value*927));
	
	cards = $("#cards").children(".cardHolder");
	$('#cardTitle').html($(cards[value]).attr('name').toUpperCase());
	$('#indexLabel').html(value+1 + " / " + $(cards).length);
	
	window.location.hash = $('#currentPageName').html().toLowerCase() + "/" + $(cards[value]).attr('alt');
}

function gotoCard(card) {
	var value = $('.cardHolder').index($(card));
	
	var windowWidth = $(window).width();
	var windowCenter = windowWidth/2;
	var selectedCardLeft = windowCenter - 463;
	var newLeft = (value * -927) + selectedCardLeft;
	$('#cards').animate({ left:newLeft+"px" },400);
	$('#slider').slider('value',(value*927));
	
	cards = $("#cards").children(".cardHolder");
	$('#cardTitle').html($(cards[value]).attr('name').toUpperCase());
	$('#indexLabel').html(value+1 + " / " + $(cards).length);
	
	window.location.hash = $('#currentPageName').html().toLowerCase() + "/" + $(card).attr('alt');
}


function changeType(item) {
	if($('#currentPageName').html() != $(item).attr('name')) {
		window.location.hash = $(item).attr('name').toLowerCase();
		$('#cards').fadeOut('fast');
		$('#loader').fadeIn('fast');
		$('#currentPageIcon').fadeOut('fast');
		$('#currentPageName').fadeOut('fast');
		$('#currentPageName').html($(item).attr('name'))
		$('#slider').slider('destroy');
		url = $(item).attr('href');
		$.ajax({
			type: "POST",
			url: url,
			data: {'cards':'1'},
			success: function(msg){
				$("#cards").html(msg);
				$("#cards").fadeIn('fast');
				$('#currentPageIcon').removeClass().addClass('icon').addClass($(item).attr('name').toLowerCase()).fadeIn('fast');
				$('#currentPageName').fadeIn('fast');
			}
		});
	}
}

function toggleInfo(item) {
	var left = $(item).parent().find('.infoLeft');
	var right = $(item).parent().find('.infoRight');

	if(right.css('top') == "454px") {
		if(left.html() != "") {
			left.animate({top:'0'},300);
		}
		if(right.html() != "") {
			right.animate({top:'0'},300);
		}
	} else {
		left.animate({top:'-454px'},300);
		right.animate({top:'454px'},300);
	}
}

function toggleShare(item) {
	bubble = $(item).parent().find('.shareBubble');
	if(bubble.is(":hidden")) {
		bubble.fadeIn();
	} else {
		bubble.fadeOut();
	}
}


//SOCIAL

function tweetThis(item) {
	var msg = $(item).attr("name");
	var textMessage = "\"" + msg + "\"... ";
	
	var url = window.location.href;
	
	BitlyCB.shortenResponse = function(data) {
	    var s = '';
	    var first_result;
	    // Results are keyed by longUrl, so we need to grab the first one.
	    for(var r in data.results) {
	    	first_result = data.results[r]; break;
	    }
	    for (var key in first_result) {
	    	if(key == "shortUrl") {
	    		s = first_result[key].toString();
	    	}
	    }
	    
	    var tweet_url = "http://twitter.com/home?status=" + encodeURIComponent(textMessage + s + "  @aeliox");
		window.open(tweet_url);
    };
	 
	BitlyClient.shorten(url, 'BitlyCB.shortenResponse');
}

function facebookThis(item) {
	var msg = $(item).attr("name");
	var textMessage = "\"" + msg + "\"... ";
	
	var url = window.location.href;
	
	BitlyCB.shortenResponse = function(data) {
	    var s = '';
	    var first_result;
	    // Results are keyed by longUrl, so we need to grab the first one.
	    for(var r in data.results) {
	    	first_result = data.results[r]; break;
	    }
	    for (var key in first_result) {
	    	if(key == "shortUrl") {
	    		s = first_result[key].toString();
	    	}
	    }
	    
	    var face_url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(s) + "&t=" + encodeURIComponent(textMessage);
		window.open(face_url);
    };
	 
	BitlyClient.shorten(url, 'BitlyCB.shortenResponse');
}

 
//KONAMI
var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e) {
  kkeys.push( e.keyCode );
  if ( kkeys.toString().indexOf( konami ) >= 0 ){
    //$(document).unbind('keydown',arguments.callee);
    kkeys = [];
    if($('body').css("background-color") == "rgb(255, 255, 255)") {
   	 	$('body').css("background","black url(/images/bgInvert.png) repeat-x");   
   	} else {
   		$('body').css("background","white url(/images/bg.png) repeat-x"); 
   	}   
  }
});

function changeMain(item) {
	alt = $(item).attr('alt');
	$('.mainLinks').css({'background-position': alt + 'px 0px'});
}

function changeContent(item) {
	$(item).parent().children('a').each(function() {
		alt = $(this).attr("alt");
		on = alt + "On";
		$(this).removeClass(on).addClass(alt);
	});
	alt = $(item).attr("alt");
	on = alt + "On";
	$(item).removeClass(alt).addClass(on);
	url = $(item).attr("href");
	
	$.ajax({
		type: "POST",
		url: url,
		success: function(msg){
			$("#mirContent").html(msg);
		}
	});
}