var Log = new Object();
try{
	Log = console;
}catch(e){
	Log.debug = function(){
	};
}

var PISAClient = function(target){
	//private

	var _width;
	var _height;

	var target = target;
	var host = "";
	var style = "";
	var color = "";
	var filename = "";
	var quality = 0;
	var background = "";
	var width = 0;
	var height = 0;
	var cwidth = 0;
	var cheight = 0;
	var icons = "";

	var zoomArr = undefined;
	var zoomLib = new Object();
	var zoomIndex = 0;

	var panorama = undefined;
	var zoomDropDown = undefined;

	var timeOutID = -1;
	//public
	return {
		getURL:function(){
			var url;

			url = host + "?";

			if (style != "") {
				url += "sty="+style;
				if (color != "") {
					url += "&col="+color;
				}
			} else if (filename != "") {
				url += "fn="+filename;
			}

			// add parameters
			if (background != "") {
				url += "&bg="+background;
			}
			if (width != "") {
				url += "&w="+width;
			}
			if (height != "") {
				url += "&h="+height;
			}
			if (quality != "") {
				url += "&q="+quality;
			}
			return url;
		},
		setHost:function(h){
			host = h;
		},
		setStyle:function(s){
			style = s;
			filename = ""
		},
		setColor:function(c){
			color = c;
			filename = ""
		},
		setFilename:function(f){
			filename = f;
			style = ""
			color = ""
		},
		setQuality:function(c){
			quality = q;
		},
		setBackground:function(b){
			background = b;
		},
		setSize:function(w,h){
			width = w;
			height = h;
		},
		setCanvasSize:function(w,h){
			cwidth = w;
			cheight = h;
		},
		setIcons:function(i){
			icons = i;
		},
		setZoomArray:function(z){
			zoomArr = z;
			for(var i=0;i<z.length;i++){
				zoomLib["z"+z[i]] = i;
			}
			zoomIndex = zoomLib["z100"];
		},
		getZoom:function(index){
			return zoomArr[index];
		},
		getIndex:function(percentage){
			Log.debug("getIndex:"+percentage);
			Log.debug("getIndexOutput:"+zoomLib["z"+percentage]);
			return zoomLib["z"+percentage];
		},
		setZoom:function(percentage){
			zoomIndex = zoomLib["z"+percentage];
		},
		getZoomIndex:function(delta){
			if(delta<0){
				zoomIndex--;
			}else if(delta>0){
				zoomIndex++;
			}
			if(zoomIndex<0){
				zoomIndex = 0;
			}else if(zoomIndex>=zoomArr.length){
				zoomIndex = zoomArr.length - 1;
			}
			return zoomIndex;
		},
		getStyle:function(){
			return style;
		},
		getColor:function(){
			return color;
		},
		getFilename:function(){
			return filename;
		},
		getQuality:function(){
			return quality;
		},
		getWidth:function(){
			return width;
		},
		getHeight:function(){
			return height;
		},
		getIcons:function(){
			return icons;
		},
		getOriginalWidth:function(){
			return _width;
		},
		getOriginalHeight:function(){
			return _height;
		},
		getPanorama:function(){
			return panorama;
		},
		
		doZoom:function(index){
		var ua = navigator.userAgent.toLowerCase();
		if(ua==undefined && !(ua.indexOf("Safari") >=0)){
			Log.debug("doZoom:"+index);
		}
			panorama.doZoom(zoomDropDown, index, zoomArr[index]);
		},
		setZoomDropDown:function(el){
			zoomDropDown = el;
		},
		render:function(){
			var params = { 
				Controller:this,
				ImageURL: this.getURL(), 
				ImageWidth: width, 
				ImageHeight: height, 
				CanvasWidth: cwidth,
				CanvasHeight: cheight,
				IconDirectory: this.getIcons(),
				RenderIn : target,
				ShowNavigator : false
			}
			_width = width;
			_height = height;

			panorama = new experience.panorama.Viewer(params);
			panorama.positionImage();
		},
		wait:function(func){
			clearTimeout(timeOutID);
			timeOutID = setTimeout(func,500);
		}
	}
};