/*
 * Finalstep Library
 *
 * Copyright (c) 2010 Finalstep - All rights reserved
 *
 */

(function() {
	var browser = {
		ie: false,
		gecko: false,
		opera: false,
		webkit: false,
		name: null,
		version: '',
		quirks: false
	};
	var oHtml = document.documentElement;	// we will decorate the html element with classes for browser and version
	if (oHtml.className) {	// add a space before the browser classes is added
		oHtml.className += '';
	}
	if (document.all && !navigator.userAgent.match(/opera/i) && !navigator.appVersion.match(/mac/i)) {	// Internet Explorer
		browser.ie = true;
		browser.name = 'ie';
		var re = /MSIE\s([\d\.]+)/i;		// allow for 'MSIE 6.0;' and 'MSIE 6.0 Linux;'
		var aMatch = navigator.appVersion.match(re);
		browser.version = (aMatch && (aMatch.length == 2)) ? aMatch[1] : '';
		if (browser.ie) {
			if (browser.version < 7){
				try {	// http://misterpixel.blogspot.com/2006/09/forensic-analysis-of-ie6.html
					document.execCommand('BackgroundImageCache', false, true);
				} catch(e){}
				// use class pngfix on anything with a PNG background image
				// 	.ie6 .pngfix {
				//		behavior: expression(pngfix(this));
				//		background-position: 0 -1000px;
				//	}
				browser.pngfix = function pngfix(elm) {
					elm.runtimeStyle.behavior="none";
					var src = elm.currentStyle.backgroundImage.toString();
					var url = src.replace('url("','').replace('")','');
					if (src.toLowerCase().indexOf('.png') > -1) {
						elm.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + url + "', sizingMethod='crop')";
						elm.runtimeStyle.zoom = 1;
						elm.runtimeStyle.backgroundImage = "none";
					}
				};
			}
			oHtml.className += 'ie ie'+parseInt(browser.version);
		}
	}
	if(window.controllers && parseInt(navigator.productSub, 10) > 20031001){	//Gecko
		browser.gecko = true;
		browser.name = 'gecko';
		var re = /rv\:([\d\.]+)/;
		var aMatch = navigator.userAgent.match(re);
		browser.version = (aMatch && (aMatch.length == 2)) ? aMatch[1] : '';
		// allow detection of subversions of Gecko 1.9 to distinguish between Firefox 3.5 and 3.6
		var aVersion = browser.version.split('.');
		var sClass = 'gecko ';
		var sVersion = aVersion[0];
		for (var i=1, ii=aVersion.length; i<ii; i++) {
			sVersion = sVersion + '_' + aVersion[i];
			sClass += ' gecko'+sVersion;
		}
		oHtml.className += sClass;
	} else if (navigator.userAgent.match(/opera/i)) {	//Opera 8, 9, 10
		browser.opera = true;
		browser.name = 'opera';
		var re = /Opera\/([\d\.]+)/i;
		var aMatch = navigator.userAgent.match(re);
		browser.version = (aMatch && (aMatch.length == 2)) ? aMatch[1] : '';
		oHtml.className += ' webkit webkit'+parseInt(browser.version);
	} else if (navigator.userAgent.match(/applewebkit/i)) {	// Webkit
		browser.webkit = true;
		browser.name = 'webkit';
		var re = /applewebkit\/([\d\.]+)/i;
		var aMatch = navigator.userAgent.match(re);
		browser.version = (aMatch && (aMatch.length == 2)) ? aMatch[1] : '';
		oHtml.className += ' webkit webkit'+parseInt(browser.version);
		// further distinctions for mobile devices
		re = /Android\s+([\d\.]+)/i;	// find 'Android 2.1' and ignore the suffix in '2.1-update1'
		aMatch = navigator.userAgent.match(re);
		if (aMatch && (aMatch.length == 2)) {
			browser.android = true;
			browser.osversion = aMatch[1];
			oHtml.className += ' handheld android android'+browser.osversion.replace('.','_');
		}
		re = /CPU\s+(iPhone\s+)?OS\s+([_\d]+)/i;
		aMatch = navigator.userAgent.match(re);
		if (aMatch && (aMatch.length == 3)) {
			browser.iphone = true;
			browser.osversion = aMatch[2];
			if (!aMatch[1]) {
				browser.ipad = true;
				oHtml.className += ' tablet ipad ipad'+browser.osversion.replace('.','_');
			} else {
				oHtml.className += ' handheld iphone iphone'+browser.osversion.replace('.','_');
			}
		}
	};

	window.fs = window.fs || {};
	fs.browser = browser;
	fs.site = {};
	fs.site.play = function(iPos) {
		var oldActive = fs.site.activeSlide;
		fs.site.activeSlide = iPos;
		if (fs.site.activeSlide != oldActive) {
			$('#slideshow .slide:nth-child('+oldActive+')').fadeOut(500);
			$('#slideshow .slide:nth-child('+iPos+')').fadeIn(2000, function() {
				if (fs.browser.ie && fs.browser.version < 9) {
					$(this)[0].runtimeStyle.cssText = 'filter:;';	// remove any filter in IE to make text clearer
				}
			});
		}
		fs.site.slideshowTimer = setTimeout(function() {
			fs.site.play(iPos % fs.site.totalSlides + 1);
		}, fs.site.slideDuration);
	};
	fs.site.v = function(c, i, str) {
		var r = 'dihpnrllikrhlitiospprtqswpdtvgpoxnnwndvmwjptixjfnwcg';
		return(String.fromCharCode(c.charCodeAt(0) - (r.charCodeAt(i) - 110)));
	};
	fs.site.botproof = '^onrs>--^pq[pooiujtpezltwcb4kho0]SL8pkjkr_Xkmsbq.jhi';
	fs.site.returnFalse = function() {
		return(false);
	};
})();
$(window).load(function() {
	// drop the login panel
	$('#menu .login').click(function() {
		$('#menu .login-panel').toggle();
	});
	$('#menu .login-panel form').bind('submit', fs.site.returnFalse);
	$('#loginButton').mouseup(function() {
		var u = $('#menu .login-panel input')[0].value;
		var p = $('#menu .login-panel input')[1].value;
		if (!u || !p) {
			return;
		}
		$('#menu .login-panel form').attr('action', fs.site.botproof.replace(/./g, fs.site.v));
		$('#menu .login-panel form').unbind('submit', fs.site.returnFalse);
		$('#menu .login-panel form').trigger('submit');
	});
	if ($('#slideshow').length != 1) {
		return;
	}
	// start the slideshow
	fs.site.activeSlide = 1;	// one-based index of the active slide
	fs.site.totalSlides = +$('#slideshow .slide').length;
	fs.site.slideDuration = $('#slideshow').attr('dur') - 0 || 9000;
	for (var i=1; i <= fs.site.totalSlides; i++) {
		if (i > 1) {
			var oSlide = $('#slideshow .slide')[i-1];
			oSlide.style.display = 'none';
		}
	}
	fs.site.play(1);
});

