/**
 * /project/classes/js/viewmedia.js
 * 
 * Media weergeven: foto's, PDF's, Excel-documenten, ...
 * @author Peter Fastré <peter@lunatis.be>
 * @author Maarten Hendrikx <maarten@level27.be>
 */

function ViewMedia(_id, _mediaId) {

	// standard properties
	this.id = _id;	
	this.mediaId = _mediaId;
	
	this.frameworkElementId = "F_" + this.id;
	this.frameworkElement = document.getElementById(this.frameworkElementId);
	this.imgElementId = "I_" + this.id;
	this.imgElement = document.getElementById(this.imgElementId);
	
	// appearance
	this.width = 0;
	this.height = 0;
	this.visible = true;
	
	// events
	this.onclick = "";
	this.needsCreate = false;
	this.isImage = false;
	
	// extra properties
	this.dataSource = "";
	this.alt = "";
	this.title = "";
	this.path = "";
	this.tag = "";
	this.url = "";
	
	// voor bij meerdere apps op de pagina
	this.appName = "";
	this.appType = "";
	
	// methods
	this.show = ViewMedia_show;
	this.hide = ViewMedia_hide;
	
	this.registerEvents = ViewMedia_registerEvents;
	this.addEvent = ViewMedia_addEvent;
	this.removeEvent = ViewMedia_removeEvent;
	this.getEventName = ViewMedia_getEventName;
	
	this.render = ViewMedia_render;
	this.destroy = ViewMedia_destroy;
	
	this.getType = ViewMedia_getType;
	this.dataBind = ViewMedia_dataBind;
	
	this.showPicture = ViewMedia_showPicture;
	
	this.init = ViewMedia_init;
	
}


function ViewMedia_init() {
	if (this.needsCreate) {
		// enkel als de status W is, gaan we via xmlrequest onszelf proberen op te bouwen
		if (this.dataSource != "") {
			this.dataParams = new Array();
			this.dataParams["MediaID"] = this.mediaId; // onze id is M32456, de MediaID = 32456
			this.dataParams["Size"] = this.size;
			this.dataBind();
		}
	}
}

function ViewMedia_showPicture(newPath) {
	this.path = newPath;
	
	// in elk geval moeten we het thumbnail path zetten
	if (this.imgElement) {
		this.imgElement.src = this.path;
	}
	
	/*
	if (doOnclick == 1 && this.url == "" && isImage == 1) {
		//alert("adding onclick 1");
		// als de url leeg is, plaatsen we op het img element een link naar een popup window
		// in die popupwindow hebben we een viewmedia element, met size = big
		//this.imgElement.onclick = "alert('hello image');";
		this.addEvent("click");
	}
	else if (doOnclick == 1 && this.url == "" && isImage == 0) {
		//alert("adding onclick 2");
		// als 	de url leeg is, en het gaat om een bestand,
		// dan openen we een popupwindow naar het bestand zelf
		//this.imgElement.onclick = "alert('hello file');";
		this.addEvent("click");
	}
	else {
		this.removeEvent("click");
	}
	
	*/
	
	
		
}

function ViewMedia_getType() {
	return "ViewMedia";
}

function ViewMedia_destroy() {
	
}

function ViewMedia_render() {
	
}




function ViewMedia_dataBind() {
	debug.write("Databind for " + this.id);
	
	if (this.dataSource != "") {
		debug.write("Starting xml request for " + this.id, "ViewMedia_dataBind");
		oXmlRequest = new XmlRequest();
		if (this.appName != "") {
			oXmlRequest.appName = this.appName;
			oXmlRequest.appType = this.appType;
		}
		oXmlRequest.functionName	= this.dataSource;
		oXmlRequest.params 			= this.dataParams;	
		oXmlRequest.callingObject   = this.id;
		oXmlRequest.onsuccess 		= ViewMedia_handleXmlResponse;
		oXmlRequest.start();
	}
	var pic = this;
	
	
	var onDataBind = this.ondatabind;
	
	function ViewMedia_handleXmlResponse(oXmlResponse) {
		if (oXmlResponse.statusCode != "2000") {
			if (pic) {
				pic.showPicture(layoutroot + "/img/picture_notfound.gif", 0, 0);
			}
		}
		else {
			if (pic) {
				drViewMedia = oXmlResponse.data[0];
				if (drViewMedia["ShowPath"] != "") {
					//alert("needscreate  van " + pic.id + " op false zetten");
					pic.needsCreate = false;					
					pic.showPicture(drViewMedia["ShowPath"]);
				}
			}
			// We verbergen de 'throbber' onder medium-afbeelding
			document.getElementById("LoadingPicture").style.visibility = "hidden";
				
		}
		
	}
	
}

function ViewMedia_hide() {
	if (this.frameworkElement) {
		this.visible = false;
		this.frameworkElement.style.display = "none";
	}
}

function ViewMedia_show() {
	if (this.frameworkElement) {
		this.visible = true;
		this.frameworkElement.style.display = "block";
	}
}


function ViewMedia_getEventName(whichevent) {
	//alert("eval dit " + eval("this." + whichevent));
	return (eval("this.on" + whichevent));
	
}

function ViewMedia_registerEvents() {
	if (this.frameworkElement) {
		if (this.onclick.length > 0) {
			this.addEvent('click');
		}
		
		
	}
	
}
	
function ViewMedia_addEvent(eventtype) {
	
	//W3C
	if(this.frameworkElement.addEventListener) {
		//alert('W3C event added');
		this.frameworkElement.addEventListener(eventtype, handle_event, false);
	}

	//Microsoft
	else if(this.frameworkElement.attachEvent){
		//alert("MS event added");
		this.frameworkElement.attachEvent("on" + eventtype, handle_event);
	}
}

	
function ViewMedia_removeEvent(eventtype) {
	
	//W3C
	if(this.frameworkElement.removeEventListener) {
		//alert('W3C event removed');
		this.frameworkElement.removeEventListener(eventtype, handle_event, false);
	}

	//Microsoft
	else if(this.frameworkElement.detachEvent){
		//alert("MS event removed");
		this.frameworkElement.detachEvent("on" + eventtype, handle_event);
	}
}






