$(function() {
	//ajax example
	//$.post('?test', {email:'email',user:'user'}, function(response) {
	//	alert(response);
	//});
	
	//ajax example
	//$.get('?get_featured_rant', function(response) {
		//alert(response);
	//});
	
	//each example
	//$("#frm_manage_users input[@type=checkbox]").each(function(){
		//alert($(this).val());
	//});
	
	$('.compose_message').click(function(){
		receiver_id = $(this).attr("id").substr(16);	
	});
	
	$("#send_message").click(SendMessage);
	
	$("#toggle_report").click(ToggleReport);
	
	$("#send_video_report").click(SendVideoReport);
	
	$("#add_comment").click(AddComment);
	
	$("#add_user_comment").click(AddUserComment);
	
	$("#home_request").click(HomeRequest);
	
	$(".favorites").click(Favorites);
	
	$(".add_friend").click(AddFriend);
	
	$(".remove_friend").click(RemoveFriend);
	
	$(".remove_favorite").click(RemoveFavorite);
	
	$(".accept_friend").click(AcceptFriend);
	
	$(".decline_friend").click(DeclineFriend);

	$("#select_messages").change(SelectMessages);
	
	$('a.delete_message').click(DeleteMessage);
	//delete message from details page
	$('a.del_message').click(DelMessage);
	
	//mass delete messages
	$('#mass_delete_messages').click(MassDeleteMessages);	
	
	$('a.mark_unread').click(MarkUnread);
	
	$("#recommend").click(Recommend);
	
	$('#frm_vault').submit(SearchVault);
	$('#frm_quick').submit(QuickSubmit);
	
	$('#add_part').click(AddPart);
	$('#clear_parts').click(ClearParts);
	
	$('.view_trailer').click(function(){
		last_trailer = $(this).attr("id").substr(13);							   
	});
	
	$('.recommend').click(function(){
		last_recommend = $(this).attr("id").substr(10);							   
	});
	
	if($("embed").length > 0){
		$("embed").attr("width", 540);
		$("embed").attr("height", 468);

		$("embed").css({
			'width': '540px',
			'height': '468px'
		});	
	}
	
	if($("object").length > 0){
		$("object").attr("width", 540);
		$("object").attr("height", 468);

		$("object").css({
			'width': '540px',
			'height': '468px'
		});	
	}
	
	if($("param[name=width]").length > 0){
		$("param[name=width]").attr("value", 540);
	}
	
	if($("param[name=height]").length > 0){
		$("param[name=height]").attr("value", 468);
	}
	
	if($("iframe").length > 0){
		$("iframe").attr("width", 540);
		$("iframe").attr("height", 468);

		$("iframe").css({
			'width': '540px',
			'height': '468px'
		});	
	}
	
	if($("#birthday").length > 0){
		// Datepicker
		$('#birthday').datepicker({
			dateFormat: 'yy-mm-dd',
			changeYear: true,
			yearRange: '1950:2000'
		});
	}
	
	if($("#release").length > 0){
		// Datepicker
		$('#release').datepicker({
			dateFormat: 'yy-mm-dd'	
		});
	}
	
	//toggle episodes field for "add title" and "edit title"
	$('select[name=type]').change(function(){
		if($(this).val() == 'tv show')
			$("#episodes_active").css({display: ''});
		else
			$("#episodes_active").css({display: 'none'});
	});
})

var last_recommend = 0;
var last_trailer = 0;

function AddPart(){
	html_code = '<tr class="extra_part">' +
                	'<td colspan="2"><strong>Embed Code:</strong></td>' +
                '</tr>' +
          		'<tr class="extra_part">' +
            		'<td colspan="2"><textarea class="field1" name="parts[]" style="width: 500px; height: 150px;"></textarea></td>' +
          		'</tr>' +
		   		'<tr class="extra_part">' +
            		'<td colspan="2"><img src="'+ HTTP + LAYOUTS +'images/spacer.gif" alt="" height="10" /></td>' +
          		'</tr>';	
		  
	$("#parts_links").before(html_code);
	$('#clear_parts').click(ClearParts);
}

function ClearParts(){
	$(".extra_part").remove();	
}

