$(document).ready(function() {

	$('ul li:last-child').addClass('last');
	
	/* Game variable */
	var game_name = $('body').attr('id');
	
	/* Leaderboards JSON */
  	$.getJSON('/wp-content/themes/werule/includes/json.php?url=http://app.plusplus.com/1/StarDefense/games/Kingdoms/leaderboards.json', function(data){
  	
	    $.each(data.leaderboards, function(leaderboard){
	      	$.getJSON('/wp-content/themes/werule/includes/json.php?url=http://app.plusplus.com/1/StarDefense/games/Kingdoms/leaderboards/'+this.level+'.json', function(data){
	      		var leaders = $('<div class="board">')
	      		var leadersol = $('<ol>')
	      		leadersol.append($('<li>').text(data.title))
		      	$.each(data.leaderboard.splice(0,5), function(){
		      		var html = "<li>"
					html += "<span class='mask'></span><img src="+this.badge_id+" />"
					html += "<strong><span>"+this.rank+"</span> "+this.gamertag+"</strong>"
					html += "<em>"+this.formatted_score+"</em>"
					html += "</li>"
					leadersol.append(html)
		      	})
		      	leadersol.appendTo(leaders)
		      	leaders.appendTo($('#boards .scroll'))
      		  	num = $('#leaderboards .board').length
      		  	if(num%2 == 1){
					dot = $('<a href="#none" class="dot">dot</a>').appendTo($('#leaderboards .dots'))
					$('#leaderboards .dots a:eq(0)').addClass('selected');
					$('#achievements .dots a:eq(0)').addClass('selected');
					dot.click(function(e){
						e.preventDefault()
						$(this).siblings('.selected').removeClass('selected')
						$(this).addClass('selected')
						$('#leaderboards .board').animate({
							'left': -($(this).index()) * 460
						});
					});
				}
	      	})
	    })
	    
	})

	/* Achievements JSON */
	$.getJSON('/wp-content/themes/werule/includes/json.php?url=http://app.plusplus.com/1/Kingdoms/users/6496/achievements.json', function(data){
		
		while(data.achievements.length > 0){
			var these = data.achievements.splice(0, 10)
			var thisList = $('<ul>');
			$.each(these, function(){
				// If empty achievement icon, replace with We City icon
				if (this.icon_url == "") {
					var icon = "http://www.ngmoco.com/we-farm/files/2010/07/werule.png"
				}
				var html =  "<li>"
				html += "<span class='mask'></span><img src='"+icon+"' />"
				html += "<strong>"+this.name+"<span>"+this.category+"</span></strong>"
				html += "</li>"
				thisList.append(html);
			});
			thisList.appendTo($('#list .scroll'))
			dot = $('<a href="#none" class="dot">dot</a>').appendTo($('#achievements .dots'))
			dot.click(function(e){ 
				e.preventDefault()
				$(this).siblings('.selected').removeClass('selected')
				$(this).addClass('selected')
				$('#achievements .scroll').stop().animate({
					'left': -($(this).index()) * 449
				});
			});

		}
		// achievements.appendTo($('#list .scroll'))
		$('#achievements h4').html('/ ' +data.unlocked_points+ ' points')
		
	})
	
	/* Twitter */
	$.jTwitter('werulegame', 1, function(data){
        $('#twitter').empty();
        $.each(data, function(i, post){
        
        	var timestamp = post.created_at
            
     		var d = Date.parse(timestamp);
			var dateFunc = new Date();
			var timeSince = dateFunc.getTime() - d;
			var inSeconds = timeSince / 1000;
			var inMinutes = timeSince / 1000 / 60;
			var inHours = timeSince / 1000 / 60 / 60;
			var inDays = timeSince / 1000 / 60 / 60 / 24;
			var inYears = timeSince / 1000 / 60 / 60 / 24 / 365;
			
			// in seconds
			if(Math.round(inSeconds) == 1){
				var timestamp = "1 second ago";
			}
			else if(inMinutes < 1.01){
				var timestamp = Math.round(inSeconds) + " seconds ago";
			}
			
			// in minutes
			else if(Math.round(inMinutes) == 1){
				var timestamp = "1 minute ago";
			}
			else if(inHours < 1.01){
				var timestamp = Math.round(inMinutes) + " minutes ago";
			}
			
			// in hours
			else if(Math.round(inHours) == 1){
				var timestamp = "1 hour ago";
			}
			else if(inDays < 1.01){
				var timestamp = Math.round(inHours) + " hours ago";
			}
			
			// in days
			else if(Math.round(inDays) == 1){
				var timestamp = "1 day ago";
			}
			else if(inYears < 1.01){
				var timestamp = Math.round(inDays) + " days ago";
			}
			
			// in years
			else if(Math.round(inYears) == 1){
				var timestamp = "1 year ago";
			}
			else
			{
				var timestamp = Math.round(inYears) + " years ago";
			}
        
            $('#twitter').append(
                        
            	'<div class="box"><div class="inner">'
					+'<a href="http://twitter.com/' + post.user.screen_name + '"><img src="' + post.user.profile_image_url + '" /></a>'
					+'<ul>'
					+	'<li><strong>Followers</strong> ' + post.user.followers_count + '</li>'
					+	'<li><strong>Updates</strong> ' + post.user.statuses_count + '</li>'
					+'</ul>'
					+'<p>' + post.text + '<small><a href="http://twitter.com/' + post.user.screen_name + '/status/' + post.id + '">' + timestamp + '</a></small></p>'
				+'</div></div>'
				         		
            );
        });
    });
		
	/* Media */
	
	$('#media .carousel ul:eq(0)').addClass('gallery-1');
	$('#media .carousel ul:eq(1)').addClass('gallery-2');
	$('#media .carousel ul:eq(2)').addClass('gallery-3');
	
	var firstHeight = $('ul.gallery-1').height() + "px";
	var secondHeight = $('ul.gallery-2').height() + "px";
	var thirdHeight = $('ul.gallery-3').height() + "px";
	
	$('#media .carousel').css("height", firstHeight);
	
	$('#media .tabs li:eq(0)').addClass('active');
	$('#media .tabs li:eq(0) a').click(function(){
		$('#media .carousel').animate({
			height: firstHeight
		}, 300);
		$('#media .carousel ul').fadeOut();
		$('#media .carousel ul:eq(0)').fadeIn();
		$('#media .tabs li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	$('#media .tabs li:eq(1) a').click(function(){
		$('#media .carousel').animate({
			height: secondHeight
		}, 300);
		$('#media .carousel ul').fadeOut();
		$('#media .carousel ul:eq(1)').fadeIn();
		$('#media .tabs li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
	$('#media .tabs li:eq(2) a').click(function(){
		$('#media .carousel').animate({
			height: thirdHeight
		}, 300);
		$('#media .carousel ul').fadeOut();
		$('#media .carousel ul:eq(2)').fadeIn();
		$('#media .tabs li').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});

	$('#media .carousel a').each(function() {
		$(this).attr('rel', '[gallery]');
		var title = $(this).children().attr('title');
		$(this).attr('title', title);
	});
	
	$('#media .carousel a').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	$('#media .carousel a[href*=youtube]').fancybox({
	        'transitionIn'    : 'elastic',
	        'transitionOut'    : 'elastic',
	        'type' : 'swf',
	        'swf' : {'allowfullscreen':'true'},
			'titleShow' : false
	    });
	
	/* Badge Popovers */
	$('.badges li').prepend('<div class="tooltip"></div>');
	$('.badges li').hover(function() {
		$(this).children().stop(true, true).fadeIn();
	}, function() {
		$(this).children().stop(true, true).fadeOut();
		
	});
	
	/* Get Help Tab */
	$("#support-launch").hover(function(){
		$(this).stop(true, true);
    	$(this).animate({ left: '-3px' }, 200);
	},
	function(){
		$(this).animate({ left: '0px' }, 200);
	});
					
});
