
//scripts for runninglog.me
$(function(){
var loading_div = "<div id='loading'>Loading... <img src='/img/ajax-loader.gif' /></div>";
$('.tooltip').hoverIntent(function(){
	var tip = $(this).attr('rel');
	$(this).css({position:'relative'});
	var wrapper = "<div class='tt'>"+tip+"</div>";
	$(this).append(wrapper);
	},function(){
	$('.tt').remove();
});


	$('.fancybox').fancybox({
	'overlayColor': '#ffffff',
	'autoDimensions' : 'false',
	'width':'400'
	});
	
	$('#comment_list li:odd').addClass('odd');
	
	$('.dayTD').hoverIntent(function () {
 	 $('.dayLink',this).stop(true, true).slideDown('fast');
						   
	},function(){ $('.dayLink').slideUp('fast');
						   
	});
	$('.edit_the_run').live('click', function(){
		$('#background').fadeOut();
		$('body').append(loading_div);
		$('#content').remove();
		$('#modal').animate({top: '-1999px'},1000,'easeInBack', function(){
			$(this).remove();
			$('#background').remove();
			
		});

			var id = $(this).attr('href');
			$('#load_the_log').prepend('<div id="content"></div>');
			$('#content').load(id, function(){
			$('#log_a_run').slideDown(1300,'easeOutBack');
			$('#loading').remove();
			});
return false;
});

$('.open_users').click(function(){
	$('#users').toggle();
	return false;
});

$('.user_close').click(function(){
	$('#users').hide();
	return false;
});

$('.details').hoverIntent(function(e){
	var toLoad =$(this).attr('href');
	var html ='<p align="center"><img src="/img/ajax-loader.gif" /></p>';
	
	$('<div class="detailCont">'+html+'</div>').fadeIn()
	.appendTo('body');
  	$('.detailCont').load(toLoad )
	$('.detailCont').css('top', e.pageY + -80).css('left', e.pageX + -410);
	$(html).remove();
} , 
function(){
	//hover out
	$('.detailCont').remove();

	
 });


$('.details').mousemove(function(e) {
		$('.detailCont').css('top', e.pageY + -80).css('left', e.pageX + -410);
	});
	
	
$('a.details').click(function(e) {
		//Cancel the link behavior
		$('body').append(loading_div);
		e.preventDefault();
		//Get the A tag
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		
		var modal = "<div id='modal'><a href='#close' class='modal_close'>Close X</a></div>";
		var id = $(this).attr('href');
		var bg = "<div id='background' style='display:none;'></div>";
		$('body').append(bg);
		$('#background').fadeIn('fast')
		$('#background').css({'width':maskWidth,'height':maskHeight, opacity: '0.6'});
		
		$('body').append(modal);
		//Get the screen height and width
		var html ='<p align="center">Loading...</p>';
		
		$('#modal').prepend('<div id="content">'+html+'</div>');
		
		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		//transition effect
		$('#modal').animate({top: '100px'},1000,'easeOutBack');
		$('#content').load(id);
		$('#loading').remove();
		$(html).remove();
	});
	
	//if close button is clicked
	$('.modal_close').live('click', function() {
		$('#background').fadeOut();
		$('#modal').animate({top: '-1999px'},1000,'easeInBack', function(){
			$(this).remove();
			$('#background').remove();
			
		});
	
	});
	
$('#log-close').hide();


// load more comments 

$('.load').live('click', function() {
var lastmsg = $(this).attr("id");
var userpage = $(this).attr("rel");
if(lastmsg)
{
$(this).html('loading...');
$.ajax({type: "POST",url: "/inc/load_comments.php",data: "last="+ lastmsg+"&userpage="+userpage,success: function(html){
$("ul#comment_list").append(html);
$("#"+lastmsg).remove();

}
});
}
else
{
$("a.load#"+lastmsg).html("You have reached the end");
}
return false;
});







$('.add_the_shoe').live('click', function() {
var shoename = $('.new_shoes').val();

$.ajax({type: "POST",url: "/inc/add_new_shoe.php",data: "shoe_name="+shoename,success: function(html){
$("ul.shoes_list").append(html);
$('.new_shoes').val(' ');
$('.success_message').text("Your shoe was added.");
$('.success_message').delay('2000').fadeOut();

}
});

});


$('.add_the_shoe_alt').live('click', function() {
var shoename = $('.new_shoes').val();

$.ajax({type: "POST",url: "/inc/add_new_shoe_manager.php",data: "shoe_name="+shoename,success: function(html){
$('.new_shoes').val(' ');
$("#shoe_container").append(html);


}
});

});



$('.add_the_tag').live('click', function() {
var tagname = $('.new_tag').val();

$.ajax({type: "POST",url: "/inc/add_tag.php",data: "tag_name="+tagname,success: function(html){
$("#the_tags").append(html);
$('.new_tag').val(' ');
}
});

});

$('.add_tag').live('click', function() {
	$('#add_tag').toggle();
});


//resize the comment textarea

 $('#comments textarea.input').autoResize();


$('.comment_link').live('click',function(){
	var id = "#child_comment-" + $(this).attr('rel');
	$(id).toggle();

});


	
	
$('.dayLink').click(function(){
	$('body').append(loading_div);
	$('#content').remove();
	var id = $(this).attr('href');
	$('#load_the_log').prepend('<div id="content"></div>');
	$('#content').load(id, function(){
	$('#log_a_run').slideDown(1300,'easeOutBack');
	$('#loading').remove();
	});
	
	
	
	return false;

});

$('.close_log_window').click(function(){
	$('#log_a_run').slideUp(300,'easeInBack');
	$('#content').slideUp();
	$('#content').remove();
});
$('.cancel').live('click', function() {
	$('#log_a_run').slideUp(300,'easeInBack');
	$('#content').slideUp();
	$('#content').remove();
	return false;
});

$('.type_trigger').live('click', function() {
	$('#workout_options').slideToggle('fast');
});
$('#workout_options li a').live('click', function() {
	var value = $(this).attr('class');
	$('#workout_options').hide();
	$('input.workout_type').val(value);
	$('.type_trigger').text(value);
});
	
$('.add_shoe').live('click', function() {
	$('#add_shoe').toggle();
});

$('.shoe_trigger').live('click', function() {
	$('.shoes_list').slideToggle('fast');
});
$('.shoes_list li a').live('click', function() {
	var value = $(this).text();
	var val_id = $(this).attr('rel');
	$('.shoes_list').hide();
	$('input.shoes').val(val_id);
	$('.shoe_trigger').text(value);
});


$('.save_run').live('click', function() {

	$('#log_run').submit();
	return false;
});


});
var validateComment = function(){
	if($('textarea[name=comment]').val() !== ''){
		$('.the_comment_form').submit();
	}
};
var validateSubComment = function(id){
	if($('textarea#c-'+id).val() !== ''){
		$('.sub_comment_form-'+id).submit();
	}
};


