slideTime = 500;

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",
			versionSearch: "Version"
		},
		{
			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.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

jQuery(document).ready(function(){

if (
	BrowserDetect.browser == "Safari"
	|| BrowserDetect.browser == "Chrome"
) { 
	var scrollObject = 'body';
	//alert(BrowserDetect.browser + ' ' + scrollObject); 
} else {
	var scrollObject = 'html';
}





	// REFER-TO FUNCTIONS :: START
	function hideFloater() {

				jQuery('body').removeClass("something-floats");
				jQuery('#FloatLarge').hide();
	}
	
	function closeAllWrappers() {
	
		jQuery('.highlighted').removeClass("highlighted");
		jQuery('.open-wrapper').removeClass("open-wrapper");
		jQuery('.wrapper').css({ 'display': 'inline-block', 'background-color': 'transparent', 'border': 'none' }); // fade in all wrappers
		jQuery('.wrapper-content').hide(); 		
		jQuery('.additional-image').hide();
	}

	function largeFilename(src) {
		fileName = src.split("/");
		fileTitle = fileName[fileName.length-1].split(".");
		return "http://kisd.de/~mborn/large/" + fileTitle[0] + "_large.jpg";
	}
	// REFER-TO FUNCTIONS :: END





	// CHAPTER CONTROL FUNCTIONS :: START
	jQuery('#MB').click(function(){
		
		if(jQuery('#Title').hasClass('visible')) {
			jQuery('#Title').removeClass('visible').hide();
		} else {
			jQuery('#Title').addClass('visible').show();
		}			
	});

	jQuery('#News').click(function(){
		
		if(jQuery('#NewsWrapper').hasClass('visible')) {
			jQuery('#NewsWrapper').removeClass('visible').hide();
		} else {
			jQuery('#NewsWrapper').addClass('visible').show();
		}			
	});

	jQuery('#Work').click(function(){
		
		if(jQuery('#WorkWrapper').hasClass('visible')) {
			jQuery('#WorkWrapper').removeClass('visible').hide();
			jQuery('#WorkChapters').hide();
		} else {
			jQuery('#WorkWrapper').addClass('visible').show();
			jQuery('#WorkChapters').show();
		}			
	});

	jQuery('#About').click(function(){
		
		if(jQuery('#AboutWrapper').hasClass('visible')) {
			jQuery('#AboutWrapper').removeClass('visible').hide();
		} else {
			jQuery('#AboutWrapper').addClass('visible').show();
		}			
	});
	// CHAPTER CONTROL FUNCTIONS :: END





	// CLICK FUNCTIONS :: START

	// Work chapter control
	jQuery('.chapter').click(function() {
		
		jQuery('body').removeClass('brown').removeClass('grey').addClass(jQuery(this).attr('rel'));
		closeAllWrappers();
	});



	// Wrapper control 1
	jQuery('.wrapper').click(function(e) { 
		e.stopPropagation();
		hideFloater();
		
		closeAllWrappers();
		jQuery(this).addClass("open-wrapper").css({ 'display': 'block' }); // store that this wrapper is now open, display as block
		jQuery(this).find('img').addClass("highlighted");
		jQuery(this).find('.additional-image').show(); // show additional images
		jQuery(this).next('.wrapper-content').show(); // show text content wrapper

		/*
		var oCoords = jQuery(this).offset();
		var iCurrentScroll = jQuery(window).scrollTop();

		if(iCurrentScroll > oCoords.top - 50) {
			jQuery(scrollObject).animate({scrollTop: oCoords.top - 50}, slideTime, 'easeOutCubic');
		}
		if(oCoords.top - iCurrentScroll + jQuery(this).parent().height() - window.innerHeight > -50) {
			jQuery(scrollObject).animate({scrollTop: oCoords.top + jQuery(this).parent().height() - window.innerHeight + 50}, slideTime, 'easeOutCubic');
		}
		*/
		return false;
	});		


	jQuery('.wrapper-content').click(function() { 
		return false;
	});		



	// Wrapper control 2: closing by click on body
	jQuery('body').click(function() {
		if(jQuery('body').hasClass("something-floats")) {
			hideFloater();
		} else {
			closeAllWrappers()
		}
	});



	// Image zoom
	jQuery('.product img, .project img, div.slide').click(function(e) {
		xPos = jQuery(this).offset.left + 10 + 'px';
		yPos = jQuery(this).offset.top + 10 + 'px';
		if (jQuery(this).closest('.wrapper').hasClass('open-wrapper')) {
			e.stopPropagation();
			jQuery('body').addClass("something-floats");
			if(jQuery(this).attr("src")) { 
				candle = jQuery(this);
			} else { 
				candle = jQuery(this).find('.foreground:first').find('.current:first'); 
			}
			var candleOffset = candle.offset();
			var candleWidth = candle.width() * 2 + 'px';
			//alert (candleOffset.top);
			jQuery('#FloatLarge').show().css({ top: candleOffset.top + 10 + 'px', left: candleOffset.left + 10 + 'px', width: candleWidth }).draggable();
			jQuery('#FloatLarge').find('img').attr('src', '').attr('src', candle.attr("src")).attr('alt', '').css({ width: candleWidth }); //.attr('alt', largeFilename(candle.attr("src")))
		}
	});



	// News toggle
	jQuery('.news-head span').click(function(){
	
		if (!jQuery(this).closest('.news').hasClass('active')) { // Wenn auf einen NICHT aktiven Eintrag geklickt wird:
			// ... diesen Eintrag einblenden, alle anderen ausblenden:
			jQuery('.news').removeClass("active");
			jQuery('.news').find('.news-content').hide();
			jQuery(this).closest('.news').addClass('active').find('.news-content').show();
			closeAllWrappers();
		}
		else { // Wenn auf einen AKTIVEN Eintrag geklickt wird:
			// ... alle Einträge ausblenden:
			jQuery('.news').find('.news-content').hide();
			jQuery('.news').removeClass("active");
		}
	});		





});







