$(document).ready(function(){
	
	//Hide all help text
	$('.help_text').hide();
	
	//Hide all stat details
	$('.stat_details').hide();
	
	//Init stat clicks
	$('.stats').click(function(e){
		//Prevent the default event
		e.preventDefault();
		
		//Get the id of the current link
		var id = $(this).attr('id').replace(/a_/, '');
		
		//Display the loading gif
		if($('#stat_' + id).is(':hidden'))
		{
			$('#loader_' + id).show();
		}
		
		//Make a call to get the stats
		$.ajax({
			type:'GET',
			url:'http://www.sharesy.com/admin/link/getStats/'+ id,
			success:function(json){ 
					
					//Clear the html of the stats div to avoid duplication of data
					$('#stat_' + id).html('');
					
					var oStats = eval('(' + json + ')');
					var cCount = oStats.stats.clickcount.count;
					var clicks = oStats.stats.clicks;
					var sCount = oStats.stats.sharecount.count;
					var shares = oStats.stats.shares;
					
					
					//Append the stats for clicks
					$('#stat_' + id).append('<a href="" class="stat_topic" id="click_'+ id +'">Click Count - ' + cCount + '</a>');
					
						$('#stat_' + id).append('<div class="stat_list" id="click_detail_'+ id +'"><table id="tblClicks_'+ id +'" class="stat_table"><tr><td class="ip">IP</td><td class="timestamp">Time Stamp</td></tr>');
							
							$.each(clicks, function(i, click){
								$.each($(this), function(i, item){
									$('#tblClicks_' + id).append('<tr><td>' + item.ip + '</td><td>' + formatTimeStamp(item.timestamp) + '</td></tr>');
								});
							});

						
					//Append the stats for shares
					$('#stat_' + id).append('<a href="" class="stat_topic" id="share_'+ id +'">Share Count - ' + sCount + '</a>');
					
						$('#stat_' + id).append('<div class="stat_list" id="share_detail_'+ id +'"><table id="tblShares_'+ id +'" class="stat_table"><tr><td class="sharetype">Type</td><td class="sharemessage">Message</td><td class="timestamp">Time Stamp</td></tr>');
							
							$.each(shares, function(i, share){
								$.each($(this), function(i, item){
									$('#tblShares_' + id).append('<tr><td>' + item.type + '</td><td>' + item.comment + '</td><td>'+ item.timestamp +'</td></tr>');
								});
							});
					
					//hide the loader
					$('#loader_' + id).hide();
					
					//Display the stats box
					$('#stat_' + id).slideToggle(300);
					
				},
			error:function(res){}
		});
		
	});	
	
	//Init all help clicks
	$('.help').click(function(e){
		//Prevent the default action
		e.preventDefault();
		
		//Get the id of the current link
		var id = $(this).attr('id');
		
		//Display the appropriate help box
		$('#'+ id + '_help').slideToggle(300);
	});
	
	//Check the email on signup
	$('#signup_email').blur(function(e){
		
		//Encode the email
		var email = $(this).val();
		
		email = $(this).val().replace(/@/, '--');
		
		//Run an ajax lookup on the email
		$.ajax({
			type:'GET',
			url:'http://www.sharesy.com/signup/emailExists/'+ email,
			success:function(res){ 
				if(res != '')
				{
					$('#signup_email').after('<span class="notice">'+ res +'</span>');
					$('#signup #btnsignup').attr("disabled", "true"); 
				} 
				else
				{
					$('#signup #btnsignup').removeAttr("disabled"); 
					
					if($('#signup .notice').length != 0) 
					{
					    $('#signup .notice').remove();
					}
					
				}
			},
			error:function(res){}
		});
	});
	
	$('#share_form').hide();
	
	$('#viewPane').css('height', $(window).height()).css('width', parseInt($(window).width() - 1) + 'px');
	
	//Init stat list expand clicks
	$('.stat_details .stat_topic').live('click', function(e){
	
		//Prevent the default action
		e.preventDefault();
		
		//Split the id to get the link id and the stat type
		arrID = $(this).attr('id').split('_');
		
		var id = arrID[1];
		var type = arrID[0];
		
		
		//Display the stats box
		$('#'+ type + '_detail_' + id).slideToggle(300);
		
	});
	
	
});
	
	
function formatTimeStamp(data)
{
	var yr = '';
	var mo = '';
	var da = '';
	var hr = '';
	var mn = '';
	var sc = '';
	var ap = '';
	
	yr = data.substr(0,4);
	mo = data.substr(5,2);
	da = data.substr(8,2);
	hr = data.substr(11,2);
	mn = data.substr(14,2);
	sc = data.substr(17,2);
	
	if(hr < 12 || hr == 24)
	{
		ap = 'am';
	}
	else
	{
		ap = 'pm';
	}
	
	switch(hr)
	{
		case '13':
			hr = 1;
		break;
		case '14':
			hr = 2;
		break;
		case '15':
			hr = 3;
		break;
		case '16':
			hr = 4;
		break;
		case '17':
			hr = 5;
		break;
		case '18':
			hr = 6;
		break;
		case '19':
			hr = 7;
		break;
		case '20':
			hr = 8;
		break;
		case '21':
			hr = 9;
		break;
		case '22':
			hr = 10;
		break;
		case '23':
			hr = 11;
		break;
		case '24':
			hr = 12;
		break;
	}
	
	return mo + '/' + da + '/' + yr + ' ' + hr + ':' + mn + ':' + sc + ' ' + ap;
}

