vpClient.addMethods({

	initBrowser : function(){
		waiter();
		this.initPageHeader();
		this.initContent();
		this.initDataSheet();
		this.initImages();
		unWaiter();
		$('listContainerNavigation').hide();
		this.currProjectNode = null;
	},
	
	initPageHeader : function(){
		// nothing for now
		$('navigator').style.display = "block";
		var func = (function(){this.view = 'list';this.initView();}).bind(this)
		$('imgBack').observe( 'click',  func);
	},
	
	initContent : function(){
		this.cleanBrowser();
		// set title
		var title  = this.getProductProperty(this.currProjectNode, 'name');
		$('projectTitle').update(title);
		
		// set metadata at title:
		var factsObj = this.getChildObjectByName(this.currProjectNode, 'facts');
		
		var director = this.getProductProperty(factsObj[0], 'Director');
 		if (director) $('projectDirector').update('Director: ' + director);

		var releaseDate = this.getProductProperty(factsObj[0], 'Year');
 		if (releaseDate) $('projectReleaseDate').update(releaseDate);
 		
 		var cat = this.getProductProperty(this.currProjectNode, 'category');
 		if (cat) $('projectCategory').update(" | " + cat);

 		var runTime = this.getProductProperty(factsObj[0], 'Running time');
 		if (runTime) $('projectLength').update(" | " + runTime);
 		
 		var story = this.getProductProperty(this.currProjectNode, 'story');
 		if (story) $('projectStory').update(story);

 		var story = this.getProductProperty(this.currProjectNode, 'synopsis');
 		if (story) $('synopsis').update(story);

	},
	
	
	initDataSheet : function(){
		Element.update($('projectDatasheet'), ' ');
		
		var html = '';
		var specs = this.getChildObjectByName(this.currProjectNode, 'facts');
		specs = specs[0];
		var htmlAfter = '';
		$A(specs.childNodes).each(
			(function(propertyNode){
				if (propertyNode.nodeName == 'property'){
					var propName = wps.dom.getElementValue(propertyNode.getElementsByTagName('key').item(0));
					if (propName == 'name' ) return;
					var propValue = wps.dom.getElementValue(propertyNode.getElementsByTagName('value').item(0));
					if (propName == "Website"){
						this.addWebsiteLink(propValue);
					}
					if (propValue) {
						//html += '<div class="dataSheetRow"><span class="title">' + propName + '</span><span class="value">' + propValue+ '</span></div>';
						html += '<li class="dataSheetRow"><span class="title">' + propName + '</span><ul><li class="value">' + propValue+ '</li></ul></li>';
					}
				}
			}).bind(this)
		);
		html = '<ul class="datasheetlist">' + html + '</ul>';
		$('projectDatasheet').update(html);
	},
	
	addWebsiteLink : function(URI){
		if (!URI) return;
		if (URI.substr(0,7) != 'http://') URI = 'http://' + URI;
		var img = Builder.node('img', {src:'images/pijl_www_' + gDisplayColorName + ".jpg", alt:"Click to go to project's website", style:'display:block;', id:'imgWWW'});
		var firstChild = wps.dom.filteredFirstNode($('navigator'));
		$('navigator').insertBefore(img, firstChild);
		Element.observe(img, 'click', function(){window.open(URI)});
	},

	initImages : function(){
		Element.update($('projectImages'), '');
		var html = '';
		var images =  this.getChildObjectByName(this.currProjectNode, 'media');
		var cachedImg = '';
		var html = '';
		images =  images[0];
		$A(images.childNodes).each(
			(function(propertyNode){
				if (propertyNode.nodeName == 'property'){
					var propName = wps.dom.getElementValue(propertyNode.getElementsByTagName('key').item(0));
					if (propName == 'name' ) return;
					var propID = wps.dom.getElementValue(propertyNode.getElementsByTagName('property_id').item(0));
					var filename = wps.dom.getElementValue(propertyNode.getElementsByTagName('fileName').item(0));
					if (filename){
						var extension = filename.substr( (filename.length - 3), filename.length);
						if (extension == 'flv'){
//							html = '<img style="display:none;" src="&amp;action=objectWeb&amp;rpc=1&amp;subAction=getCachedBin&amp;propID='+ propID+ '">';
							var img = Builder.node('img', {width:'190px', src:'/upload/objectweb/' + propID + '.flv1.jpg', className:'imgItem', border:'0'});
							img.filename = filename;
							img.propID = propID;
//							if (this.is_ie()){
								var flvhtml = '<img style="display:none;" src="&amp;action=objectWeb&amp;rpc=1&amp;subAction=getCachedBin&amp;propID='+ propID+ '">';
								flvhtml += '<object style="z-index:999;margin-top:50px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" style="z-index: 99;" data="wpsRepos2/lib/3rd/flashPlayer/flvplayer.swf" type="application/x-shockwave-flash" height="240" width="320" >';
								flvhtml += '<param value="#FFFFFF" name="bgcolor">';
								flvhtml += '<param name="movie" value="wpsRepos2/lib/3rd/flashPlayer/flvplayer.swf">';
								flvhtml += '<param name="flashvars" value="file=/upload/objectweb/' + propID + '.flv&amp;showdigits=true&amp;autostart=true">';
								flvhtml += '<param name="allowScriptAccess" value="sameDomain">';
								flvhtml += '<embed style="z-index:999;margin-top:50px;"  style="padding:50px;" src="wpsRepos2/lib/3rd/flashPlayer/flvplayer.swf" quality="high" bgcolor="#ffffff" wmode="transparent" style="z-index: 99;" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="file=/upload/objectweb/' + propID + '.flv&amp;showdigits=true&amp;autostart=true" height="240" width="320">';
								flvhtml += '</object>';
								flvhtml += '<div class="closer" onclick="closeBlocker();">Close</div>';								
								new wps.imageViewer(img, false, true, flvhtml);
								$('projectImages').appendChild(img);
//							}else{
//								var a = new Element('a', {id:propID + '_lightWindow',  className:"lightWindow", href:'http://' + window.location.host + gRelRootPath + 'Catalogue/&action=vpClient&rpc=1&xmlReq=<command><call><method>renderMovie</method><arg>fileName=' + filename + '</arg></call></command>'});
//								img.wrap(a);
//								a.setAttribute('type', 'page');
//								$('projectImages').appendChild(a);
//							}
						}else{
//							var thumb = filename.substring(0,(filename.length-4)) + '190.' + filename.substring((filename.length-3) , filename.length);
//							cachedImg += '<img style="display:none;" src="&amp;action=objectWeb&amp;rpc=1&amp;subAction=getCachedBin&amp;propID='+ propID+ '&amp;resizeWidth=190">';
//							var largeImg = gRelRootPath + '_wpsTemp/'+filename;
							if (this.is_ie()){
								html += '<span id="' + propID + '_lightWindow" class="lightWindowIE" myBigBrother="/upload/objectweb/' + propID + '.jpg" ><img myBigBrother="/upload/objectweb/' + propID + '.jpg"  src="/upload/objectweb/' + propID + '_190.jpg" /></span>';
							}else{
								html += '<a href="/upload/objectweb/' + propID + '.jpg" id="' + propID + '_lightWindow" class="lightWindow"><img src="/upload/objectweb/' + propID + '_190.jpg" /></a>';
							}
						}
					}
				}
			}).bind(this)
		);
//		if (cachedImg){
			var tmpNode = Builder.node('span', {id:'flashMovies'});
			$('projectImages').appendChild(tmpNode);
			Element.update(tmpNode, html + cachedImg);
//		}
		if (!this.is_ie()){
			$$('.lightWindow').each(
				function(elem){
					if (elem.id) {
						myLightWindow.createWindow(elem.id);
					}
				}
			)
		}else{
			$$('.lightWindowIE').each(
					(function(elem){
						if (elem.id) {
							$(elem).observe('click', this.ieZoom.bindAsEventListener(this));
							//new wps.imageViewer($(elem), false, true, largeImg);

						}
					}).bind(this)
				)
		}
		// set default image:
		//if ($('imgBrowser').hasChildNodes()) this._showImage($('imgBrowser').childNodes.item(0));
	},
	
	ieZoom : function(event){
		var elem = event.element();
		window.open(elem.getAttribute('myBigBrother'), this.instID, 'toolbar=no,status=no,menuBar=no,directories=no,location=no');
	},
	
	cloneImg : function(img){
		var imgSrc = img.getAttribute('src');
		return Builder.node('img', {src:imgSrc});
	},

	expandMovie : function(event){
		waiter();
		var elem = Event.element(event);
		var filename = elem.filename;
		var propID = elem.propID;
		this.openimgDisplay();
		this.timeoutCnt = 0;
		var container = $('imgDisplay');
	 	var html = '<img style="display:none;" src="&amp;action=objectWeb&amp;rpc=1&amp;subAction=getCachedBin&amp;propID='+ propID+ '">';
		html += '<div class="overlayPlayer"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" style="z-index: 99;" data="wpsRepos2/lib/3rd/flashPlayer/flvplayer.swf" type="application/x-shockwave-flash" height="240" width="320" >';
		html += '<param value="#FFFFFF" name="bgcolor">';
		html += '<param name="movie" value="wpsRepos2/lib/3rd/flashPlayer/flvplayer.swf">';
		html += '<param name="flashvars" value="file=' + gRelRootPath + '_wpsTemp/' + filename + '&amp;showdigits=true&amp;autostart=true">';
		html += '<param name="allowScriptAccess" value="sameDomain">';
		html += '<embed style="padding:50px;" src="wpsRepos2/lib/3rd/flashPlayer/flvplayer.swf" quality="high" bgcolor="#ffffff" wmode="transparent" style="z-index: 99;" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="file=/' + gRelRootPath + '_wpsTemp/' + filename + '&amp;showdigits=true&amp;autostart=true" height="240" width="320">';
		html += '</object>';
		html += '<div class="closer" onclick="closeBlocker();">Close</div></div>';
		Element.update(container, html);
		unWaiter();
/*		var newParentImg = this.cloneImg(elem);
		newParentImg.style.position = 'relative';
		newParentImg.style.top = '10px';
		newParentImg.style.zIndex = '99'
		newParentImg.style.cursor = 'pointer';
		container.appendChild(newParentImg);
		newParentImg.setAttribute('alt', "Click to close.")
		this.addEvent(newParentImg, 'click', this.showImgSmall.bind(this));
		this.resizeOverlayImage(newParentImg);		*/
	},

	expandImage : function(event){
		waiter();
		var elem = Event.element(event);
		var imageBrowser = new wps.imageViewer(elem);
	},

	openimgDisplay : function(){
		if (this.is_ie()){
	    	var screenSize = Element.getDimensions(document.body);
	    	var bottom = (screenSize.height);
	    	var right = screenSize.width;
	    	$('imgDisplay').style.position = 'absolute';
	    	$('imgDisplay').style.left = '0px';
	    	$('imgDisplay').style.top = '0px';
			$('imgDisplay').style.height = bottom + 'px';
			$('imgDisplay').style.width = right + 'px';
			$('imgDisplay').style.display = 'block';

		}else{
			$('imgDisplay').style.display = 'block';
		}
	},

	showNext : function(e){
		this.page++;
		var nextPage = this.objects[this.page];
		this.init(nextPage);
	},
	
	showPrevious : function(e){
		this.page--;
		var nextPage = this.objects[this.page];
		this.init(nextPage);
	},
	
	showImgSmall : function(event){
		var elem = Event.element(event);
		var elemParent = elem.parentNode;
		//elem.original.style.display = 'block';
		elemParent.removeChild(elem);
		this.closeimgDisplay();
	},
	
	closeimgDisplay : function(){
		closeBlocker();
	},
	
	resizeOverlayImage : function(elem){
		if (this.is_ie()){
			var width = elem.offsetWidth;
			var height = elem.offsetHeight;
		}else{
			var dim = elem.getDimensions();
			var width = dim.width;
			var height = dim.height;
		}
		if ((width < 50) ||(height < 50)){
			// timeouts actually only occur on FF
			this.timeoutCnt++;
			if (this.timeoutCnt < 5){
				var func = (function(){this.resizeOverlayImage(elem);}).bind(this)	
				setTimeout(func, 750);
				return;				
			}else{
				return;
			}
		} 
    	var screenSize = Element.getDimensions(document.body);
    	var absBottom = screenSize.height;
    	var absRight = screenSize.width;
		var resizeWidth =  (width > absRight);
		var resizeHeight = (height > absBottom);
		if (this.is_ie){
			var diff = 50;
		}else{
			var diff = 20;
		}
		if (resizeWidth && resizeHeight){
			// prefer height-resize over width;
			elem.style.height = (absBottom - diff);
			return;
		}
		if (resizeHeight){
			// again prefer height-resize over width;
			elem.style.height = (absBottom - diff);
			return;
		}
		if (resizeWidth){
			// resize width, height is already OK
			elem.style.width = (absRight - 20);	
			return;
		}
	},
	

	cleanBrowser : function(){
		$('projectTitle').update('');	
		$('projectDirector').update( '');
		$('projectReleaseDate').update('');
		$('projectCategory').update( '');
		$('projectLength').update( '');
		$('projectStory').update( '');	
		$('synopsis').update( '');
		$('projectDatasheet').update( '');
		if ($('imgWWW') ) $('navigator').removeChild($('imgWWW'));
	}

});