function QuickSubmit(){
	$("#quick_submit_error").attr("class", "");
	$("#quick_submit_error").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Processing...');
	
	var url = $("#frm_quick input[name=url]").val();
	
	$.post(HTTP + 'ajax/?quick_vault_submit', {url:url}, function(response) {
		update = response.split("|||");
		if(update[0] == 0){
			$("#quick_submit_error").attr("class", "rederror");
			$("#quick_submit_error").text(update[1]);	
		}
		else{
			$("#frm_quick input[name=url]").val("");
						
			$("#quick_submit_error").attr("class", "greenerror");
			$("#quick_submit_error").text("Video successfully added! If it's approved, you will see it on your VideoVault Page");		
		}
	});	
	
	return false;
}

function SearchVault(){
	var keyword = $("#frm_vault input[name=keyword]").val();
	if(keyword == null || keyword == ''){
		alert("Incomplete field!");
		return false;
	}
	
	document.location = HTTP + 'videovault/' + url_encode(keyword) + '/';
	
	return false;
}

function Recommend(){
	$("#modal_error").attr("class", "");
	$("#modal_error").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
			
	var title_id = $("#frm_recommend input[name=title_id]").val();
	var to = $("#frm_recommend input[name=to]").val();
	var subject = $("#frm_recommend input[name=subject]").val();
	var message = $("#frm_recommend textarea[name=message]").val();
	
	if(to == '' || subject == '' || message == ''){
		$("#modal_error").attr("class", "rederror");
		$("#modal_error").text("Incomplete fields!");
		return false;
	}

	$.post(HTTP + 'ajax/?recommend', {title_id:title_id,to:to,subject:subject,message:message}, function(response) {
		update = response.split("|||");
		if(update[0] == 0){
			$("#modal_error").attr("class", "rederror");
			$("#modal_error").text(update[1]);	
		}
		else{
			$("#frm_recommend input[name=to]").val("");
						
			$("#modal_error").attr("class", "greenerror");
			$("#modal_error").text("Message sent!");		
		}
	});	
}

function MarkUnread(){
	message_id = $(this).attr("id").substr(12);
	
	link_text = $(this).text();
	
	if(link_text.indexOf("Unread") != -1)
		var read_type = 1;
	else
		var read_type = 2;
	
	if(!confirm("Are you sure you want to mark this message as unread?"))
		return false;
	
	$.post(HTTP + 'ajax/?mark_unread', {id:message_id}, function(response) {
		if(response == 1){
			if(read_type == 1)
				$(".mark_unread").text('Mark as Read');
			else
				$(".mark_unread").text('Mark as Unread');
		}
		
		//reload trigger
		$('a.mark_unread').click(MarkUnread);	
	});	
}

function DeleteMessage(){
	message_id = $(this).attr("id").substr(15);
	
	if(!confirm("Are you sure you want to delete this message?"))
		return false;
	
	$.post(HTTP + 'ajax/?delete_message', {id:message_id}, function(response) {
		if(response == 1)
			$("#message_container_" + message_id).remove();		
				
		//reload trigger
		$('a.delete_message').click(DeleteMessage);	
	});	
}

function DelMessage(){
	message_id = $(this).attr("id").substr(12);
	
	if(!confirm("Are you sure you want to delete this message?"))
		return false;
	
	$.post(HTTP + 'ajax/?del_message', {id:message_id}, function(response) {
		update = response.split("|||");
		if(update[0] == 1)
			document.location = update[1];
				
	});	
}

function MassDeleteMessages(){
	var message_ids = new Array();
	$("#selectMessages input[@type=checkbox]").each(function(){
		if($(this).attr("checked") == true)
			message_ids.push($(this).val());
	});
	
	message_ids = message_ids.join(",");
	
	if(message_ids == '' || message_ids == null){
		alert("Please select at least one message!");
		return false;
	}
	
	if(!confirm("Are you sure you want to delete these messages?"))
		return false;
		
	$.post(HTTP + 'ajax/?mass_delete_messages', {ids:message_ids}, function(response) {
		//response will return the ids of the users that have been deleted; ids will be used to remove html elements from page;
		update = response.split("|||");
		if(update[0] == 1 && update[1] != '' && update[1] != null){
			message_ids = update[1].split(",");
			for(i = 0; i < message_ids.length; i++)
				$("#message_container_" + message_ids[i]).remove();		
		}
	});
	
}

