var CONST = {
	numQuotes: {
		IT: 30,
		EN: 44
	}
};

var time = '';
var hours = (new Date()).getHours();

if (context == 'pandora') {
	time = '_day';

	if (hours <= 6) {
		time = '_night';
	} else if (hours >= 12) {
		time = '_afternoon';

		if (hours >= 20) {
			time = '_night';
		}
	}
}

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",	identity: "OmniWeb",	versionSearch: "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();

window.onresize = manageResize;

document.oncontextmenu = function() { return false; };

function manageResize() {
	var pageWidth;

	if (typeof(window.innerWidth) == 'number') {
		pageWidth = window.innerWidth;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		pageWidth = document.documentElement.clientWidth;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		pageWidth = document.body.clientWidth;
	}

	if (pageWidth < 950) {
		document.body.style.overflowX = 'scroll';
	} else {
		document.body.style.overflowX = 'hidden';
	}
};

function homeInit() {
	manageResize();

	if (cats[0].length > 1) { randomizeCats(0, 'F'); }
	if (cats[1].length > 1) { randomizeCats(1, 'M'); }
	if (cats[2].length > 1) { randomizeCats(2, 'C'); }

	if (BrowserDetect.browser != 'Explorer') {
		var div = document.getElementById('HOME-QUOTES');

		div.style.opacity = 1;
		div.style.MozOpacity = 1;
		div.style.filter = 'alpha(opacity=100)';
		div.style.left = '625px';

		setTimeout("fadeOutQuote();", 2500 + Math.floor(Math.random() * 10000));
	}

	if (!!document.createElement('audio').canPlayType) {
		document.getElementById('soundtrack-note').style.visibility = 'visible';
	} else {
		swfobject.embedSWF('img/soundtrack.swf', 'soundtrack', '25', '25', '9.0.0', false, {}, { play: 'true', loop: 'true', menu: 'false', wmode: 'transparent', quality: 'high' }, {});
	}

	document.getElementById('fieldEmail').onkeydown = function(e) {
		e = e || window.event;

		if (e.keyCode == 13) {
			submitEmail();
		}
	};
};

function toggleSoundtrack() {
	try {
		var audio = document.getElementsByTagName('audio')[0];

		if (audio.paused) {
			audio.play();
		} else {
			audio.pause();
		}

		return false;
	} catch(ex) { }
};

function innerInit() {
	manageResize();

	if (cats[0].length > 1) { randomizeCats(0, 'F'); }
	if (cats[1].length > 1) { randomizeCats(1, 'M'); }
	if (cats[2].length > 1) { randomizeCats(2, 'C'); }

	try { afterLoad(); } catch(ex) { }
};

function randomizeCats(index, context) {
	var img = document.getElementById('SIDE-CAT-' + context);
	var imgU = document.getElementById('SIDE-CAT-' + context + '-U');

	if (Number(img.style.opacity) == 1) {
		imgU.src = cats[index][Math.floor(Math.random() * cats[index].length)];
		setTimeout("fadeCatImage(" + index + ", '" + context + "', -1);",  2500 + Math.floor(Math.random() * 10000));
	} else {
		img.src = cats[index][Math.floor(Math.random() * cats[index].length)];
		setTimeout("fadeCatImage(" + index + ", '" + context + "', 1);", 2500 + Math.floor(Math.random() * 10000));
	}
};

function fadeCatImage(index, context, factor) {
	var img = document.getElementById('SIDE-CAT-' + context);
	var nextValue = (Number(img.style.opacity) * 10) + factor;

	img.style.opacity = (nextValue / 10);
	img.style.MozOpacity = (nextValue / 10);
	img.style.filter = 'alpha(opacity=' + (nextValue * 10) + ')';

	if (nextValue == 0 || nextValue == 10) {
		randomizeCats(index, context);
	} else {
		setTimeout("fadeCatImage(" + index + ", '" + context + "', " + factor + ");", 60);
	}
};

function fadeOutQuote() {
	var div = document.getElementById('HOME-QUOTES');
	var nextValue = (Number(div.style.opacity) * 10) - 1;

	div.style.opacity = (nextValue / 10);
	div.style.MozOpacity = (nextValue / 10);
	div.style.filter = 'alpha(opacity=' + (nextValue * 10) + ')';

	//div.style.left = (Number(String(div.style.left).replace('px', '')) + 1) + 'px';

	if (nextValue == 0) {
		div.style.backgroundImage = 'url(img/home/quotes/' + addZero(Math.floor(Math.random() * CONST.numQuotes[LAN])) + '_' + LAN + '.png)';
		setTimeout("fadeInQuote();", 1500);
	} else {
		setTimeout("fadeOutQuote();", 60);
	}
};

function fadeInQuote() {
	var div = document.getElementById('HOME-QUOTES');
	var nextValue = (Number(div.style.opacity) * 10) + 1;

	div.style.opacity = (nextValue / 10);
	div.style.MozOpacity = (nextValue / 10);
	div.style.filter = 'alpha(opacity=' + (nextValue * 10) + ')';

	//div.style.left = (Number(String(div.style.left).replace('px', '')) - 1) + 'px';

	if (nextValue == 10) {
		setTimeout("fadeOutQuote();", 5000 + Math.floor(Math.random() * 10000));
	} else {
		setTimeout("fadeInQuote();", 60);
	}
};

function submitEmail() {
	var email = document.getElementById('fieldEmail').value;

	if (email != emptyFieldText) {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		if (!filter.test(email)) {
			alert(LAN == 'IT' ? 'Indicare un indirizzo di posta elettronica valido!' : 'Please provide a valid email address!');
			document.getElementById('fieldEmail').focus();
		} else {
			window.location.href = 'newsletter.aspx?email=' + email;
		}
	}
};

