$j = jQuery;

$j(function(){

	//Load the player swf
  	var params = { allowScriptAccess: "always"};
	var atts = { };
	
	//Loop thru player divs
	
	$j('.player').each(function(e){
		//give it an id
		this.id = "player" + e;

		//Get the vidpath from the a inside the holder div
		var vidpath = $j(this).find('a').attr("href");
		//imgpath is jpg in same location with same name as vid
		var imgpath =  vidpath.replace(".flv", ".jpg");
		
		var flashvars = { 
			vidpath : vidpath,
			imgpath : imgpath 
		};
		
		var params = {
			"scale" : "noscale",
			"quality" : "high",
			"salign" : "lt"
		}



	  	swfobject.embedSWF(appDir + "/flash/vidplay.swf", 
                     this.id, "522", "323", "8", null, flashvars, params, atts);
		
	});
	
	//Login code
	$j('.login').click(function() {
		//Display login panel by animating up
		$('.adminLogin').animate({top: "-39px"});
		$('.adminLogin .pass').focus();
		
		return false;
	})
	
	//Handle clicking on a locked folder
	$j('.locked a.link').click(function() {
		//Hide other open entry
		$(".files .entry").hide();	
		$(".files .lock a").show();
		
		//Show input for entry
		var lock = $(".lock", $(this).closest(".locked"));
		lock.find("a").hide();
		lock.find(".entry").fadeIn("normal").find(".pass").focus();
		
		return false;
	});
	
	var checkPass = function(target, password) {
		//Get destinaton link
		var link = $(".link", $(target).closest(".locked"))[0];
		var urlPart = link.rel;
		link = link.href;			
		//Check pass is correct
		$.post(
			baseDir + "security/login/",
			{	"password" : password,
				"ajax" : "true",
				"url" : urlPart},
			function(data) {
				if (data.passOk) {
					//Redirect to asked for page - this is secure, because there won't
					//be a session if the user isn't authenticated - they'll still be asked to login
					window.location	= link;
				} else{
					alert('Invalid password, please try again');
				}
			},
			"json"
		)		
	}
	
	var savePass = function(target, password) {
		//Write new password
		//Requires a admin session on server to work
		//Get destinaton link
		var dir = $(target).closest(".directory");
		var link = $(".link", dir)[0];
		var urlPart = link.rel;		
		$.post(
			baseDir + "security/login/",
			{	"password" : password,
				"ajax" : "true",
				"url" : urlPart,
				"savePass" : "true"},
			function(data) {
				if (data.saveOk) {
					dir.find(".entry").hide();
					dir.find(".display").show().find("span").text(password);
					dir.find(".lock a").show();
				} else{
					alert('Error saving password, please logout, login and try again');
				}
			},
			"json"
		)	
	}
	
	//Handle Click on password ok button
	$(".lock .ok").click(function() {
		var mode = "user";
		if ($(".container.admin").length > 0) mode = "admin";
		if (mode == "user")
			checkPass(this, $(this).siblings(".pass")[0].value);
		else
			savePass(this, $(this).siblings(".pass")[0].value);
		return false;
	});
	
	//Handle Enter on the password input
	$(".lock .pass").keypress(function(e) {
		var mode = "user";
		if ($(".container.admin").length > 0) mode = "admin";
		
		if (mode == "user" && e.which == 13) {
			checkPass(e.target, this.value);
			return false;
		}
		
		if (mode == "admin" && e.which == 13) {
			savePass(e.target, this.value);
			return false;
		}			
		
		
		//Escape hide open entry
		if (e.keyCode == 27) {
			//Hide  open entry
			$(".files .entry").hide();	
			$(".files .lock a, .files .display").show();
		}		
				
	})
	
	//Handle admin password edit
	$(".admin .lock a").click(function() {
		//Hide other open entry
		$(".files .entry").hide();	
		$(".files .lock a, .files .display").show();
		var link = $(this);
		link.hide();
		link.siblings(".display").hide();
		link.siblings(".entry").fadeIn("normal").find(".pass").focus();
		
		return false;
	})
	
	
	//Upload via SWFUpload
/*
	var swfUp = new SWFUpload({
		upload_url : "/clients/_download/upload.php",
		flash_url : "/clients/_download/js/swfupload/swfupload.swf",
		button_placeholder_id: "upload",
		button_width: "65",
		button_height: "29",
		button_text: '<span>Uploadr</span>'
	});	
*/
	
});

