if (typeof(WizzgoCore) == "undefined") WizzgoCore = {};

//onDOMReady Event Extension
//http://clientside.cnet.com/code-snippets/event-scripting/a-dom-ready-extension-for-prototype/
Object.extend(Event, {
	_domReady : function() {
		if (arguments.callee.done) return;
		arguments.callee.done = true;

		if (this._timer)  clearInterval(this._timer);
		WizzgoCore.isDomReady = true;
		if(this._readyCallbacks) this._readyCallbacks.each(function(f) { f() });
		this._readyCallbacks = null;
	},
	onDOMReady : function(f) {
		if(WizzgoCore.isDomReady) {
			f();
		}
		else {
			if (!this._readyCallbacks) {
				var domReady = this._domReady.bind(this);

				if (document.addEventListener)
				document.addEventListener("DOMContentLoaded", domReady, false);

				/*@cc_on @*/
				/*@if (@_win32)
				document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
				document.getElementById("__ie_onload").onreadystatechange = function() {
					if (this.readyState == "complete") domReady(); 
				};
				/*@end @*/

				if (/WebKit/i.test(navigator.userAgent)) { 
					this._timer = setInterval(function() {
						if (/loaded|complete/.test(document.readyState)) domReady(); 
						}, 10);
					}

					Event.observe(window, 'load', domReady);
					Event._readyCallbacks =  [];
				}
				Event._readyCallbacks.push(f);
			}
		}
	}
);

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));
	},
	isBuggyBrowser: function () {
		if(this.browser == "Explorer" && this.version < 7){
			return true;
		}
		else{
			return false;
		}
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	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();

var currentSlideshow = null;
SlideShow = Class.create();
SlideShow.prototype = {
	
	initialize: function() {
		this.translation = 590;
		this.current = 1;
		this.next = 2;
		this.previousOrNext = '';
		currentSlideshow = this;
	},
	
	slideToNextPage : function (){
		if(this.previousOrNext ===''){
			this.previousOrNext = 'next';
			new Effect.Move('SlideShowRow' + this.current, { x: -1 * this.translation, y: 0, transition: Effect.Transitions.sinoidal, mode: 'relative', duration:0.3, afterFinish: function() { currentSlideshow.slideToPageEnd(); }, queue: { position: 'end', scope: 'slide' } });
		}
	},
	
	slideToPreviousPage : function (){
		if(this.previousOrNext ===''){
			this.previousOrNext = 'previous';
			new Effect.Move('SlideShowRow' + this.current, { x: this.translation, y: 0, transition: Effect.Transitions.sinoidal, mode: 'relative', duration:0.3, afterFinish: function() { currentSlideshow.slideToPageEnd(); }, queue: { position: 'end', scope: 'slide' } });
		}
	},

	firstLast : function(element) {
		var firstElement = element.childElements().first(); 
		element.childElements().last().insert({ after:firstElement });
	},

	lastFirst : function(element) {
		var lastElement = element.childElements().last(); 
		element.childElements().first().insert({ before:lastElement });
	},
	
	slideToPageEnd : function() {
		var element1Name = 'SlideShowRow' + this.current;
		var element2Name = 'SlideShowRow' + this.next;
		var element1 = $(element1Name);
		var element2 = $(element2Name);

		if (this.previousOrNext === 'next') {
			this.firstLast(element2);
		}
		else {
			this.lastFirst(element2);
		}

		$(element1Name, element2Name).invoke('toggle');

		if (this.previousOrNext === 'next') {
			this.firstLast(element1);
		}
		else {
			this.lastFirst(element1);
		}

		element1.style.left = element2.style.left;

		var tmp = this.current;
		this.current = this.next;
		this.next = tmp;	
		
		this.previousOrNext = '';	
	}
}

 
 
// From  website javascript code 
Cookies = Class.create();
Cookies.prototype = {
	data: [],
 	name: null,
	initialize: function() {
		this.read();
	},
	
	resetData: function() {
		this.data = this.data.uniq();
		this.string = this.data.join(',');
	},
 
	setCookieName: function(inName) {
		this.name = inName;
	},
	
	getCookieName: function() {
		return this.name;
	},
 
	read: function() {
		var cookies = document.cookie.split(';');
		
		for (var i=0; i<cookies.length; i++) {
			var cookie = cookies[i];
 
			if (cookie.match(this.getCookieName())) {
				var seen = cookie.substring(cookie.indexOf('=')+1, cookie.length).split(',');
				for (var j=0; j<seen.length; j++) {
					if (seen[j] != '') this.data.push(seen[j]);
				}
 
				this.resetData();
				return;
			}
		}
	},
	
	set: function(id) {
		// set cookie to expire in 1 week
		var date = new Date();
		date.setDate(date.getDate()+7);
		
		var path = this.getCookieName();
 
		// comma delimited to sort later
		this.string = this.data.join(',');
		if (this.string.match(id) == null) {
			this.string = (this.string != '') ? this.string+','+id : id;
		}
 
		// set the cookie
		document.cookie = path+'='+this.string+'; expires='+date.toGMTString()+'; path='+path+'; domain=wizzgo.com';
 
		// remember we added it		
		this.data.push(id);
		this.resetData();
	},
	
	isSet: function(id) {
		if (!this.string) this.resetData();
		return this.string.match(id);
	}
 
};

GlobalInit = function() {
	var downloadLinksTable = $$('#mb-download a');
	if (downloadLinksTable != null && downloadLinksTable[0] != null) {
		var downloadLink = downloadLinksTable[0];
		if(navigator.appVersion.indexOf("Mac")!=-1) {
			downloadLink.setAttribute('href', '/download/macosx');
		}
		else{
			downloadLink.setAttribute('href', '/download/windows');
		}		
	}
};
Event.onDOMReady(GlobalInit);