$('.remove_the_shoe').live('click', function(){
	var del =confirm("Are you sure you want to remove this shoe?"); 
	
	if(del){
var shoeID = $(this).attr('rel');

$.ajax({type: "POST",url: "/inc/remove_shoe.php",data: "shoe_id="+shoeID,success: function(html){

$(".shoe-"+shoeID).remove();

}
});
}
});

//pace stuff
function zeroPad(num,count)
{
var numZeropad = num + '';
while(numZeropad.length < count) {
numZeropad = "0" + numZeropad;
}
return numZeropad;
}
function calcIt()
{
	// Time
	var hr = $('.hour').val();
	var min = $('.min').val();
	var sec=$('.sec').val();
	var totalTime = (hr*60)+(min*1) + (sec/60);
	var unit = $('.unit').val();
	// Distance
	var dist = $('.dist').val();

		if(unit === "miles"){
		dist = (dist*1609);
		}
		else {
		dist = (dist*1000); 
		}// 
		pace = totalTime/dist;
		pace = (pace*1609);
		minP=Math.floor(pace);
		secP=Math.round(60*(pace-minP));
		if (secP === 60)
		{
			// to prevent 60 seconds from showing up in the seconds field
			minP++;
			secP = 0;
		}
		$('span.paceM,span.paceS').remove();
		if ($('.dist').val() ===""||$('.dist').val()==="0"||$('.dist').val()==="00"||$('.dist').val()==="000"){
		
		$('span.paceM,span.paceS').remove();
		$('.pace').append("<span class='paceM'>0:</span>");
		$('.pace').append("<span class='paceS'>00</span>");
		}
		else{
		$('.pace').append("<span class='paceM'>"+minP+":</span>");
		$('.pace').append("<span class='paceS'>"+zeroPad(secP,2)+"</span>");
		}
}

// when converting distance to meters, multiply by conversion constant
function distConversion(dist, unit)
{
	if ( unit === 'miles')
	{
		dist = (dist/1609); 
	}
	
	else if (unit === 'km')
	{
		dist = (dist*1000);
	}
	return dist;
}

$(function(){
	$('a.mile_km_btn').live('click',function() {
	if($(this).text()==="Miles"){
	$('.unit').val("km");
	$(this).text('Km');
	}
	else{
	$('.unit').val("miles");
	$(this).text('Miles');
	}
	calcIt();
    return false;
    
	});
	});


