/**
 * phplisting.js
 * This is the user-side javascript, not admin side javascript.
 *
 * It defines a class for handling the interactions with the server for the user.
 */


function PhpListing(config){
	this.listurl = config["listurl"];
	this.userdiv = config["userdiv"];
	this.useriframe = config["useriframe"];
	this.sharedsecret = config["sharedsecret"];
	PhpListing.prototype.listurl = config["listurl"];
	PhpListing.prototype.userdiv = config["userdiv"];
	PhpListing.prototype.useriframe = config["useriframe"];
	PhpListing.prototype.sharedsecret = config["sharedsecret"];
	
}

//does the login for the user
PhpListing.doLogin = function() {
	
	var name = $('#filelistLoginFormUsername').val();
	var password = $('#filelistLoginFormPassword').val();
	
	var datenow = new Date();
	data = {
			'username': name,
			'password': password,
			'time' : datenow.getMilliseconds()
	};
	
	$.getJSON(PhpListing.prototype.listurl + "/phplistcontroller.php/login", data, PhpListing.doLoginFinish);
	
	return false;
}

//handler for callback after login
PhpListing.doLoginFinish = function(data){
	var reData = eval(data);

	$('#' + PhpListing.prototype.userdiv).empty();
	$('#' + PhpListing.prototype.userdiv).html(reData['thehtml']);

	//call for re-initialization to assign the right event handlers if success is fail
	if(reData['success'] == 'true'){
		//$('#filelistList  a ').click(PhpListing.doDownload);
		$('#filelistLogoutDiv  a ').click(PhpListing.doLogout);
		
	}
	return false;
}


//does the logout for the user
PhpListing.doLogout = function() {
	var datenow = new Date();
	data = {
		'sharedsecret': PhpListing.prototype.sharedsecret,
		'time' : datenow.getMilliseconds()
	};
	
	$.getJSON(PhpListing.prototype.listurl + "/phplistcontroller.php/logout", data, PhpListing.doLoginFinish);
	
	return false;
}

//this does the download for the user init 
PhpListing.doDownload = function(event) { 

	var filename = "";

	if("" != this.text && null != this.text) {
		filename = this.text;
	} else {
		filename = event.srcElement.innerText;
	}

	if("" == filename || null == filename) {
		return false;
	}
	
	var iframe = $('#' + PhpListing.prototype.useriframe)[0];
	
	iframe.src = PhpListing.prototype.listurl + "/phplistcontroller.php/getfile?filename=" + filename + "&sharedsecret=" + PhpListing.prototype.sharedsecret;
	
	return false;
}

//initializes the userdiv
PhpListing.doInit = function() {
	
	$.getJSON(PhpListing.prototype.listurl + "/phplistcontroller.php", PhpListing.doInitFinish);
	
	return false;
}


PhpListing.doInitFinish = function(data) {
	var reData = eval(data);
	
	if(reData['success'] == 'false'){
		alert(reData['message']);
	} else {
		
		$('#' + PhpListing.prototype.userdiv).html(reData['thehtml']);
		
	}
	//we could get the login form or the file list
	$('#filelistLoginForm').bind('submit', PhpListing.doLogin);
//	$('#filelistList  a ').click(PhpListing.doDownload);
	$('#filelistLogoutDiv  a ').click(PhpListing.doLogout);
	
	return false;
}


//re-initializes the div for the user
PhpListing.doReinit = function() {
	
	PhpListing.doInit();
	return false;
}

