
/*
	Edelweiss Media Player  v1
	dju@isnow.fr
*/

function showOverlay(f) {
	$("#overlay").fadeIn('fast', f);
}
function hideOverlay(f) {
	$("#overlay").fadeOut('fast', f);
}


function showPlayer(f) {
	$("#player").fadeIn('fast', f);
}
function hidePlayer(f) {
	$("#player").fadeOut('fast', f);
}


function EdelweissPlayer(edwConnect) {
	
	this.edwConnect = edwConnect; 
		
	this.title = null;
	this.stream = null;
	this.currentImage = null;
	this.currentMediaIdx = -1;
	
	// callbacks when user has reached the beginning or the end of stream, if there is more to be displayed
	// this orders EdelweissConnect to fetchs a new mediaStream (different page)
	//this.eosCallback = null;
	//this.bosCallBack = null;
	// not used yet
	

	
	this.setStream = function(stream) {
		this.stream = stream;
	};
	
	
	this.setConfig = function(config) {
		this.config = config;
	};
	
	
	this.showMedia = function(idx) {
		var media = this.stream.list[idx];
		
		$("#player > #playerTitle").text(media.title);
		
		$("#playerMediaPreviewLoader").show();
		
		$("#playerMediaPreviewImage").load(function() {				
			$("#playerMediaPreviewLoader").hide();
			
			if(!$("#playerMediaPreviewImage").hasClass("whiteborder"))
				$("#playerMediaPreviewImage").addClass("whiteborder");
		});
		

		var src = edwConnect.getMediaURL(media, "std");
		$("#playerMediaPreviewImage").attr({'src':src});

		this.currentMediaIdx = idx;
		
		if(this.canGoBack()) {
			var t = this.stream.list[idx-1];
			$("#prevMedia img.thumb").attr({'src':edwConnect.getMediaURL(t, "sq")});
			if($("#prevMedia").is(':hidden'))
				$("#prevMedia").fadeIn('fast');
		} else
			$("#prevMedia").fadeOut('fast');
		
		if(this.canGoNext()) {
			var t = this.stream.list[idx+1];
			$("#nextMedia img.thumb").attr({'src':edwConnect.getMediaURL(t, "sq")});
			if($("#nextMedia").is(':hidden'))
				$("#nextMedia").fadeIn('fast');
		} else
			$("#nextMedia").fadeOut('fast');
		
		
		this.edwConnect.media_getInfo(media.mid, function(data) {
			$("#playerExtraInfo").empty();
			if(data.date_taken)
				$("<li>").text(data.date_taken).appendTo("#playerExtraInfo");
		
			if(data.model && data.make)
				$("<li>").text(data.model).appendTo("#playerExtraInfo");
		});

		
	};
		
	this.canGoNext = function() {
		return this.currentMediaIdx <= (this.stream.list.length -2);
	};
	
	this.canGoBack = function() {
		return this.currentMediaIdx > 0;
	};
	
	this.showNextMedia = function() {
		if(!this.canGoNext())
			return false;
		
		this.showMedia(this.currentMediaIdx+1);
		return true;
	};
	
	this.showPrevMedia = function() {
		if(!this.canGoBack())
			return false;

		this.showMedia(this.currentMediaIdx-1);
		return true;

	};
	
	
}

function EdelweissConfig(endpoint, storage) {
	this.endpoint = endpoint;
	this.storage = storage;
}


/*
 * API for interacting with Edelweiss Logic 
 * Uses JQuery 1.3
 */
function EdelweissConnect(config, sid) {
	this.config = config;
	this.sid = sid;
	
	// should be q, perpage, page
	this.media_search = function(q, f) {		
		this.jsonCall("?m=edw.media.search&q="+q, f); 
	};
	
	
	
	this.album_getAlbum = function(aid, f) {
		this.jsonCall("?m=edw.album.getAlbum&aid="+aid, f); 
	};
	
	
	/*
	 * returns an object with mediaStream, albumStream
	 */
	this.album_getMedias = function(aid, f) {
		this.jsonCall("?m=edw.album.getMedias&aid="+aid,f);		
	};
	
	
	this.media_getInfo = function(mid, f) {
		this.jsonCall("?m=edw.media.getInfo&mid="+mid, f);
	};
	
	
	this.jsonCall = function(queryString, f) {
		var p = this;
		
		$.getJSON(config.endpoint+queryString+"&sid="+this.sid+"&callback=?", function(data) {
			
			if(!p.checkResponse(data)) {
				alert("erreur !");
				return false;
			}

			if(f)
				return f(data);
			
		 }); 
	};

	
	this.media_updateTitle = function(mid, title, f) {
		
	};
	
	this.media_updateDescription = function(mid, description, f) {
		
	};
	
	

	
	
	
	this.media_addComment = function(mid, comment, f) {
		
	};
	this.media_removeComment = function(mid, cid, f) {
		
	};
	this.media_addTags = function(mid, tags, f) {
		
	};
	this.media_removeTags = function(mid, tags, f) {
		
	};
	
	
	this.checkResponse = function(data) {
		return !data.error;
	};
	
	this.getMediaURL = function(media, identifier) {
		return this.config.storage+"/edw"+media.mid+"_"+media.secret+"_"+identifier+".jpg";
	};
	
}


 