var clipTop  = 0;
var clipIncr = 5;
var clipMax  = 300;
var nextImage = 2;

var noAnim = 0;

function initAnim() {
	if ( !noAnim && animImages.length > 2 )
		setTimeout('clipImg()', 4000);
}

function clipImg() {
	document.getElementById('imgTop').style.clip = "rect(" + clipTop + "px,auto,auto,auto)";
	clipTop += clipIncr;
	if ( clipTop < clipMax ) {
		setTimeout('clipImg()', 40);
	} else {
		document.getElementById('imgTop').style.clip = "rect(300px,auto,auto,auto)";
		setTimeout('nextImg()', 4000);
	}
}

function nextImg() {
	tmp = document.getElementById('imgTop').src;
	document.getElementById('imgTop').src = document.getElementById('imgBck').src;
	document.getElementById('imgTop').style.clip = "rect(0px,auto,auto,auto)";
	document.getElementById('imgBck').src = animImages[ nextImage++ ];
	if ( nextImage == animImages.length )
		nextImage = 0;
	clipTop = 0;
	setTimeout('clipImg()', 20);
}



function pauseMS(ms) {
	var d = new Date();
	while (1) {
	var mill = new Date();
	var diff = mill - d;
	if (diff > ms )
		{ break; }
	}
}


function validateMail() {
//alert('validating ' + document.f.email.value);
	if (document.f.email.value.indexOf('@') == -1) {
		alert('Please enter valid email address!');
		return false;
	} else {
		return true;
	}
}

function showMenu() {
	Effect.Appear( 'submenu', { duration: 2.0 });
}


function initToggle() {
	$$('a.toggle').each(
		function( link ) {
			Element.hide( link.up().next('div.toggle') );
			link.up().next('div.toggle').style.visibility = 'visible';
			Event.observe( link, 'click',
				function( event ) {
					$$('H3').each( function(h3) { var el = h3.next('div.toggle'); if ( el.className == 'toggle' )	Element.hide(el); } )
					var el = Event.element(event).up('H3').next('div.toggle');
					if ( el.className == 'toggle' ) {
						if ( BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 6 ) {
							el.style.left = 0;
						}
						el.style.top  = Event.pointerY(event) - 100 + 'px';
						Element.toggle(el);
					}
				}
			);
		}
	);

	$$('a.close').each(
		function( link ) {
			Event.observe( link, 'click',
				function( event ) {
					Element.toggle( Event.element(event).up('div') );
				}
			)
		}
	);
}

function init() {
//	initToggle();
//	initAnim();
//	showMenu();
}

//window.onload = init;



document.observe("dom:loaded",
	function() {
		$$('a.toggle').each(
			function( link ) {
				var bq = findNextEl( link, 'BLOCKQUOTE' );;
				Element.hide( bq );
				// link.insert({ top: '<span style="font-size: 16px; vertical-align: 0px;">&#187;&nbsp;</span>' });
				bq.insert({ top: '<div class="close"><a class="close">x</a></div>' });
				Event.observe( link, 'click',
					function( event ) {
						var bq = findNextEl( link, 'BLOCKQUOTE' );
						var left = event.pointerX() - link.getOffsetParent().viewportOffset().left;
						if ( left > 250 ) left = 0;
						var top  = event.pointerY() - link.getOffsetParent().viewportOffset().top;
						if ( top > 250 ) top = 0;
						//bq.style.left  = left +  1 + 'px';
						//bq.style.top   = top  +  1 + 'px';
						Element.toggle( bq );
						event.stop();
					}
				);
			}
		);

		$$('a.close').each(
			function( link ) {
				Event.observe( link, 'click',
					function( event ) {
						Element.toggle( link.up('BLOCKQUOTE') );
						event.stop();
					}
				);
			}
		);

		initAnim();
		showMenu();


	}
);

function findNextEl( srcEl, nodeName ) {
	if ( srcEl.next( nodeName ) ) {
		return srcEl.next( nodeName );
	} else {
		var ancestors = srcEl.ancestors();
		for ( var i = 0, len = ancestors.length; i < len; i++ ) {
			foundEl = ancestors[ i ].next( nodeName );
			if ( ancestors[ i ].next( nodeName ) )
				return ancestors[ i ].next( nodeName );
		}
	}
}
















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();