// JavaScript Document
$(document).ready(function(){
	// BACKSTAGE MENU HACK
	if($("#backstage-header").length) {
		$("#header").addClass("backstage-header-img"); 
		// $(".main-menu").remove();
		$(".backstage-menu").appendTo("#header");
	}
	
	$(".color-list").each(function() {
			$(this).find("li:even").addClass("odd");
	});
	
	// Dropdown button
	$('.dropdown-btn').click(function (e) {
		e.stopPropagation();
		$(this).children('ul').slideToggle("fast");  
	});
	
	// profile view
	$("#twitter-small").tweet({
    username: $("#twitter-small").attr("data-twitter-small"),
    count: 1,
    loading_text: "loading tweets...",
  });
	
	$("#twitter").tweet({
    username: $("#twitter").attr("data-twitter"),
 //   avatar_size: 48,
    count: 3,
    loading_text: "loading tweets..."
  }); 
	
	if ($('#youtube').length) {
		var ytuser = $("#youtube").attr("data-youtube");

		$.getJSON('http://gdata.youtube.com/feeds/api/users/'+ytuser+'/uploads?alt=json&callback=?', function(data) { 
			$.each(data.feed.entry, function(i, item) {
				var updated = item.updated;
				var vidid = item['id']['$t'];
				var title = item['title']['$t'];
				var thumb = item['media$group']['media$thumbnail'][0]['url'];
				//var description = item['media$group']['media$description']['$t'];
				var numViews = item['yt$statistics']['viewCount'];
				vidid = vidid.replace('http://gdata.youtube.com/feeds/api/videos/','');
				$('#youtube').append('<li><a href="http://www.youtube.com/embed/'+vidid+'?wmode=Opaque&autoplay=1" target="youtube" title="'+title+' ('+numViews+'x bekeken)"><img src="'+thumb+'" alt="'+title+'" height="79" width="140"></a></li>');
				
				if(i == 0) {
					$('#ytplayer').attr('src','http://www.youtube.com/embed/'+vidid+'?wmode=Opaque');
				}
				
			});
			$("#youtube a[title]").tooltip({ position: "center left" });
		});		
		
	};
	
	if ($('#vimeo').length) {
	  var vimeouser = $("#vimeo").attr("data-vimeo");

		$.getJSON('http://vimeo.com/api/v2/'+vimeouser+'/videos.json?callback=?', function(data) {
			$.each(data, function(i, item) {
				var id = item.id;
				var title = item.title;
				var url = item.url;
				var thumbnail_medium = item.thumbnail_medium;
				var stats_number_of_plays = item.stats_number_of_plays;	
				$('#vimeo').append('<li><a href="http://player.vimeo.com/video/'+id+'" target="vimeo" title="'+title+' ('+stats_number_of_plays+'x bekeken)"><img src="'+thumbnail_medium+'" alt="'+title+'" height="79" width="140"></a></li>');
				if(i == 0) {
					$('#vimeoplayer').attr('src','http://player.vimeo.com/video/'+id);
				}	
			});
		});

		$("#vimeo a[title]").tooltip({ position: "center left" });	
	};
	
	// profile edit
	$("h3.slide-open").click(function() {
		var obj = $(this);
		$(obj).toggleClass("active").siblings("div.hidden").slideToggle();
		$("h3.slide-open").not(this).siblings("div.hidden").slideUp();
	});
	
	$("input.app").change(function() {
		
	});
	
	$("#f-edit_account").live('submit', function() {
		$('#f-edit_account_submit').attr("disabled","disabled").siblings(".message").addClass("loading").text("Loading..");
		$("#f-edit_account").find("input:enabled").removeClass("negative positive");
		$("#f-edit_account").find(".message").empty();
		$.post("scripts/process.php", $("#f-edit_account").serialize(), function() {

			}, "json")
    .success(function(data) {
			if(data) {
				if(data.error == "1") {
					if(data.error_server == "1") {
						alert(data.error_msg); 
					} else {
						$.each(data.error_obj_array, function(index, value) { 
							$("#f-"+value).addClass("negative").siblings(".message").addClass("negative").text(data.error_msg_array[index]);
							$('#f-edit_account_submit').siblings(".message").empty();
						});
					}
				} else {
					$.jnotify(data.msg);
					$('#f-edit_account_submit').siblings(".message").addClass("positive").text(data.msg);
					$("#f-edit_account").find("input:enabled").val("");
				}
			}
		})
    .error(function() { 
			$('#f-news_edit_button').attr('value','Retry');
			tmpNotification($('#f-message'),"Processing script could not be found.",'negative',30000);
			$.jnotify("Processing script could not be found.", "error"); })
    .complete(function() {
			$('#f-edit_account_submit').removeClass("loading");
			$('#f-edit_account_submit').removeAttr('disabled');
		});
		return false;
	});
	
	$("#f-edit_details").live('submit', function() {
		$('#f-edit_details_submit').attr("disabled","disabled").siblings(".message").addClass("loading").text("Loading..");
		$("#f-edit_details").find("input:enabled").removeClass("negative positive");
		$("#f-edit_details").find(".message").empty().removeClass("negative positive");
		$.post("scripts/process.php", $("#f-genres-selected").sortable("serialize", { key: 'genre[]' }), function() {
			
		}, "json");
		$.post("scripts/process.php", $("#f-edit_details").serialize(), function() {

			}, "json")
    .success(function(data) {
			if(data) {
				if(data.error == "1") {
					if(data.error_server == "1") {
						alert(data.error_msg); 
					} else {
						$.each(data.error_obj_array, function(index, value) { 
							$("#f-"+value).addClass("negative").siblings(".message").addClass("negative").text(data.error_msg_array[index]);
							$('#f-edit_details_submit').siblings(".message").empty();
						});
					}
				} else {
					$.jnotify(data.msg);
					$('#f-edit_details_submit').siblings(".message").addClass("positive").text(data.msg);
				}
			}
		})
    .error(function() { 
			$('#f-edit_details_submit').attr('value','Retry');
			tmpNotification($('#f-message'),"Processing script could not be found.",'negative',30000);
			$.jnotify("Processing script could not be found.", "error"); })
    .complete(function() {
			$('#f-edit_details_submit').removeClass("loading");
			$('#f-edit_details_submit').removeAttr('disabled');
		});
		return false;
	});
	
	$("#f-edit_social").live('submit', function() {
		$("#f-edit_social input.app").siblings("span").addClass("loading").removeClass("small-btn validated not-validated");
		$("#f-edit_social input.app").each(function() {
			var obj = $(this);
			var app = $(obj).attr("data-app");
			var value = $(obj).val();
			
			$(obj).siblings("span").removeClass("loading");
			$.post("scripts/process.php", { action: "edit_social", app: app, value: value }, function(data) {
				if(data.error == "0") $(obj).siblings("span").addClass("small-btn validated");
				else if(data.error == "1") $(obj).siblings("span").addClass("small-btn not-validated");
			}, "json");
		});
		return false;
	});
	
	$("#f-genres-selected").sortable({ axis: 'y', 
		connectWith: '#f-genres',
		receive: function(event, ui) {
			var items = $("#f-genres-selected").sortable("toArray");
			if(items.length > 3) {
				$(ui.sender).sortable('cancel');
			}
		},
		remove: function(event, ui) {
			var items = $("#f-genres-selected").sortable("toArray");
			if(items.length < 1) {
				$(this).sortable('cancel');
			}
		}
	}).disableSelection();
	
	$("#f-genres").sortable({ axis: 'y', 
		connectWith: '#f-genres-selected',
		update: function(event, ui) {
			
		}
	}).disableSelection();
	
	if($("#avatar-uploader").length) {
		createAvatarUploader();
	}
});