function SelectMessages(){
	option_value = $(this).val();
	option_value = parseInt(option_value);
	if(option_value == 0)
		return false;
	
	$("#selectMessages input[@type=checkbox]").attr("checked", false);
	
	// 1 - all; 2- none; 3 - read; 4 - unread
	$("#selectMessages input[@type=checkbox]").each(function(){
		switch(option_value){
			case 1:
			  $(this).attr("checked", true);
			  break;
			case 2:
			  $(this).attr("checked", false);
			  break;
			case 3:
			  checkbox_class = $(this).attr("class");
			  if(checkbox_class.length > 0)
			  	 $(this).attr("checked", true);
			  break;
			case 4:
			   checkbox_class = $(this).attr("class");
			   if(checkbox_class.length == 0)
			   	 $(this).attr("checked", true);
			  break;  
			default:
		  	  $(this).attr("checked", true);
			  break;
		}
	});
}

var receiver_id = 0;

function SendMessage(){
	$("#modal_error").text("");
	
	var to = $("#frm_compose input[name=to]").val();
	var subject = $("#frm_compose input[name=subject]").val();
	var message = $("#frm_compose textarea[name=message]").val();
	
	if(to == '' || subject == '' || message == ''){
		$("#modal_error").attr("class", "rederror");
		$("#modal_error").text("Incomplete fields!");
		return false;
	}

	$.post(HTTP + 'ajax/?send_message', {to:to,subject:subject,message:message}, function(response) {
		update = response.split("|||");
		if(update[0] == 0){
			$("#modal_error").attr("class", "rederror");
			$("#modal_error").text(update[1]);	
		}
		else{
			$("#frm_compose input[name=subject]").val("");
			$("#frm_compose textarea[name=message]").val("");
			
			$("#modal_error").attr("class", "greenerror");
			$("#modal_error").text("Message sent!");		
		}
	});

}

function RemoveFavorite(){
	var favorite_id = $(this).attr("id").substr(9);
	
	if(!confirm("Are you sure you want to this title from your favorites?"))
		return false;
	
	$.post(HTTP + 'ajax/?remove_favorite', {id:favorite_id}, function(response) {
		if(response == 1)
			$("#movie-item-" + favorite_id).remove();
		$(".remove_favorite").click(RemoveFavorite);
	});
}

function Favorites(){
	var title_id = $(this).attr("id").substr(9);
	
	$.post(HTTP + 'ajax/?change_favorite', {title_id:title_id}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			if(update[1] == 0) //favorite removed
				$("#container-favorite-" + title_id).html('<img src="' + HTTP + LAYOUTS + 'images/favorite_out.gif" alt="Favorite" /> <a href="javascript: void(0);" class="favorites" id="favorite_' + title_id + '">Add to favorites</a>');	
			else
				$("#container-favorite-" + title_id).html('<img src="' + HTTP + LAYOUTS + 'images/favorite.gif" alt="Favorite" /> <a href="javascript: void(0);" class="favorites" id="favorite_' + title_id + '">Remove favorite</a>');	
			
		}
		$(".favorites").click(Favorites);
	});
}

function DeclineFriend(){
	var friend_id = $(this).attr("id").substr(15);
	
	if(!confirm("Are you sure you want to decline this friend request?"))
		return false;
	
	$.post(HTTP + 'ajax/?decline_friend', {id:friend_id}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			$("#friend-item-" + friend_id).remove();			
		}
		$(".decline_friend").click(DeclineFriend);
	});
}

function AcceptFriend(){
	var friend_id = $(this).attr("id").substr(14);
	
	if(!confirm("Are you sure you want to accept this friend request?"))
		return false;
	
	$.post(HTTP + 'ajax/?add_friend', {id:friend_id}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			$("#friend-item-" + friend_id).remove();			
		}
		$(".accept_friend").click(AcceptFriend);
	});
}

function AddFriend(){
	var friend_id = $(this).attr("id").substr(11);
	
	if($(this).attr("class").indexOf("blue2") != -1)
		var link_class = 'blue2 add_friend';
	else
		var link_class = 'add_friend';
	
	container = $("#container-friend-" + friend_id).html();
	
	if(container.indexOf('add.png') != -1)
		question = 'Are you sure you want to add this user as friend?';
	else
		question = 'Are you sure you want to remove this user from your list of friends?';
	
	if(!confirm(question))
		return false;
	
	$.post(HTTP + 'ajax/?add_friend', {id:friend_id}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			if(update[1] == 0) //favorite removed
				$("#container-friend-" + friend_id).html('<img src="' + HTTP + LAYOUTS + 'images/remove.png" alt="Friend" /> <a href="javascript: void(0);" class="' + link_class + '" id="add_friend_' + friend_id + '">Remove Friend</a>');	
			else
				$("#container-friend-" + friend_id).html('<img src="' + HTTP + LAYOUTS + 'images/add.png" alt="Friend" /> <a href="javascript: void(0);" class="' + link_class + '" id="add_friend_' + friend_id + '">Add To Friends</a>');	
			
		}
		$(".add_friend").click(AddFriend);
	});
}

