/****************************************
*
* Galbusera Tra custom js
*
* Filename: lib.js
* Author: Simone Lippolis
* Company: Now Available - http://nowavailable.it
*
****************************************/

function setupBaloon() {
	var $p = $('.ui-slider-handle').offset();
	var $t = $p.top + 19;
	var $l = $p.left - ($('#baloon').width() / 2) + ($('.ui-slider-handle').width() / 2);
	$('#baloon').css('top', $t + 'px').css('left', $l + "px").show();
}

function moveContent(val) {
	var $perc = Math.round((val * 100) / $('#scroller').width());
	var $totalWidth  = Math.round($('#stripe div.content').width() - $('body').width());
	var $newPosition = -1 * Math.round($totalWidth / 100 * $perc);
	var $speed = 1;
	if ($perc > 30) {
		$speed = 5;
	}
	if ($perc > 60) {
		$speed = 10;
	}
	if ($perc > 90) {
		$speed = 15;
	}
	$('#stripe div.content').animate(
		{ 
			marginLeft: $newPosition
		},
		$speed
	);
}

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();
	$(document).pngFix(); 
}

function openLink(url) {
	window.open(url, 'link');
}

function writeStats(page) {
	// alert (page);
	var pageTracker = _gat._getTracker("UA-10927533-1");
	pageTracker._trackPageview(page);
}

function setupNavigation() {
	$('#stripe div.element').hover(
		function() {
			var $id = $(this).attr('id');
			$(this).css('background-image','url(images/' + $id + '_h.png)');
			$(document).pngFix(); 
		},
		function () {
			var $id = $(this).attr('id');
			$(this).css('background-image','url(images/' + $id + '.png)');
			$(document).pngFix(); 
		}
	);
	$('#stripe div.element').click(
		function() {
			var $id = $(this).attr('id');
			var $selector = '#' + $id + '-detail';
			var $options = '';
			writeStats('/' + $id);
			if ($id == 'tratris') {
				$options = {
					bgiframe: true,
					dialogClass: $id,
					draggable: false,
					height: 565,
					hide: 'blind',
					modal: true,
					resizable: false,
					show: 'blind',
					width: 670
				};
				openModal($selector,$options);
			} else if ($id == 'cinequiz') {
				$options = {
					bgiframe: true,
					dialogClass: $id,
					draggable: false,
					height: 565,
					hide: 'blind',
					modal: true,
					resizable: false,
					show: 'blind',
					width: 670
				};
				openModal($selector,$options);	
			} else if ($id == 'tratizio') {
				writeStats('/ext/http://apps.facebook.com/tratizioecaio/galbusera.php');
				$url = 'http://apps.facebook.com/tratizioecaio/galbusera.php';
				openLink($url);
			} else {
				$options = {
					bgiframe: true,
					dialogClass: $id,
					draggable: false,
					height: 394,
					hide: 'blind',
					modal: true,
					resizable: false,
					show: 'blind',
					width: 670
				};
				openModal($selector,$options);
			}
		}
	);
}

function setupSlider() {
	$("#scroller").slider({
		value:0,
		min: 0,
		max: 920,
		step: 1,
		slide: function(event, ui) {
			moveContent(ui.value);
		}
	});
	$(document).bind(
		"mousemove",
		{},
		setupBaloon
	).bind(
		"mouseover",
		{},
		setupBaloon
	).bind(
		"mouseout",
		{},
		setupBaloon
	).bind(
		"mouseenter",
		{},
		setupBaloon
	).bind(
		"mouseleave",
		{},
		setupBaloon
	).bind(
		"click",
		{},
		setupBaloon
	).bind(
		"dblclick",
		{},
		setupBaloon
	).bind(
		"mousedown",
		{},
		setupBaloon
	).bind(
		"mouseup",
		{},
		setupBaloon
	);
	setupBaloon();
	$(document).pngFix();
}

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();
		}
	);
}

function preloadImages() {
	var pr = new Array();
	pr[0] = new Image();
	pr[0].src = './images/screensaver_h.png';
	pr[1] = new Image();
	pr[1].src = './images/emoticon_h.png';
	pr[2] = new Image();
	pr[2].src = './images/suonerie_h.png';
	pr[3] = new Image();
	pr[3].src = './images/tratizio_h.png';
	pr[4] = new Image();
	pr[4].src = './images/snack_h.png';
	pr[5] = new Image();
	pr[5].src = './images/tratris_h.png';
	pr[6] = new Image();
	pr[6].src = './images/cinequiz_h.png';
	pr[7] = new Image();
	pr[7].src = './images/screensaver.png';
	pr[8] = new Image();
	pr[8].src = './images/emoticon.png';
	pr[9] = new Image();
	pr[9].src = './images/suonerie.png';
	pr[10] = new Image();
	pr[10].src = './images/tratizio.png';
	pr[11] = new Image();
	pr[11].src = './images/snack.png';
	pr[12] = new Image();
	pr[12].src = './images/tratris.png';
	pr[13] = new Image();
	pr[13].src = './images/cinequiz.png';
}
