var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
	if(BrowserDetect.browser == 'Firefox' && parseInt(BrowserDetect.version) < 1.5)
	{
		alert('This portfolio requires Firefox version 1.5 or greater. Please upgrade your browser.');
	}else if(BrowserDetect.browser == 'Explorer' && parseInt(BrowserDetect.version) < 6){
		alert('This portfolio requires Intertnet Explorer version 6.0 or greater. Please upgrade your browser.');	
	}
	var init = true;
	var initLoopCount=5;
	var updatePreLoadDone=false;
	
	var ImagePreloader = new Class({ 
		getOptions: function(){ 
			return { 
				onProgress : Class.empty, 
				onComplete : Class.empty, 
				hrefIndex : 0 // in case 'images' is an mutlti dimensional array, index of HREF 
				}; 
		}, 
		initialize: function(options){ 
			this.setOptions(this.getOptions(), options); 
			if (this.options.initialize) this.options.initialize.call(this); 
			this.img = new Image(); 
			this.img.onload = function() { 
				/*
				$('testAsset').innerHTML = $('testAsset').innerHTML + '<br>======================================================';
				$('testAsset').innerHTML = $('testAsset').innerHTML + '<hr>';
					$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClassInit currArrayIdx:'+this.current+'<br>');
					$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClassInit id:'+this.img.id+'<br>');
					$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClassInit src:'+this.img.src+'<br>');
					$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClassInit idnum:'+this.img.idnum+'<br>');
					$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClassInit idRef:'+this.img.idRef+'<br>');
				$('testAsset').innerHTML = $('testAsset').innerHTML + '<hr>';
				$('testAsset').innerHTML = $('testAsset').innerHTML + '======================================================<br>';
				*/
				this.options.onProgress.delay(10,null,[this.current, this.img.src, this.img.idRef, this.img.id, this.img.idnum]); 
				if(!this.stopped) this._next.delay(10,this); 
			}.bind(this); 
		}, 
		preload : function(basepath, images) { 
			this._basepath = basepath; 
			this.images = ($type(images) == 'array') ? images : [images]; 
			this.current = -1; this.stopped = false; this._next(); 
		}, 
		stop : function() { this.stopped = true; }, 
		resume : function() { 
			if(this.stopped && this.images.length && this.current != -1) this._next(); 
		}, 
		_next : function() { 
			if(this.current < this.images.length - 1) { 
				this.current++; 
			} else { 
				this.options.onComplete.delay(10); 
				return; 
			} 
			
			var href = this.images[this.current].src;
			var idRef = this.images[this.current].id;
			var id = '_'+this.images[this.current].id;
			var idnum = this.images[this.current].idnum;
			this.img.src = this._basepath + href; 
			this.img.idRef = idRef;
			this.img.id = id;
			this.img.idnum = idnum;
			/*
			$('testAsset').innerHTML = $('testAsset').innerHTML + '<hr>';
			$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClass currArrayIdx:'+this.current+'<br>');
			$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClass id:'+this.img.id+'<br>');
			$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClass src:'+this.img.src+'<br>');
			$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClass idnum:'+this.img.idnum+'<br>');
			$('testAsset').innerHTML = $('testAsset').innerHTML +("\n"+'inClass idRef:'+this.img.idRef+'<br>');
			$('testAsset').innerHTML = $('testAsset').innerHTML + '<hr>';
			*/
		} 
	}); 
	ImagePreloader.implement(new Options);
	
	function reloadClient(id,clientname,loopCount){
		showClient(id,clientname,loopCount);
	}
	
	function showClient(id,clientname,loopCount){
		var splashImg,splashImg2,thumbCSSSelector,currDisplay,portThumbs,timeout,initSrc,fxIn,fxOut;
		var clientname = clientname;
		var loopCount = loopCount;
		// hide currentDisplay
		portDisid='portDisplay_'+currClientId;
		portLink='portLink_'+id;
		_portSplashImg='portSplashImg_'+id;
		portSplashImg=$(_portSplashImg);
		_portSplashImgPreLoadedImgid='_portSplashImg_'+id;
		portSplashImgPreLoadedImgid=$(_portSplashImgPreLoadedImgid);
		_portSplashImg2='portSplashImg2_'+id;
		portSplashImg2=$(_portSplashImg2);
		_portSplashImg2PreLoadedImgid='_portSplashImg2_'+id;
		portSplashImg2PreLoadedImgid=$(_portSplashImg2PreLoadedImgid);
				
		_loader = '#'+portLink + ' #l_portLink_'+id;
		//console.log(_loader);
		loadMessage = $$(_loader);
		//console.log('loop ' + loopCount + ' ' + loadMessage);
		if(parseInt(loopCount)<=initLoopCount){
			ready=true;
			//alert('parseInt');
			//console.log('parseInt ' +loader + ' ' + loopCount);
		}else if (!portSplashImgPreLoadedImgid){
			ready=false;
		}else{
			ready=true;
			//alert('good to go');
		}
		
		if(ready){
			if(parseInt(loopCount)>=initLoopCount){
				portSplashImg.src=portSplashImgPreLoadedImgid.src;
			    if(portSplashImg2PreLoadedImgid){
					portSplashImg2.src=portSplashImg2PreLoadedImgid.src;
				}
			}
			
			lastPortLink='portLink_'+currClientId;
			if(!init){
				$(portDisid).setStyle('display','none');
				$(lastPortLink).removeClass('highlite');
			}
			init=false;
			currClientId=id;
			// update title
			$('clientname').innerHTML = clientname;
			$(portLink).addClass('highlite');
			// initialize thisDisplays variables
			portSImgId='portSplashImg_'+id;
			var splashImg=$(portSImgId);
			portSImgId2='portSplashImg2_'+id;
			var splashImg2=$(portSImgId2);
			
			var thumbCSSSelector='#contentthumbs_'+id+' img';
			// setup thisDisplay
			// show thisDisplay
			$('portDisplay_'+id).setStyle('display','block');
			if(splashImg2)splashImg2.setOpacity(0);
			var currDisplay = splashImg;
			var portThumbs = $$(thumbCSSSelector);
			var timeout=null;
			var initSrc = splashImg.src;
			var fxIn = splashImg.effects({duration: 700, wait:false,transition: Fx.Transitions.Quart.easeInOut});
			if(splashImg2)var fxOut = splashImg2.effects({duration: 700, wait:false,transition: Fx.Transitions.Quart.easeInOut});
				
			portThumbs.each(function(element) {
				//console.log(element);	
				element.addEvent('mouseenter', tOver.bind(element));
				element.addEvent('mouseleave', tOut.bind(element));
			});
		}else{// !loader
			//console.log('not loaded' + loader + ' ' + loopCount);
			
			if(!preLoadDone){
				alert('This client has not finished loading yet');
			}else{
				alert('This one seems to be stuck, we\'ll trying loading it again.');
				path = portSplashImg.getProperty('path');
				src = '/_media/img/portfolio/'+path;
				var upPreImg = new Asset.image(src,  {
					'style':'position:absolute;left:-8000px;',
					'idRef': 'portSplashImg_'+id,
					'id':'_portSplashImg_'+id,
					'idnum':idnum,
					'class':'preLoadedImages',
					onload: function(){
						$(document.body).appendChild(this);
						_loading = 'l_portLink_'+id;
						_loading2 = 'l2_portLink_'+id;
						loading = $(_loading);
						loading2 = $(_loading2);
						if(loading){
							loading.remove();
							loading2.remove();
						}
						reloadClient(id,clientname,loopCount);
					}
				});
				
			}
		}
			
		function tOver(event){
			$clear(timeout);
			timeout = overFX.delay(200, this);
		}
		function tOut(event){
			$clear(timeout);
			timeout = outFX.delay(500, this);
		}
		function overFX(){
			element = this;
			
			if(currDisplay == splashImg){
				splashImg2.src = element.getProperty('path');
				fxIn.start({'opacity': 0});
				fxOut.start.delay(100,fxOut,{'opacity': 1});
				currDisplay = splashImg2;
			}else{
				splashImg.src = element.getProperty('path');
				fxOut.start({'opacity': 0});
				fxIn.start.delay(100,fxIn,{'opacity': 1});
				currDisplay = splashImg;
			}
		}
		
		function outFX(){
			if(currDisplay == splashImg){
				splashImg2.src = initSrc;
				fxIn.start({'opacity': 0});
				fxOut.start({'opacity': 1});
				currDisplay = splashImg2;
			}else{
				splashImg.src = initSrc;
				fxIn.start({'opacity': 1});
				fxOut.start({'opacity': 0});
				currDisplay = splashImg;
			}
		}
		
	}
	
	

	