$(document).ready(function() {
	
	// Initialize tooltips on tools
	$('ul.shareTools2 li').tipsy({gravity:'n'});
		
	//initialize labelify
	$('.labelify').labelify({ labelledClass: "labelinside" });
	
	// Initialize date picker for event date
	$('.datepicker').datepicker({minDate: '+0m +0w'});

	// Initialize all sharebox instances so that the 'text' share mode is active
	shareModeStatus();

	//sharebox validation setup:
	
	//add custom method for youtube videos (to be used down below for video validation)
	jQuery.validator.addMethod("youtube", function(value, element) {
			return this.optional(element) || /^http:\/\/www\.youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)[&\w;=\+_\-]*/.test(value); 
		}, 
		"Please enter a valid YouTube video URL."
	);
	

	//Status:
	$("#formShareStatus").validate({
	   	rules: {
	   		body: {
	   			required: true,
	   			maxlength: 140
  			}
		}
	});
	
	//Blog:
	$("#formShareText").validate({
	   	rules: {
	   		title: {
	   			required: true
  			},
  			body: {
	   			required: true
  			}
		}
	});
	
	//Link:	
    $("#formShareLink").validate({
	   	rules: {
	   		link: {
	   			required: true,
	   			url: true
  			}
		}
	});
	
	/*
	//does not work --> .val() returns undefined for input type 'file'.
	$('#formShareImage').submit(function() {
		var filePath = $(this).children('#filePath').val();
		var filePattern = /([^\s]+(\.(jpg|png|gif))$)/;
		if(!filePattern.test(filePath)){
			alert("There was an error submitting your post. Make sure you have selected an image file of type .jpg, .gif, or .png");
			return false;
		}
	});*/
	
	//Youtube:
	$("#formShareVideo").validate({
	   	rules: {
	   		link: {
	   			required: true,
	   			youtube: true
  			}
		}
	});

});

function shareModeStatus() {
	$('div.share').hide();
	$('div.shareStatus').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareStatus').addClass('selected').blur();
}
function shareModeText() {
	$('div.share').hide();
	$('div.shareText').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareText').addClass('selected').blur();
}
function shareModeLink() {
	$('div.share').hide();
	$('div.shareLink').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareLink').addClass('selected').blur();
}
function shareModeImage() {
	$('div.share').hide();
	$('div.shareImage').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareImage').addClass('selected').blur();
}
function shareModeVideo() {
	$('div.share').hide();
	$('div.shareVideo').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareVideo').addClass('selected').blur();
}
function shareModeEvent() {
	$('div.share').hide();
	$('div.shareEvent').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareEvent').addClass('selected').blur();
}
function shareModeClassified() {
	$('div.share').hide();
	$('div.shareClassified').show();
	$('ul.shareTools2 li').removeClass('selected');
	$('ul.shareTools2 li#shareClassified').addClass('selected').blur();
}

function getIdentity($obj) {
	var $form = $obj.parent().parent().parent();
	return $form
			.children('div.postSettings')
			  .children('div.identity')
			    .children('select[name=asAdmin]').val();
}

function ajaxPostStatus($obj) {
	
	var $form    = $obj.parent().parent().parent();
	var $body    = $form.children('div.formInputs').children('div.fieldInput').children('textarea[name=body]');
	var asAdmin  = getIdentity($obj);
	
	if($form.valid()){
		$.post('/me/ajaxPostMiniBlogEntry',
				{'type'    : 'status',
			     'body'    : $body.val(),
			     'asAdmin' : asAdmin
				},
				function(data,textState) {
					if($('#miniBlogEntriesContainer').children('div.entryAndToolContainer:visible')>0){
						$('#miniBlogEntriesContainer').prepend(data);
					}else{
						$('#firstPost').prepend(data);
					}
					$body.val('');
					showHideNoActivity();
				});
	}else{
		alert("There was a problem submitting your post. Make sure you have entered something in the text box.");
	}
}

function ajaxPostText($obj) {
	
	var $form    = $obj.parent().parent().parent();
	var $title   = $form.children('div.formInputs').children('div.fieldInput').children('input[name=title]');
	var $body    = $form.children('div.formInputs').children('div.fieldInput').children('textarea[name=body]');
	var asAdmin  = getIdentity($obj);

	if($form.valid()){
		$.post('/me/ajaxPostMiniBlogEntry',
				{'type'    : 'text',
				 'title'   : $title.val(),
			     'body'    : $body.val(),
			     'asAdmin' : asAdmin
				},
				function(data,textState) {
					if($('#miniBlogEntriesContainer').children('div.entryAndToolContainer:visible')>0){
						$('#miniBlogEntriesContainer').prepend(data);
					}else{
						$('#firstPost').prepend(data);
					}
					$title.val('');
					$body.val('');
					showHideNoActivity();
				});
	}else{
		alert("There was a problem submitting your post. Make sure you have provided a title and the text of your post.");
	}
}

function ajaxPostLink($obj) {
	
	var $form    = $obj.parent().parent().parent();
	var asAdmin  = getIdentity($obj);
	var $body    = $form.children('div.formInputs').children('div.fieldInput').children('textarea[name=body]');
	var $link    = $form.children('div.formInputs').children('div.fieldInput').children('input[name=link]');

	if($form.valid()){
		$.post('/me/ajaxPostMiniBlogEntry',
				{'type'    : 'link',
			     'link'    : $link.val(),
				 'body'    : $body.val(),
			     'asAdmin' : asAdmin
				},
				function(data,textState) {
					if($('#miniBlogEntriesContainer').children('div.entryAndToolContainer:visible')>0){
						$('#miniBlogEntriesContainer').prepend(data);
					}else{
						$('#firstPost').prepend(data);
					}
					$link.val('http://');
					$body.val('');
					showHideNoActivity();
				});
	}else{
		alert("There was a problem submitting your post. Make sure you have entered a valid URL.");
	}
}



function ajaxPostVideo($obj) {
	
	var $form    = $obj.parent().parent().parent();
	var asAdmin  = getIdentity($obj);
	var $body    = $form.children('div.formInputs').children('div.fieldInput').children('textarea[name=body]');
	var $link    = $form.children('div.formInputs').children('div.fieldInput').children('input[name=link]');

	if($form.valid()){
		$.post('/me/ajaxPostMiniBlogEntry',
			{'type'    : 'video',
		     'link'    : $link.val(),
			 'body'    : $body.val(),
		     'asAdmin' : asAdmin
			},
			function(data,textState) {
				if($('#miniBlogEntriesContainer').children('div.entryAndToolContainer:visible')>0){
					$('#miniBlogEntriesContainer').prepend(data);
				}else{
					$('#firstPost').prepend(data);
				}
				$link.val('http://');
				$body.val('');
				showHideNoActivity();
			});
	}else{
		alert("There was a problem submitting your post. Make sure you have entered a valid YouTube URL.");
	}
}

function pickClassifiedLocation(obj){
	if(obj.value==1){
		$('div#classifiedLocationHere').show();
		$('div#classifiedLocationAway').hide();
	}else if(obj.value==0){
		$('div#classifiedLocationHere').hide();
		$('div#classifiedLocationAway').show();
	}
}

function pickLocation(obj){
	if(obj.value==1){
		$('div#locationHere').show();
		$('div#locationAway').hide();
	}else if(obj.value==0){
		$('div#locationHere').hide();
		$('div#locationAway').show();
	}
}

function pickRecurrence(obj) {
	if (obj.value == 1) {
		$('div#eventRecurrence').show();
	} else if (obj.value == 0) {
		$('div#eventRecurrence').hide();
	}
}
