function submitXhrLogin() {
	$("#xhrlogin").submit(function() {
		 
		var username     = $('#username').attr('value');
		var password     = $('#password').attr('value');
		var responseId   = $('#responseId').attr('value');
		var onsuccess    = $('#onsuccess').attr('value');
		var scriptcontroller = $('#scriptcontroller').attr('value');
		var productId   = $('#productId').attr('value');
		 
		$.ajax({
			type: "POST",
			url: "/auth/identify/onsuccess/" + onsuccess + "/scriptcontroller/" +  scriptcontroller + "/product_id/" +  productId,
			data: "username="+ username + "& password=" + password,
			success: function(data){
				$('#addVidResponse').html(data);
				addvideo(); // rebind video form event handler
				submitXhrLogin(); // rebind login form event handler
				updateLoginLinks();
				$("#vidFormInfo").fadeOut("fast");
				return false;
			}
		});		
		return false;
	});	
	
}

function updateLoginLinks() {
	$.ajax({
		type: "POST",
		url: "/video/updatelogin",
		data: "",
		success: function(data){
			$('#shop-access div.main li.login').html(data);
		}
	});
}

function getFlashPlayer(videoId) {
	var updateTarget = "#listing-" +videoId;
	$.ajax({
		type: "GET",
		url: "/video/getplayer/video_id/" + videoId,
		data: "",
		success: function(data){
		    $(updateTarget).fadeOut("slow");
			$(updateTarget).html(data);
			$(updateTarget).fadeIn("slow");
		}
	});
	$("#listing-feedback-" + videoId + ":hidden:first").fadeIn("fast");
	$("#info-"+videoId).attr("style","width:35%");
	return false;
}

function rateVideo(videoId, vote, responseTarget) {
	var url = "/video/rate/vote/" + vote + "/video_id/" + videoId;
	$.ajax({
		type: "POST",
		url: url,
		data: "",
		success: function(data){		    
			$(responseTarget).html(data);
			$(responseTarget).fadeIn("slow");
		}
	});	
}

function flagVideo(videoId,  responseTarget) {
	var url = "/video/flag/video_id/" + videoId;
	$.ajax({
		type: "POST",
		url: url,
		data: "",
		success: function(data){		    
			$(responseTarget).html(data);
			$(responseTarget).fadeIn("slow");
		}
	});	
}

function addvideo() {
	$("#xhrAddVid").submit(function() {
		 
		var videoUrl    = $('#videoUrl').attr('value');
		var productId   = $('#productId').attr('value');
		var responseId   = $('#responseId').attr('value');
		 
		$.ajax({
			type: "POST",
			url: "/video/add/product_id/" + productId,
			data: "videoUrl="+ videoUrl,
			success: function(data){
				$('#addVidResponse').html(data);				
				refreshVidList(productId);
				addvideo();
				return false;
			}
		});		
		return false;
	});	
}

function showVidForm() {
	$(document.body).click(function () {
	      $("#addVidResponse:hidden:first").fadeIn("fast");
	    });
}

function refreshVidList(productId, page) {
	if( page == '') {
	 var page = 1;	
	}
	if(productId == '') {
		productId = "all";
	}
	$.ajax({
		type: "GET",
		url: "/video/list/product_id/" + productId + "/vidpage/" + page,
		data: "",
		success: function(data){
			$('#vidList').html(data);
		}
	});
}

function refreshModerationList(page) {
	if( page == '') {
		 var page = 1;	
		}
		$.ajax({
			type: "GET",
			url: "/video/moderate/vidpage/" + page,
			data: "",
			success: function(data){
				$('#vidList').html(data);
			}
		});
	}

function moderateVideo(videoId, action) {
	var url = "/video/moderate/modaction/" + action + "/video_id/" + videoId;
	$.ajax({
		type: "POST",
		url: url,
		data: "",
		success: function(data){		    
			$("#modresposne").html(data);
			$("#modresposne").fadeIn("slow");
		}
	});	
}

submitXhrLogin();
addvideo();
//moderateVideo(); 

