/********************************
plugin to center objects on the screen
********************************/
(function($){
	$.fn.center = function() {
		var win = $(window);
		this.css('position', 'absolute');
		this.css('top', ((win.height()-this.outerHeight())/2)+win.scrollTop() + 'px');
		this.css('left', ((win.width()-this.outerWidth())/2)+win.scrollLeft() + 'px');
		return this;
	};
})(jQuery);

$(document).ready(function() {
	
	/********************************
	auto resize textareas
	********************************/
	$('textarea').autoResize();
	
	
	/********************************
	delay loading of images
	********************************/
	$('img[delaysrc]').each(function() {
		var src = $(this).attr('delaysrc');
		$(this).attr('src', src).removeAttr('delaysrc');
	});
	
	
	/********************************
	textbox placeholders 
	********************************/
	if (!Modernizr.input.placeholder){
		$('input[placeholder]').placeholder();
    }
	
	
	/********************************
	textbox number types 
	********************************/
	if (!Modernizr.inputtypes.number){
		$('input[type=number]').numeric();
    }
	
	
	/********************************
	modal windows
	********************************/
	$('a[rel=modal]').live('click', function(e) {
		e.preventDefault();
		var modal = $($(this).attr('href'));
		var overlay = $('<div id="overlay" />');
		overlay.appendTo('body').fadeIn();
		modal.center().prepend('<a href="#" class="x close">Close</a>').fadeIn();
		modal.find(':input:first').not('button').focus();
	});
	$('.close', '.modal').live('click', function(e) {
		e.preventDefault();
		$('.modal').fadeOut();
		$('#overlay').fadeOut(function() {
			$(this).remove();
		});
	});
	
	
	/********************************
	Delete Photo Confirms
	********************************/
	$('a[rel=removephoto]').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var li = $this.parents('li');
		var url = $this.attr('href');
		var img = $this.prev().find('img').attr('src');
		var html = [
			'<div id="removephoto" class="confirm">',
				'<h3>Are you sure you want to remove this photo?</h3>',
				'<div id="removephoto-img"><img src="'+ img +'" /></div>',
				'<div class="actions">',
					'<button>Yes, I\'m sure</button>',
					'or <a href="#" class="close">Cancel</a>',
				'</div>',
			'</div>'
		].join('\n');
		
		var overlay = $('<div id="overlay" />').appendTo('body').fadeIn();
		var modal = $(html).appendTo('body').center().fadeIn();
		modal.find(':input:first').not('button').focus();
		
		// ok
		$('button', modal).click(function() {
			$.get(url, function(resp) {
				modal.fadeOut(function() {
					$(this).remove();
				});
				overlay.fadeOut(function() {
					$(this).remove();
					li.fadeOut(function() {
						$(this).remove();
					});
				});
			});
		});
		
		// cancel
		$('.close', modal).click(function(e) {
			e.preventDefault();
			modal.fadeOut(function() {
				$(this).remove();
			});
			overlay.fadeOut(function() {
				$(this).remove();
			});
		});
	});
	
	
	/********************************
	Remove Friend Confirms
	********************************/
	$('a[rel=removefriend]').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var li = $this.parents('li');
		var url = $this.attr('href');
		var name = li.find('.fullname').text();
		var image = li.find('img').attr('src');
		var loc = li.find('.location').text();
		var html = [
			'<div id="removefriend" class="confirm">',
				'<h3>Are you sure you want remove this friend?</h3>',
				'<div id="removefriend-img">',
					'<img src="'+ image  +'" alt="Photo of '+ name +'" />',
					'<h4>'+ name +'</h4>',
					'<p>'+ loc +'</p>',
				'</div>',
				'<div class="actions">',
					'<button>Yes, I\'m sure</button>',
					'or <a href="#" class="close">Cancel</a>',
				'</div>',
			'</div>'
		].join('\n');
		
		var overlay = $('<div id="overlay" />').appendTo('body').fadeIn();
		var modal = $(html).appendTo('body').center().fadeIn();
		modal.find(':input:first').not('button').focus();
		
		// ok
		$('button', modal).click(function() {
			$.get(url, function(resp) {
				modal.fadeOut(function() {
					$(this).remove();
				});
				overlay.fadeOut(function() {
					$(this).remove();
					li.fadeOut(function() {
						$(this).remove();
					});
				});
			});
		});
		
		// cancel
		$('.close', '#removefriend').click(function(e) {
			e.preventDefault();
			modal.fadeOut(function() {
				$(this).remove();
			});
			overlay.fadeOut(function() {
				$(this).remove();
			});
		});
	});

	
	/********************************
	Remove Score Confirms
	********************************/
	$('a[rel=removescore]').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var row = $this.parents('tr');
		var url = $this.attr('href');
		var sibs = $this.parent().siblings();
		var course = $.trim(sibs.eq(0).text());
		var date = $.trim(sibs.eq(1).text());
		var holes = $.trim(sibs.eq(2).text());
		var strokes = $.trim(sibs.eq(3).text());
		var score = $.trim(sibs.eq(4).text());	
		var html = [
			'<div id="removescore" class="confirm">',
				'<h3>Are you sure you want remove this score?</h3>',
				'<div id="removescore-details">',
					'<h4>'+ course +'</h4>',
					'<p>Played on '+ date +'</p>',
					'<p>Holes: '+ holes +'</p>',
					'<p>Strokes: '+ strokes +'</p>',
					'<p>Score: '+ score +'</p>',
				'</div>',
				'<div class="actions">',
					'<button>Yes, I\'m sure</button>',
					'or <a href="#" class="close">Cancel</a>',
				'</div>',
			'</div>'
		].join('\n');
		
		var overlay = $('<div id="overlay" />').appendTo('body').fadeIn();
		var modal = $(html).appendTo('body').center().fadeIn();
		modal.find(':input:first').not('button').focus();
		
		// ok
		$('button', modal).click(function() {
			$.get(url, function(resp) {
				modal.fadeOut(function() {
					$(this).remove();
				});
				overlay.fadeOut(function() {
					$(this).remove();
					row.fadeOut(function() {
						$(this).remove();
					});
				});
			});
		});
		
		// cancel
		$('.close', modal).click(function(e) {
			e.preventDefault();
			modal.fadeOut(function() {
				$(this).remove();
			});
			overlay.fadeOut(function() {
				$(this).remove();
			});
		});
	});
	

	/********************************
	add/remove faves
	********************************/
	$('.addfave').live('click', function(e) {
		e.preventDefault();
		$(this).addClass('removefave').removeClass('addfave').find('span').text('Remove Fave');
	});
	$('.removefave').live('click', function(e) {
		e.preventDefault();
		$(this).addClass('addfave').removeClass('removefave').find('span').text('Add to Faves');
	});
	
	
	/********************************
	Modals
	
	$('a[rel=modal]').modal();
	$('.uploadphotos').modal();
	$('.deletephoto').modal();
	$('.reportphoto').modal();
	$('.makeprofilepic').modal();
	$('.removescore').modal();
	********************************/
	
	
	
	

	
	
	


	/*
	$('#signintrigger').click(function(e) {
		e.preventDefault();

		var off = $(this).offset();
		var top = off.top + 60;
		var left = off.left -60;
		
		$('#signin').css({
			top: top,
			left: left
		}).show();		
	});
	*/

	
	

	$('#myinbox a').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		
		var link = $(this);
		var iwin = $('#inbox-window');
		if (iwin.is(':visible')) {
			link.removeClass('active');
			iwin.hide();
		} else {
			var off = link.offset();
			var hgt = link.outerHeight();
			var wth = link.outerWidth();
			var iwh = iwin.outerWidth();
			link.addClass('active');
			iwin.css({
				top: off.top + hgt,
				left: off.left - iwh + wth
			}).show();
		}
	});
	$('#inbox-window').click(function(e) {
		e.stopPropagation();
	});
	$(document).click(function(e) {
		$('#inbox-window').hide();
		$('#myinbox a').removeClass('active');
	});
	
	
	
	
	/*
	$('.addfave').live('click', function(e) {
		var pos = $(this).position();
		
		// get the image url
		$(this).addClass('removefave').removeClass('addfave');
		var bgurl = $(this).css('background-image').replace(/'/g,'').replace(/"/g,'').replace(/url\(|\)$/ig, '');
		var image = $('<img src="'+ bgurl +'" />');

		// sweet puff effect like on Flickr
		image.css({
			left: pos.left,
			top: pos.top,
			height: 32,
			width: 32,
			position: 'absolute'
		}).appendTo($(this).parent()).animate({
			left: pos.left -20,
			top: pos.top -16,
			height: 64,
			width: 64,
			opacity: 0
		}, {
			duration: 750,
			complete: function() {
				$(this).remove();
			}
		});

		$.get($(this).attr('href'));
		e.preventDefault();
	});
	*/
	

	
	
	
	
	/********************************
	Validate Comment Forms
	********************************/
	$('#newcomment').validate({
		rules: {
			comment: 'required'
		},
		messages: {
			comment: 'Please enter your comment'
		},
		submitHandler: function(form) {
			var form = $(form);
			var url = form.attr('action');
			var type = form.attr('method');
			
			// block the form
			form.block({
				message: 'Posting Comment ...',
				overlayCSS: {
					backgroundColor: '#fff',
					opacity: .5
				},
				css: {
					backgroundColor: '#fff',
					border: '1px solid #333',
					padding: '10px'
				} 
			});
			
			// submit the form
			$.ajax({
				url: url,
				type: type,
				data: form.serialize(),
				success: function(json) {
					var img = form.data('img');
					var name = form.data('name');
					var url = form.data('url');
					var html = [
						'<li>',
							'<a href="'+ url +'"><img src="'+ img +'" /></a>',
							'<div class="detail">',
								'<h4><a href="'+ url +'">'+ name +'</a></h4>',
								'<p>REPLACE THIS TEXT WITH AJAX RESPONSE</p>',
								'<p>March 18, 2011 at 2:45pm</p>',
							'</div>',
						'</li>'
					].join('');
					form.prev().append(html);
					form.unblock(); // unblock
					form.find('textarea').val('');
				}
			});
		}
	});
	
	
	
	

	$('#sendmessage form').validate({
		rules: {
			subject: 'required',
			message: 'required'
		},
		messages: {
			firstname: 'Please enter a subject',
			lastname: 'Please enter a message'
		}
	});

});





