$(document).ready(function() {
	
	function fixheight() {
		if($('#cnt').height() < $(document).height()) {
			$('#cnt').height($(document).height());
		}
		if($('#sidebar').height() < $('#container').height()) {
			$('#sidebar').height($('#container').height());
		}				
	}
	fixheight();
	$(window).resize(fixheight);

	Cufon.replace('#tabs ul li a, #goal-stats-heading h4', { fontFamily: 'Gotham Medium', hover: true });	
	Cufon.replace('#content h2, #content h3, .weights-column h4, #week-overview h4, #overview-slider h5, #overview-slider .day p.today, #invoices-heading p', { fontFamily: 'Gotham Bold' });	
	Cufon.replace('#footer p, .breadcrumb a, #sub-menu ul li a', { fontFamily: 'Gotham Book' });	
	Cufon.replace('#weights .weights-cnt span.number, #weights .weights-cnt span.measure', { fontFamily: 'Gotham Black' });	
		
	$('#days-cnt').animate({'left': ($('.day').length - 7) * -94 + 'px'}, 300);				

	$('#overview-slider .day').click(function() {
		
		$(this).find('.status').hide();
																						
		if($(this).find('.enter').length == 0){
			$(this)
			.append('<div class="enter"><input type="text" maxlength="5" value="'+$(this).find('.status').text()+'" /></div>')
			.find('input[type="text"]')
			.focus()
			.select()
			.blur(function(e){
				
				weight = $(this).attr('value');
				wdate = $(this).parents('.day').find('input[type="hidden"]').attr('value');
				
				if(weight){
					if($(this).parents('.day').find('.status').length == 0)
						$(this).parents('.day').attr('class','day').append('<p class="status">'+weight+'</p>');
					else
						$(this).parents('.day').find('.status').text(weight).show();
				} else {
					$(this).parents('.day').addClass('empty').find('.status').remove();
				}
				
				$(this).parents('.day').find('.enter').remove();
				
				// AJAX TO SERVER TO UPDATE WEIGHT
				var dataString = 'wdate='+wdate+'&weight='+weight;

				$.ajax({  
				  type: "POST",  
				  url: "/dashboards/updtweight",  
				  data: dataString 
				});  
				
				
			});
		}
		
		if($(this).hasClass('empty')) {
			$('#overview-slider .day').removeClass('selected selected-empty');
			$(this).addClass('selected-empty');
		}	else {
			$('#overview-slider .day').removeClass('selected selected-empty');
			$(this).addClass('selected');
			if($(this).siblings('.day').find('.status').text() == '')
				$(this).addClass('empty');
		}
		
	});
	
	var current = parseInt($('#days-cnt').css('left'));
	if($('#days-cnt').css('left') == "0px") 
		$('#overview-slider-nav .next').css({opacity:'0.2'});

	curr_pos = ($('.day').length - 7) * -94;
	if($('#days-cnt').css('left') ==  curr_pos + "px") 
		$('#overview-slider-nav .prev').css({opacity:'0.2'});


		
	$('#overview-slider-nav a').click(function() {
		if($("#days-cnt:animated").length == 0) {
			var current = parseInt($('#days-cnt').css('left'));
			var nextmove, curr_pos;
			
			if($(this).hasClass('prev')) {
				if($('#days-cnt').css('left') == "0px") {
					$('#overview-slider-nav .prev').css({opacity:'0.2'});
					$('#overview-slider-nav .next').css({opacity:'1.0'});
					return false;
				} else if($('#days-cnt').css('left') == "-94px") {
					$('#overview-slider-nav .prev').css({opacity:'0.2'});
					$('#overview-slider-nav .next').css({opacity:'1.0'});
					nextmove = 0;
				} else if($('#days-cnt').css('left') == "-188px") {
					$('#overview-slider-nav .prev').css({opacity:'1.0'});
					$('#overview-slider-nav .next').css({opacity:'1.0'});
					nextmove = current + 94;
				}	else {
					$('#overview-slider-nav .prev').css({opacity:'1.0'});
					$('#overview-slider-nav .next').css({opacity:'1.0'});
					nextmove = current + 188;
				}
			}
			else if($(this).hasClass('next')) {
				curr_pos = ($('.day').length - 7) * -94;
				
				distance_from_right_side = curr_pos - parseInt($('#days-cnt').css('left'));
								
				if($('#days-cnt').css('left') ==  curr_pos + "px") {
					$('#overview-slider-nav .prev').css({opacity:'1.0'});
					$('#overview-slider-nav .next').css({opacity:'0.2'});
					return false;
				} else if(distance_from_right_side == -94) {
					$('#overview-slider-nav .prev').css({opacity:'1.0'});
					$('#overview-slider-nav .next').css({opacity:'0.2'});
					nextmove = current - 94;
				} else if(distance_from_right_side == -188) {
					$('#overview-slider-nav .prev').css({opacity:'1.0'});
					$('#overview-slider-nav .next').css({opacity:'1.0'});
					nextmove = current - 188;
				}	else {
					$('#overview-slider-nav .prev').css({opacity:'1.0'});
					$('#overview-slider-nav .next').css({opacity:'1.0'});
					nextmove = current - 188;
				} 		
			}		
			$('#days-cnt').animate({'left': nextmove + 'px'}, 300);				
		}		
		return false;
	});
	
	/*
	var current_page = $('#notebook-entry-paging a.page-number').index($('#notebook-entry-paging a.active'));
	
	function change_page () {	
		$('#notebook-entry-paging a.page-number:eq(' + current_page + ')').click();	
	}
	
	$('#notebook-entry-paging a.prev-next-ctrl').click(function() {
		var is_first = current_page == 0;
		var is_last = current_page == $('#notebook-entry-paging a.page-number').length - 1;
		
		var first_and_go_prev =  is_first && $(this).hasClass('prev');
		var last_and_go_next = is_last && $(this).hasClass('next');
		
		if (first_and_go_prev || last_and_go_next) {
			return false;
		} else if($(this).hasClass('prev')) {
			current_page -= 1;
		} else if($(this).hasClass('next')) {
			current_page += 1;
		}						
		change_page();	
		return false;
	});
	
	$('#notebook-entry-paging a.page-number').click(function() {
		var rel = $(this).attr("href");
		var clicked = $(this);			
		$('#notebook-entry-paging a').removeClass('active');
		clicked.addClass('active');			
		
		$('#notebook-entries').
			height($('#notebook-entries').height()).
			html('<br /><br /><br /><img src="css/images/ajax-loader.gif" class="center" alt="" /><br /><br /><br />');
		$('#notebook-entries').load(rel + " #notebook-entries .notebook-entry", function() {$('#notebook-entries').css('height', 'auto')});
		return false;
	});
	

	$('#sub-menu ul li a').click(function() {
		var rel = $(this).attr("href");
		var heading_text = $(this).find('cufontext').html();
		$('#profile').
			height($('#notebook-entries').height()).
			html('<br /><br /><br /><img src="css/images/ajax-loader.gif" class="center" alt="" /><br /><br /><br />');
		window.setTimeout(function(){
			$('#profile').load(rel + "#profile .sub-profile", function() {		
				$('.breadcrumb h2').html(heading_text);
				$('#notebook-entries').css('height', 'auto');
				Cufon.replace('#content h2, #content h3, #invoices-heading p', { fontFamily: 'Gotham Bold' });
			});
			
			
		}, 500);
		$('#sub-menu ul li a').removeClass('active');
		$(this).addClass('active');	
		return false;
	});
	*/
	$('#profile .edit-profile').click(function() {
		$('#profile-data .hide-field').hide();
		$('#profile-data .extra-field').slideDown('normal');
		$('#profile-data p.value').hide();
		$('#profile-data .profile-field').show();
		$(this).hide();
		$('#profile .update-profile').show();
	});
	
	$('#profile .update-profile').click(function() {
		//CHECK PASSWORDS
		var passwd = $('#UserPasswd').val();
		var conf_passwd = $('#UserConfPasswd').val();
		
		if (passwd != conf_passwd) {
			alert("Passwords do not match.");
			return false;
		}
	});
		
	$('.datepicker').datePicker({startDate:'2010-01-01'});
	
	$('.inline-datepicker')
		.datePicker({inline:true,startDate:'2010-01-01'})
		.bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				$('#osx-container .date').val(selectedDate);
			}
		);


	$('#sidebar input[type="text"]').focus(function(e){
				
		if($(this).val() == this.defaultValue){
			$(this).val('').toggleClass("selected");
		}
		
	});
	
	$('#sidebar input[type="text"]').blur(function(e){

		if($(this).val() == ""){
			$(this).val(this.defaultValue).toggleClass("selected");
		}
		
	});
	
	$('.goal-stats-item a.edit').live("click",function(e){
		e.preventDefault();
		
		$cur_date = $(this).parents('.goal-stats-item').find('.goal-completion p').html();
		$cur_wgt = $(this).parents('.goal-stats-item').find('.weight-goal p').html();

		$(this)
		.parents('.goal-stats-item')
		.find('.goal-completion')
		.html('<div class="goal-completion-cnt"><div class="date-field left"><input class="datepicker" type="text" value="'+$cur_date+'"/></div></div>')
		.find('.datepicker')
		.datePicker()
		.parents('.goal-stats-item')
		.find('.weight-goal')
		.html('<div class="weight-field"><input type="text" name="weight" class="weight" maxlength="4" value="'+$cur_wgt+'"/></div>')
		.parents('.goal-stats-item')
		.find('.goal-buttons')
		.html('<a class="primary submit button" href="#">Save Goal</a>');
				
	});

	$('.goal-stats-item a.submit').live("click", function(e){
		e.preventDefault();

		var date = $(this).parents('.goal-stats-item').find('.datepicker').val();
		var weight = $(this).parents('.goal-stats-item').find('.weight').val();
		
		// AJAX TO SERVER TO UPDATE GOAL
		var dataString = 'gdate='+date+'&gweight='+weight;

		$.ajax({  
		  type: "POST",  
		  url: "/dashboards/updtgoal",  
		  data: dataString 
		}); 
		
		// CHANGE ROW BACK TO VIEWING
		$(this)
		.parents('.goal-stats-item')
		.find('.goal-completion')
		.html('<p>'+date+'</p>')
		.parents('.goal-stats-item')
		.find('.weight-goal')
		.html('<p>'+weight+' lbs</p>')
		.parents('.goal-stats-item')
		.find('.goal-buttons')
		.html('<a class="delete" href="#">Delete Goal</a><a class="edit" href="#">Edit Goal</a>');
	});

	$('.goal-stats-item a.delete').live("click", function(e){
		e.preventDefault();
		
		var delete_item = confirm('Are you sure you want to delete this goal?');
		
		if ( delete_item == true ) {
			var goalid = $(this).parents('.goal-stats-item').find('.goal-id').html();
			
			// AJAX TO SERVER TO DELETE GOAL
			var dataString = 'goalid='+goalid;
	
			$.ajax({  
			  type: "POST",  
			  url: "/dashboards/delgoal",  
			  data: dataString 
			}); 		
			
			$(this).parents('.goal-stats-item').fadeOut('slow').remove()
			$('#goal-stats').find('.goal-stats-item:last-child').addClass('last-item');
		}
	});
	
	$('.notebook-entry a.edit').live("click",function(e){
		e.preventDefault();
		currentText = $(this).parents('.notebook-entry').find('.notebook-entry-content p').html();
		$(this)
		.parents('.notebook-entry')
		.find('.notebook-entry-content')
		.html('<textarea name="notebook-edit">'+currentText+'</textarea>')
		.parents('.notebook-entry')
		.find('.goal-buttons')
		.html('<a class="primary submit button right" href="#">Save</a>');
	});

	$('.notebook-entry a.submit').live("click",function(e){
		e.preventDefault();
		newText = $(this).parents('.notebook-entry').find('.notebook-entry-content textarea').val();
		entryid = $(this).parents('.notebook-entry').find('.notebook-entry-id').html();
		$(this)
		.parents('.notebook-entry')
		.find('.notebook-entry-content')
		.html('<p>'+newText+'</p>')
		.parents('.notebook-entry')
		.find('.goal-buttons')
		.html('<a class="delete" href="#">Delete Goal</a><a class="edit" href="#">Edit Goal</a>');
		
		// AJAX TO SERVER TO UPDATE NOTEBOOK ENTRY
		var dataString = 'newText='+newText+'&entryid='+entryid;

		$.ajax({  
		  type: "POST",  
		  url: "/dashboards/updtentry",  
		  data: dataString  
		}); 
	});
	
	$('.notebook-entry a.delete').live("click",function(e){
		e.preventDefault();
		
		var delete_item = confirm('Are you sure you want to delete this notebook entry?');
		
		if ( delete_item == true ) {
			entryid = $(this).parents('.notebook-entry').find('.notebook-entry-id').html();
			// AJAX TO SERVER TO DELETE NOTEBOOK ENTRY
			var dataString = 'entryid='+entryid;
	
			$.ajax({  
			  type: "POST",  
			  url: "/dashboards/delentry",  
			  data: dataString  
			});
			
			$(this)
			.parents('.notebook-entry')
			.slideUp('slow')
			.stop()
			.remove();
		}
	});
		
	// osx
	$(".post-quick-entry").click(function (e) {
		e.preventDefault();	

		$("#osx-modal-content").modal({
			overlayId: 'osx-overlay',
			containerId: 'osx-container',
			closeHTML: '<div class="close"><a href="#" class="simplemodal-close">x</a></div>',
			minHeight:80,
			opacity:65, 
			position:['0',],
			overlayClose:true,
			onOpen:OSX.open,
			onClose:OSX.close
		});
	});
	
});


var OSX = {
	container: null,
	open: function (d) {
		var self = this;
		self.container = d.container[0];
		d.overlay.fadeIn('slow', function () {
			$("#osx-modal-content", self.container).show();
			var title = $("#osx-modal-title", self.container);
			title.show();
			d.container.slideDown('slow', function () {
				setTimeout(function () {
					var h = $("#osx-modal-data", self.container).height()
						+ title.height()
						+ 20; // padding
					d.container.animate(
						{height: h}, 
						200,
						function () {
							$("div.close", self.container).show();
							$("#osx-modal-data", self.container).show();
						}
					);
				}, 300);
			});
		})
	},
	close: function (d) {
		var self = this;
		d.container.animate(
			{top:"-" + (d.container.height() + 20)},
			500,
			function () {
				self.close(); // or $.modal.close();
			}
		);
	}
};
