var gallery;

document.observe('dom:loaded', function () {  gallery = new Gallery(); });

function appicationGalleryDropCurtain(cubeid){
	gallery.start();	
	applicationGalleryShowStartbox(cubeid);
	document.getElementById('applicationLink').href = 'javascript:doNothing()';
}

function applicationGalleryRaiseCurtain(){
	document.getElementById('applicationLink').href = 'javascript:appicationGalleryDropCurtain('+GalleryOptions.cubeid+')';
	gallery.end();			
}

function applicationGalleryShowStartbox(cubeid){
	pars = 'cubeid='+cubeid+'&action=showStartbox';
	gallery.showStartboxContents('applications/gallery/controller.php', pars);
}

function startSlideShow(cubeid){
	projectCategory = document.getElementById('projectCategory');
	if(projectCategory != undefined && projectCategory.type == "select-one"){
		category = projectCategory[projectCategory.selectedIndex].value;
	}else{
		category = 'all';
	}
	pars = "cubeid="+cubeid+'&category='+category+'&action=startSlideShow';
	gallery.showImage('applications/gallery/controller.php',0,pars);		
}

function reStartSlideShow(cubeid){
	projectCategory = document.getElementById('projectCategoryToolbar');
	if(projectCategory != undefined && projectCategory.type == "select-one"){
		category = projectCategory[projectCategory.selectedIndex].value;
	}else{
		category = 'all';
	}
	pars = "cubeid="+cubeid+'&category='+category+'&action=startSlideShow';
	gallery.showImage('applications/gallery/controller.php',0,pars);		
}

function nextImage(cubeid, category){
	GalleryOptions.imgindex++;
	pars = "cubeid="+cubeid+'&category='+category+'&action=startSlideShow';
	gallery.showImage('applications/gallery/controller.php',GalleryOptions.imgindex,pars);	
}

function previousImage(cubeid, category){
	GalleryOptions.imgindex--;
	
	pars = "cubeid="+cubeid+'&category='+category+'&action=startSlideShow';
	gallery.showImage('applications/gallery/controller.php',GalleryOptions.imgindex,pars);	
}

function previousProject(cubeid, category){
	if(GalleryOptions.imgindex<0){
		GalleryOptions.imgindex = 0;
	}
	pars = "cubeid="+cubeid+'&category='+category+'&action=startSlideShow&skip=previousProject';
	gallery.showImage('applications/gallery/controller.php',GalleryOptions.imgindex,pars);	
}

function nextProject(cubeid, category){
	pars = "cubeid="+cubeid+'&category='+category+'&action=startSlideShow&skip=nextProject';
	gallery.showImage('applications/gallery/controller.php',GalleryOptions.imgindex,pars);	
}
	
function scaleIt() {
	
	  var scalePhoto = document.getElementById('imageArea');
	  windowSize = gallery.getWindowSize();
	  
	  imageSize = GalleryOptions.imageSize;

  	  width = imageSize[0];
	  height = imageSize[1];  
	 	
	 
	  requestWidth = (windowSize[0]>495)?(windowSize[0] - 245):250;
	  requestHeight = (windowSize[1] >330)?(windowSize[1] - 80):250;
	  
	  imageAspectRatio = imageSize[0]/imageSize[1];
	  viewAspectRatio =  requestWidth/requestHeight;
	  
	  if(viewAspectRatio > imageAspectRatio){
	  		height = requestHeight;
	  		width = imageAspectRatio*height;
	  }else if(viewAspectRatio < imageAspectRatio){
	  		width = requestWidth;
	  		height = width/imageAspectRatio;
	  }else{
	  		width = requestWidth;
	  		height = requestHeight;
	  }	  

	  current_width = scalePhoto.style.width;
	  currentWidth = current_width.substr(0, (current_width.length-2));
	  
	  current_height = scalePhoto.style.height;
	  currentHeight = current_height.substr(0, (current_height.length-2));

	  if(currentWidth < 250){
	  	width = 250+'px';	  	
	  }
	  if(width > imageSize[0]){
	  	width = imageSize[0];
	  }
	  
	  if(currentHeight < 250){
	  	height = 250+'px';	  	
	  }
	  
	  if(height > imageSize[1]){
	  	height = imageSize[1];
	  }
	  
	  document.getElementById('imageTable').style.height = windowSize[1]-80;
	  scalePhoto.style.width = width;
	  scalePhoto.style.height = height;	  
	} 

function truncateText(){
		topContentVisibility = true;
		containerLeftWidth = document.getElementById('topContainerLeft').getWidth();
		containerRightWidth = document.getElementById('topContainerRight').getWidth();
	
		if((containerLeftWidth < 1)||(containerRightWidth<1)){
			gallery.topContent.show();
			gallery.bottomContent.show();
			topContentVisibility = false;
		}
		
		projectTitle = GalleryOptions.title;
		categoryText = GalleryOptions.projectCategory;
		containerLeftWidth = document.getElementById('topContainerLeft').getWidth();
		containerRightWidth = document.getElementById('topContainerRight').getWidth();
		//alert(containerLeftWidth+' '+containerRightWidth);
		if(containerLeftWidth !=0){
			if(projectTitle != ''){
				titlelength = projectTitle.length*12;		
				//alert(titlelength+20+' '+containerLeftWidth);
				if((titlelength+20)>containerLeftWidth){
					if(projectTitle.length>12){
						document.getElementById('projectTitle').innerHTML = projectTitle.substr(0,12)+'...';
					}
				}else{
					document.getElementById('projectTitle').innerHTML = projectTitle;
				}
			}
		}
		
		if(containerRightWidth != 0){
			if(categoryText != ''){
				categoryLength = categoryText.length*12;
				if((categoryLength+30)>containerRightWidth){			
					newCategoryWidth = Math.ceil((containerRightWidth-30)/12);
					if(categoryText.length>newCategoryWidth){
						document.getElementById('categoryText').innerHTML = categoryText.substr(0,newCategoryWidth)+'...';
					}
				}else{
					document.getElementById('categoryText').innerHTML = categoryText;
				}	
			}	
		}
		if(!topContentVisibility){
			gallery.topContent.hide();
			gallery.bottomContent.hide();
		}
	}
	
	function getPositionLeft(This){
		var el = This;var pL = 0;
		while(el){pL+=el.offsetLeft;el=el.offsetParent;}
		return pL
	}

// To find the top position, add this snippet to your code:

function getPositionTop(This){
	var el = This;var pT = 0;
	while(el){pT+=el.offsetTop;el=el.offsetParent;}
	return pT
}

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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			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();