// JavaScript Document
(function( window, document, $, undefined ){
	
if(typeof window.Philips == "undefined") {
	window.Philips = {};
}
var Philips = window.Philips;
var pexPlayer

/** @namespace */
 
var PEX = Philips.PEX = function (pdata, settings) {  
	pexPlayer = this;
	this._debug = true;
	this._def = {	// default settings of pex player  
		_w:954, // width of pex player div  
		_h:500  // height of pex player div
	}
	 
	 this._settings = $.extend({}, this._def, settings);
	
	 if(typeof pdata == 'undefined'){ 
	 	this._log("No pex data available");
		return;
	 }
	 
	 this._prdData = pdata;
	 this._assets = pdata.Product.Assets.Asset;
	 this._currentAsset = null; 
	 
	 this._displayObj = [{id:'introduction', container:'.pex-alternative'}, {id:'imagezoom', container:'.imageviewer'}, {id:'viewer360', container:'#viewer360'} ]; 
	 
	 this._currentDisplay =  this._displayObj[0].id;
	 
	 $("#pex-navigation").addClass('full-version');
	 
	 this._asset3D = this._getIndex(this._assets, 'P3D', "DocType");
	 
	$('.back-to-product-overview').click(function(){ 
		pexPlayer._showIntroduction();
	}); 
	
	 
	
}

PEX.prototype._init = function() { 
	

}


 


PEX.prototype._onthumbClick = function(assetId){ 
	if(assetId == 0) {
		pexPlayer._showIntroduction(); 	
	}else { 
		pexPlayer._showAsset(assetId); 
	}
}
			
PEX.prototype._showAsset = function(assetId){ 
	 this._currentAsset = this._getIndex(this._assets, assetId, "ServerID"); 
	 if(this._currentAsset) { 
	 	var asset = this._assets[this._currentAsset]
		this._log(asset.DocType);
		 if(asset.DocType != "P3D"){ 
			 if(this.imageZoom){  
				 this._updateStage('imagezoom');		 		
				 this.imageZoom.launch({
						_viewerDiv:$("#zoomContainer"),  
						_fullImage:asset.AssetURL + "?align=0%2C0&hei=1200&$jpglarge$",
						_thumbImage:asset.AssetURL + "?align=0%2C0&hei=384&$jpglarge$"  
					}); 		
			 }
		 }else if(asset.DocType == "P3D") { 
		 	if(this.viewer360){  
				 this._updateStage('viewer360');	 
				 this.viewer360.launch({  
				_imgURL: asset.AssetURL 
				});				
			}
		 }
	 }
}
  
 
PEX.prototype._updateStage = function(showObj) { 
	var tself = this;
	var inTrans = true;	
	var showObjDiv= ''; 
	if(this._currentDisplay != showObj){  
		$(this._displayObj).each(function(index) { 
			 if(this.id == showObj)showObjDiv = this.container; 
		});
		$(this._displayObj).each(function(index) {
			 if(this.id == tself._currentDisplay ){  
			 $(this.container).fadeOut(500, function () { 
					if(inTrans) {  
						if(showObj == 'introduction') { 
							$('.back-to-product-overview').hide(); 
						}else { 
							$('.back-to-product-overview').show(); 
						}

						$(showObjDiv).fadeIn();  
						inTrans = false;
					}
					
					
				});
			 }
		});
		 
		this._currentDisplay = showObj;
	}  
	
}

PEX.prototype._showIntroduction = function() {this._updateStage('introduction'); }

PEX.prototype._getIndex = function(o, v, f) { for(var i=0; i<o.length; i++)if(o[i][f] == v)return i; return false;} 
 
PEX.prototype._log = function(strMsg){ if(this._debug && typeof(console) != 'undefined' )console.log(strMsg);}	

})(window, document, jQuery);