function RemoveFriend(){
	var friend_id = $(this).attr("id").substr(14);
	
	var link_class = 'blue2 remove_friend';
	
	if(!confirm("Are you sure you want to remove this friend from your list?"))
		return false;
	
	$.post(HTTP + 'ajax/?add_friend', {id:friend_id}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			$("#friend-item-" + friend_id).remove();
		}
		$(".remove_friend").click(RemoveFriend);
	});
}

function AddComment(){
	var description = $("textarea[name=comment]").val();
	var title_id = $("input[name=title_id]").val();
	
	if(description == null || description == '' || title_id == null || title_id == ''){
		alert('Incomplete field!');
		return false;
	}
	
	$.post(HTTP + 'ajax/?add_comment', {title_id:title_id,description:description}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			if($("#no_comments").length > 0)
				$("#no_comments").remove();
			
			$('#report_error').attr("class", "greenerror");	
			$('#report_error').text('Comment has been posted!');	
			$("#comments").append(update[1]);
			$("textarea[name=comment]").val('');
		}
		else{
			$('#report_error').text(update[1]);	
		}
	});
}

function AddUserComment(){
	var description = $("textarea[name=comment]").val();
	var profile_id = $("input[name=profile_id]").val();
	
	if(description == null || description == '' || profile_id == null || profile_id == ''){
		alert('Incomplete field!');
		return false;
	}
	
	$.post(HTTP + 'ajax/?add_user_comment', {profile_id:profile_id,description:description}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			if($("#no_comments").length > 0)
				$("#no_comments").remove();
			
			$('#report_error').attr("class", "greenerror");	
			$('#report_error').text('Comment has been posted!');	
			$("#comments").append(update[1]);
			$("textarea[name=comment]").val('');
		}
		else{
			$('#report_error').text(update[1]);	
		}
	});
}

function ToggleReport(){
	$('#report_video').toggle(500);	
}

function SendVideoReport(){
	var reason = $("textarea[name=reason]").val();
	var rec_challenge = $("input[name=recaptcha_challenge_field]").val();
	var rec_response = $("input[name=recaptcha_response_field]").val();
	
	if(reason == null || reason == '' || rec_challenge == null || rec_challenge == '' || rec_response == null || rec_response == ''){
		alert('Incomplete field!');
		return false;
	}
	
	var strHref = window.location.href;
	
	$.post(HTTP + 'ajax/?send_video_report', {reason:reason,recaptcha_challenge_field:rec_challenge,recaptcha_response_field:rec_response,href:strHref}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			$('#report_error').attr("class", "greenerror");	
			$('#report_error').text('Report sent!');	
			$('#report_video').toggle(1000);	
			$("textarea[name=reason]").val();
		}
		else{
			$('#report_error').text(update[1]);	
			Recaptcha.reload();
		}
	});
}

function HomeRequest(){
	var name_text = $("input[name=name]").val();
	var url = $("input[name=url]").val();
	var code = $("input[name=code]").val();	
	
	if(name_text == null || name_text == '' || code == null || code == ''){
		alert("Incomplete fields!");
		return false;
	}
	
	$.post(HTTP + 'ajax/?home_request', {name:name_text,url:url,code:code}, function(response) {
		var update = response.split("|||");
		if(update[0] == 1){
			$('#request_error').attr("class", "greenerror");	
			$('#request_error').text('Request sent!');	
		}
		else{
			$('#request_error').attr("class", "rederror");	
			$('#request_error').text(update[1]);	
		}
	});
}

function url_encode(string)
{
	var string;
	var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
	var hex = "0123456789ABCDEF";
	var encoded_string = "";
	for(var i = 0; i < string.length; i++){
		var character = string.charAt(i);
		if(character == " "){
			encoded_string += "+";
		}else if(safechars.indexOf(character) != -1){
			encoded_string += character;
		}else{
			var hexchar = character.charCodeAt(0);
			if(hexchar > 255){
				encoded_string += "+";
			}else{
				encoded_string += "%";
				encoded_string += hex.charAt((hexchar >> 4) & 0xF);
				encoded_string += hex.charAt(hexchar & 0xF);
			}
		}
	}
	return encoded_string;
}

