
/*global $,log,app,style,svc,ml,confmodell,modeldisplayCtrl,layoutManager */

function CC3ModelView(params) {
	this.init(params);
}

$.extend(CC3ModelView.prototype, {
	init : function(params) {
		this.errorimage = params.errorimage;
		this.loading = "/nwapp/nw/cc3/images/loading.gif";
		this._container =  "#info_header_big-div";
		this._containerBig = "#modellbild_container";
		this._modelImage1 =  "#modelImage1";
		this._modelImageBig1 = "#modelImageBig1";
		this._modelImage2 =  "#modelImage2";
		this._modelImageBig2 = "#modelImageBig2";
		this._loading = false;
	},
	
	setup: function(params){
		var that = this;
		var hideLoading = function(){			
			that.hideLoading();
		};
		
		$(this._container).show();
		$(this._containerBig).show();
		this.showLoading();
		$(this._modelImage1).imageview('create', {src: params.src, errorimage: this.errorimage, ready: hideLoading}).show();
		$(this._modelImageBig1).imageview('create', {errorimage: this.errorimage, ready: hideLoading}).hide();
		$(this._modelImage2).imageview('create', {errorimage: this.errorimage, ready: hideLoading}).hide();
		$(this._modelImageBig2).imageview('create', {errorimage: this.errorimage, ready: hideLoading}).hide();
		
		this.display = 'normal';
		this.layer = 1;
		this._currentImage = this._modelImage1;
	},
	
	setDisplay: function(display, layer){
		if (display === undefined || 
			layer === undefined || 
			this.display === display && this.layer === layer) {
			return;
		}
		
		var displayChange = this.display !== display; 
		
		if (displayChange){
			$(this._currentImage).hide();
		}else{
			$(this._currentImage).fadeOut();
		}
		this.display = display;
		this.layer = layer;
		
		if (display === 'normal' && layer === 1){
			this._currentImage = this._modelImage1;
		}else if (display === 'normal' && layer === 2){
			this._currentImage = this._modelImage2;
		}else if (display === 'big' && layer === 1){
			this._currentImage = this._modelImageBig1;
		}else if (display === 'big' && layer === 2){
			this._currentImage = this._modelImageBig2;
		}
		
		if (displayChange){
			$(this._currentImage).show();	
		}else{
			$(this._currentImage).fadeIn();	
		}
	},

	update: function(params){
		//log("CC3ModelView","update",params);
		
		if(params.display){
			this.setDisplay(params.display, params.layer);
		}
		if (this.display === 'normal') {
			this.showLoading();
			if (this.layer === 1) {
				$(this._modelImage1).imageview('show', params);
			} else {
				$(this._modelImage2).imageview('show', params);
			}
		} 
		else if (this.display === 'big')
		{
			this.showLoadingBig();
			if (this.layer === 1) {
				$(this._modelImageBig1).imageview('show', params);
			} else {
				$(this._modelImageBig2).imageview('show', params);
			}
		}		
	},
	hideLoading: function(){	
		this._loading = false;
		$("#imageLoading, #imageBigLoading").stop(true).hide();
	},	
	showLoading: function(){	
		if(this._loading){
			return;
		}
		this._loading = true;
		$("#imageLoading").delay(400).show(0);
	},
	showLoadingBig: function(){	
		if(this._loading){
			return;
		}
		this._loading = true;
		$("#imageBigLoading").delay(400).show(0);
	}
});