/********************************
Upload Photos
********************************/
$(document).ready(function() {
	$('#fileupload').uploadify({
		uploader: '/assets/flash/uploadify.swf',
		cancelImg: '/assets/images/remove2.png',
		buttonImg: '/assets/images/pixel.gif',
		script: '/uploadify.php',
		folder: '/uploads',
		auto: true,
		multi: true,
		width: 105,
		height: 37,
		fileExt: '*.jpg;*.gif;*.png',
		fileDesc: 'Image Files',
		buttonText: 'Select Photos',
		wmode: 'transparent',
		queueID: 'uploadphotos-queue',
		removeCompleted: false,
		onSelectOnce: function(e, data) {
			$('#uploadphotos-tip').fadeOut();
		},
		onCancel: function(e, id, file, data) {
			var que = $('#uploadphotos-queue');
			if (que.children().size()-1 == 0) {
				$('#uploadphotos-tip').fadeIn();
			}
		},
	});
});



/********************************
	confirm windows
	*******************************
	$('a[rel=confirm]').live('click', function(e) {
		e.preventDefault();
		var url = $(this).attr('href'), modal;
		var overlay = $('<div id="overlay" />');
		$.get(url, function(html) {
			modal = $(html).appendTo('body');
			overlay.appendTo('body').fadeIn();
			modal.center().fadeIn();
			modal.find(':input:first').not('button').focus();
		});	
	});
	$('.close', '.confirm').live('click', function(e) {
		e.preventDefault();
		$('.confirm').fadeOut();
		$('#overlay').fadeOut(function() {
			$(this).remove();
		});
	});
	*/