$(".colorbox").live("click", function() {
	var url = $(this).attr("href");
	$.colorbox({ href: url+" #content", initialWidth:"50%", initialHeight:"50%"});
	return false;
});

$(".colorbox-image").live("click", function() {
	var url = $(this).attr("href");
	$.colorbox({ href: url, photo:true, initialWidth:"50%", initialHeight:"50%"});
	return false;
});	

$(".colorbox-enroll").live("click", function() {
	var ID = $(this).attr("data-enroll");
	$.post("/scripts/process.php", { action:"enroll_display", id: ID }, function(data) {
		$.colorbox({ html: data.html, initialWidth:"50%", initialHeight:"50%"});
	}, "json");
	return false;
});

$("#f-user_crew_add").live("click", function() {
	$("#f-user_crew tr:last").after("<tr><td><input type=\"text\" name=\"fname[]\" value=\"\" maxlength=\"50\" /></td><td><input type=\"text\" name=\"lname[]\" value=\"\" maxlength=\"50\" /></td><td><input type=\"text\" name=\"postcode[]\" class=\"input-zip\" value=\"\" maxlength=\"7\" /></td><td><input type=\"text\" name=\"role[]\" value=\"\" maxlength=\"50\" /></td><td><span class=\"small-btn btn-delete f-user_crew_delete\">X</span></td></tr>");
	return false;
});

