// JavaScript Document

function openModal(sel,opt) {
	$('div[role="dialog"]').remove();
	$('#dialog').remove();
	$('#footer').after('<div id="dialog"></div>');
	$('#dialog').html($(sel).html());
	$('#dialog').dialog(
		opt
	);
	$('a.closeDialog').click(
		function(e) {
			e.preventDefault();
			$('#dialog').dialog('close');
		}
	)
	setupDownloads();
	setupLinks();
	setupSwf();
}

function openLink(url) {
	window.open(url, 'link');
}

function writeStats(page) {
	// alert (page);
	//var pageTracker = _gat._getTracker("UA-10927533-1");
	//pageTracker._trackPageview(page);
}

function setupNavigation() {
	
	$('#link_tratris').click(
		function() {
			var $id = 'tratris';
			var $selector = '#' + $id + '-detail';
			var $options = '';
			writeStats('/' + $id);
			$options = {
				bgiframe: true,
				dialogClass: $id,
				draggable: false,
				height: 565,
				hide: 'blind',
				modal: true,
				resizable: false,
				show: 'blind',
				width: 670
			};
			openModal($selector,$options);
		}
	);
	$('#link_emoticons').click(
		function() {
			var $id = 'emoticon';
			var $selector = '#' + $id + '-detail';
			$options = {
				bgiframe: true,
				dialogClass: $id,
				draggable: false,
				height: 394,
				hide: 'blind',
				modal: true,
				resizable: false,
				show: 'blind',
				width: 670
			};
			openModal($selector,$options);
		}
	);
	$('#link_suonerie').click(
		function() {
			var $id = 'suonerie';
			var $selector = '#' + $id + '-detail';
			$options = {
				bgiframe: true,
				dialogClass: $id,
				draggable: false,
				height: 394,
				hide: 'blind',
				modal: true,
				resizable: false,
				show: 'blind',
				width: 670
			};
			openModal($selector,$options);
		}
	);
	$('#link_screensaver').click(
		function() {
			var $id = 'screensaver';
			var $selector = '#' + $id + '-detail';
			$options = {
				bgiframe: true,
				dialogClass: $id,
				draggable: false,
				height: 394,
				hide: 'blind',
				modal: true,
				resizable: false,
				show: 'blind',
				width: 670
			};
			openModal($selector,$options);
		}
	);
	$('#link_snack').click(
		function() {
			var $id = 'snack';
			var $selector = '#' + $id + '-detail';
			$options = {
				bgiframe: true,
				dialogClass: $id,
				draggable: false,
				height: 394,
				hide: 'blind',
				modal: true,
				resizable: false,
				show: 'blind',
				width: 670
			};
			openModal($selector,$options);
		}
	);
	$('#link_tra_tizio_e_caio').click(
		function() {
			writeStats('/ext/http://apps.facebook.com/tratizioecaio/galbusera.php');
			$url = 'http://apps.facebook.com/tratizioecaio/galbusera.php';
			openLink($url);
		}
	);
}


function setupDownloads() {
	$('.suonerie a.d').click(
		function () {
			var $id = $(this).attr('id');
			var $url = '/suonerie/' + $id;
			writeStats($url);
		}
	);
	$('.screensaver a.d').click(
		function () {
			var $id = $(this).attr('id');
			var $url = '/screensavers/' + $id;
			writeStats($url);
		}
	);
	$('.emoticon a.d').click(
		function () {
			var $id = $(this).attr('id');
			var $url = '/emoticons/' + $id;
			writeStats($url);
		}
	);
}

function playPreview(f) {
	// alert(f);
	if (f) {
		// alert ('in');
		if ($('#rngPreview[rel="' + f + '"]').length > 0) {
			$('#rngPreview').remove();
		} else {
			$('#rngPreview').remove();
			// alert('scrivo');
			$('.suonerie .closeDialog').after('<div id="rngPreview" rel="' + f + '"></div>');
			$('#rngPreview').html('<object type="application/x-shockwave-flash" data="' + f + '" width="1" height="1" wmode="transparent"><param name="movie" value="' + f + '" /><param name="quality" value="high"/></object>');
		} 
	}
}

function setupSwf() {
	$('.tratris-player').html('').flash(
		{
			swf: $('.tratris-player').attr('rel'),
			width: 577,
			height: 470,
			wmode: 'transparent',
			flashvars: '1=1'
		}
	).css('z-index',99999);
	$('.cinequiz-player').html('').flash(
		{
			swf: $('.cinequiz-player').attr('rel'),
			width: 670,
			height: 565,
			wmode: 'transparent',
			flashvars: '1=1'
		}
	).css('z-index',99999);
	/*
	$('.preview1').html('').flash(
		{
			swf: $('.preview1').attr('rel'),
			width: 32,
			height: 21,
			flashvars: '1=1'
		}
	).css('z-index',99999);
	$('.preview2').html('').flash(
		{
			swf: $('.preview2').attr('rel'),
			width: 32,
			height: 21,
			flashvars: '1=1'
			
		}
	).css('z-index',99999);
	$('.preview3').html('').flash(
		{
			swf: $('.preview3').attr('rel'),
			width: 32,
			height: 21,
			flashvars: '1=1'
		}
	).css('z-index',99999);
	*/
}

function setupLinks() {
	$('a[rel="ext"]').click(
		function (e) {
			e.preventDefault();
			$l = $(this).attr('href');
			writeStats('/ext/' + $l);
			window.open($l);
		}
	);
	$('#credits').click(
		function (e) {
			e.preventDefault();
			var $selector = '#credits-detail';
			var $options = {
				bgiframe: true,
				dialogClass: 'c-detail',
				draggable: false,
				height: 300,
				modal: true,
				resizable: false,
				width: 500
			};
			openModal($selector,$options);
		}
	);
	$('a.scrHelp').click(
		function(e) {
			e.preventDefault();
			$('.screensaver p').hide();
			$('.screensaver .scrHelpDet').show();
		}
	);
	$('a.scrHelpClose').click(
		function(e) {
			e.preventDefault();
			$('.screensaver .scrHelpDet').hide();
			$('.screensaver p').show();
		}
	);
	$('a.emoHelp').click(
		function(e) {
			e.preventDefault();
			$('.emoticon p').hide();
			$('.emoticon ul').hide();
			$('.emoticon .emoHelpDet').show();
		}
	);
	$('a.emoHelpClose').click(
		function(e) {
			e.preventDefault();
			$('.emoticon .emoHelpDet').hide();
			$('.emoticon p').show();
			$('.emoticon ul').show();
		}
	);
	$('a.rngHelp').click(
		function(e) {
			e.preventDefault();
			$('.suonerie p').hide();
			$('.suonerie table').hide();
			$('.suonerie a.rngHelp').hide();
			$('.suonerie .rngHelpDet').show();
		}
	);
	$('a.rngHelpClose').click(
		function(e) {
			e.preventDefault();
			$('.suonerie .rngHelpDet').hide();
			$('.suonerie p').show();
			$('.suonerie table').show();
			$('.suonerie a.rngHelp').show();
		}
	);
}