function addZero(num) {
	num = Number(num);

	if (isNaN(num)) {
		return '00';
	} else {
		return (num < 10 ? '0' + num : '' + num);
	}
};

function schedaGatto(IDGatto) {
	window.location.href = 'gattopersiano.aspx?IDGatto=' + IDGatto;
};

function elencoGattiOverPergamena(IDGatto) {
	var elem;

	for (var ii = 0; ii < arrIDs.length; ii++) {
		elem = document.getElementById('PERGAMENA' + arrIDs[ii]);
		elem.style.visibility = (Number(arrIDs[ii]) == Number(IDGatto) ? 'visible' : 'hidden');
	}
};

function elencoGattiOutPergamena(IDGatto) {
	var elem = document.getElementById('PERGAMENA' + IDGatto);
	elem.style.visibility = 'hidden';
};

function titoloGattoOverPergamena(context) {
	var elem;

	elem = document.getElementById('PERGAMENACFA');
	if (elem) { elem.style.visibility = (context == 'CFA' ? 'visible' : 'hidden'); }

	elem = document.getElementById('PERGAMENAFIFE');
	if (elem) { elem.style.visibility = (context == 'FIFE' ? 'visible' : 'hidden'); }

	elem = document.getElementById('PERGAMENAFWCF');
	if (elem) { elem.style.visibility = (context == 'WCF' ? 'visible' : 'hidden'); }

	elem = document.getElementById('PERGAMENATICA');
	if (elem) { elem.style.visibility = (context == 'TICA' ? 'visible' : 'hidden'); }
};

function titoloGattoOutPergamena(context) {
	var elem = document.getElementById('PERGAMENA' + context);
	elem.style.visibility = 'hidden';
};

var timeoutPalmares = null;

function gattoOverPalmares(context) {
	if (timeoutPalmares != null) {
		clearTimeout(timeoutPalmares);
	}

	var elem;

	elem = document.getElementById('PERGAMENACOPPE');
	if (elem) { elem.style.visibility = (context == 'COPPE' ? 'visible' : 'hidden'); }

	elem = document.getElementById('PERGAMENACOCCARDE');
	if (elem) { elem.style.visibility = (context == 'COCCARDE' ? 'visible' : 'hidden'); }

	timeoutPalmares = setTimeout("gattoOutPalmares('" + context + "');", 10000);
};

function gattoOutPalmares(context) {
	var elem = document.getElementById('PERGAMENA' + context);
	elem.style.visibility = 'hidden';
};

try {
	$(document).ready(function(){
		var preventClick = false;

		$('.pic a').bind('click', function(e) {
			if (preventClick) {
				e.stopImmediatePropagation();
				e.preventDefault();
			}
		});

		$('.pic').draggable({
			containment: 'parent',
			start: function(e,ui) { preventClick = true; },
			stop: function(e, ui) { setTimeout(function(){ preventClick = false; }, 250); }
		});

		$('.pic').mousedown(function(e) {
			var maxZ = 0;

			$('.pic').each(function() {
				var thisZ = parseInt($(this).css('zIndex'))
				if (thisZ > maxZ) maxZ = thisZ;
			});

			if ($(e.target).hasClass('pic'))
				$(e.target).css({ zIndex: maxZ + 1 });
			else
				$(e.target).closest('.pic').css({ zIndex: maxZ + 1 });
		});

		$("a.fancybox-image").fancybox({
			'opacity'			: true,
			'centerOnScroll'	: true,
			'titlePosition'		: 'inside',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) { return '<span style="font-family: Trebuchet MS, Verdana; font-size: 12px; color: #f71a60;">' + title + '</span>'; }
		});

		$("a.fancybox-video").fancybox({
			'type'				: 'iframe',
			'opacity'			: true,
			'centerOnScroll'	: true,
			'width'				: 640,
			'height'			: 480,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition'		: 'inside',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) { return '<span style="font-family: Trebuchet MS, Verdana; font-size: 12px; color: #f71a60;">' + title + '</span>'; }
		});
	});
} catch(ex) { }

function getElementsByClassName(node, classname) {
	if (node.getElementsByClassName) {
		return node.getElementsByClassName(classname);
	} else {
		return (function getElementsByClass(searchClass,node) {
			if (node == null)
				node = document;

			var	classElements = [],
				els = node.getElementsByTagName("*"),
				elsLen = els.length,
				pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)"), i, j;

			for (i = 0, j = 0; i < elsLen; i++) {
				if ( pattern.test(els[i].className) ) {
					classElements[j] = els[i];
					j++;
				}
			}
			return classElements;
		})(classname, node);
	}
};

function showGalleries() {
	var galleries = getElementsByClassName(document, 'gallery-title-dd');

	for (var i = 0; i < galleries.length; i++) {
		galleries[i].style.visibility = 'visible';
	}
};

function hideGalleries() {
	var galleries = getElementsByClassName(document, 'gallery-title-dd');

	for (var i = 0; i < galleries.length; i++) {
		galleries[i].style.visibility = 'hidden';
	}
};

function displayMessage(message, title) {
	if (title === null) {
		$.fancybox({
			'padding'	: 10,
			'titleShow' : false,
			'content'	: '<span style="font-family: Trebuchet MS, Verdana; font-size: 18px; color: #f71a60;">' + message + '</span>'
		});
	} else {
		if (!title) {
			title = (LAN == 'IT' ? 'ATTENZIONE!' : 'PAY ATTENTION!');
		}

		$.fancybox({
			'padding'	: 10,
			'title'   	: title,
			'content'	: '<span style="font-family: Trebuchet MS, Verdana; font-size: 18px; color: #f71a60;">' + message + '</span>'
		});
	}
};