$(".f-user_crew_delete").live("click", function() {
	var row = $(this).parent().parent();
	$(row).fadeOut("slow", function() {
		$(row).remove();
	});
	return false;
});

$("#f-register_backstage").live('submit', function() {
	$(".negative").removeClass("negative");
	$("#f-register_backstage").find("input:enabled").removeClass("negative positive");
	$("#f-register_backstage").find(".message").empty();
	$.post("scripts/process.php", $("#f-register_backstage").serialize()+"&"+$("#f-genres-selected").sortable("serialize", { key: 'genre[]' }), function() {
		
	}, "json")
	.success(function(data) { 
		if(data.error == "1") {
			if(data.error_server == "1") {
				alert(data.error_msg); 
			} else {
				if(data.error_obj_array.length) {
					$.each(data.error_obj_array, function(index, value) { 
						$("#f-"+value).addClass("negative").siblings(".message").addClass("negative").text(data.error_msg_array[index]);
					});
				}					
				tmpNotification($('#f-message'),data.error_msg,'negative',30000);
				$.jnotify(data.error_msg, "error");
			}
		} else {
			window.location.href = "backstage/profiel-activeren/";
		} 
	})
	.error(function() {
		$('#f-register_backstage_submit').attr('value','Retry');
		tmpNotification($('#f-message'),"Processing script could not be found.",'negative',30000);
		$.jnotify("Processing script could not be found.", "error");
	})
	.complete(function() {
		$('#f-message').removeClass("loading");
	});
	return false;
});

$("#f-backstage_activate").live('submit', function() {
	$('#f-message').addClass("loading").text("Loading..");
	$.post("scripts/process.php", $("#f-backstage_activate").serialize(), function(data) {
		
	}, "json")
	.success(function(data) { 
		if(data.error == "1") {
			if(data.error_server == "1") {
				// alert(data.error_msg); 
				$.jnotify(data.error_msg, "error");
				$("#f-backstage_activate_submit").text("Retry");
			} else {
				tmpNotification($('#f-message'),data.error_msg,'negative',30000);
				$.jnotify(data.error_msg, "error");
			}
		} else {
			$.jnotify(data.msg);
			tmpNotification($('#f-message'),data.msg,'positive',3000);
			window.location.href = data.to;
		}
	})
	.error(function() { 
		$('#f-backstage_activate_submit').attr('value','Retry');
		tmpNotification($('#f-message'),"Processing script could not be found.",'negative',30000);
		$.jnotify("Processing script could not be found.", "error");
	})
	.complete(function() { $('#f-message').removeClass("loading"); });
	return false;
});

$("#enroll_user").live('click', function() {
	$('#f-message').removeClass("positive negative").addClass("loading").text("Loading..");
	var enroll = $(this).attr("data-enroll");
	$.post("scripts/process.php", {action: "enroll_user",id: enroll}, function(data) {
		
	}, "json")
	.success(function(data) { 
		if(data.error == "1") {
			if(data.error_server == "1") {
				// alert(data.error_msg); 
				$.jnotify(data.error_msg, "error");
			} else {
				tmpNotification($('#f-message'),data.error_msg,'negative',30000);
				$.jnotify(data.error_msg, "error");
			}
		} else {
			$.jnotify(data.msg);
			$('#f-message').html(data.msg).addClass('positive');
			$("#enroll_user").attr("disabled","disabled");
		}
	})
	.error(function() { 
		tmpNotification($('#f-message'),"Processing script could not be found.",'negative',30000);
		$.jnotify("Processing script could not be found.", "error");
	})
	.complete(function() { $('#f-message').removeClass("loading"); });
	return false;
});

function createAvatarUploader(){            
		var uploader = new qq.FileUploader({
		element: document.getElementById('avatar-uploader'),
		action: 'scripts/process_avatar.php',
		allowedExtensions: ['jpg', 'jpeg'],
		multiple: false,
		debug: true
	});      
}
