$.ui.dialog.prototype.options.resizable = false;
$.ui.dialog.prototype.options.hide= 'fold';

var tabs =$('#tabs');
var accordion = $('#accordion');
var banner = $('#banner');
var zoomDialog = $('#zoom_dialog');
var videoDialog = $('#video_dialog');
var progressBar = $('#progressbar');
var gameContainer=$('#game_container');
var last_banner_is_tab = false;
var accordionCache = [];
var snapshots = [];

function loadCachedSection(game_type)
{
	if(!accordionCache[game_type])
		$.get('ajax/games_list/'+game_type,{},function(data){accordionCache[game_type]=data;loadCachedSection(game_type)});
	else
		gameContainer.html(accordionCache[game_type]).css('backgroundImage','');
}

function setBannerFromTab(index)
{
	last_banner_is_tab = true;

	//Set banner
	banner.empty().css({'backgroundColor':'','backgroundImage':'','width':'','margin':''}); //To-Do : Alan lo ha hecho pensando en ancho fijo :(
	options = {mouseport: $('#container')};
	switch(index)
	{
		default:
		case 0:
			banner.css('textAlign','right').html(embed_flash(images_url+'banner/productos.swf',1100,170));
		break;

		case 1:
			banner.css('backgroundColor','white').html('<div id="about_video"></div>');
			flowplayer('about_video', video_url+'flowplayer-3.1.3.swf',{
				clip:{
					url:video_url+'degestec.flv',
					scaling: 'orig',
					onFinish: function(){
						this.hide();
						banner.css('background','white url('+images_url+'banner/empresa.png) no-repeat 90% 50%').animate({backgroundColor: 'black' },3000);
						}
				},
				plugins:{controls: null},
				canvas: {backgroundColor: '#FFFFFF'}
			});
		break;

		case 2:
			banner.css('textAlign','right').html(embed_flash(images_url+'banner/donde.swf',1100,170));
		break;

		case 3:
			banner.css('textAlign','right').html(embed_flash(images_url+'banner/negocios.swf',1100,170));
		break;

		case 4:
			banner.css('textAlign','right').html(embed_flash(images_url+'banner/contacto.swf',1100,170));
		break;

		case 5:
			banner.css('textAlign','right').html(embed_flash(images_url+'banner/login.swf',1100,170));
		break;
	}
}

function embed_flash(file,width,height)
{
	return '<object width="'+width+'" height="'+height+'" style="postion:absolute;right:0"><param name="movie" value="'+file+'"><embed src="'+file+'" width="'+width+'" height="'+height+'"></embed></object>'
}

function  doParallaxDiv(divwidth,divheight,img) {return '<div style=width:'+divwidth+'px;height:'+divheight+'px><img src='+images_url+img+' /></div>';}

function doGame(id)
{
	last_banner_is_tab = false;
	banner.css('backgroundImage','');
	$.getJSON('ajax/game/'+id,{},function (data){
		gameContainer.css('backgroundImage','url('+images_url+'games/'+data.background+')');
		if(data.banner.length>1) //Parallax
		{
			var parallax = '';
			banner.css({'width':'1100px','margin':'auto'}); //To-Do : Alan lo ha hecho pensando en ancho fijo :(
			$.each(data.banner, function(i,item){
				parallax = parallax +  doParallaxDiv(data.bannerx[i],data.bannery[i],'games/'+item);
			});
			banner.html(parallax).jparallax({mouseport: $('#container')});
		}
		else
		{
			//To-do : static image
		}
		gameContainer.html(data.html);
		snapshots = data.snapshots;
	});
}

var current_snap = -1;
function cicle_snapshot()
{
	current_snap = current_snap == (snapshots.length-1) ? 0 : current_snap+1;
	$('img',zoomDialog).attr('src',images_url+'games/large/'+snapshots[current_snap]);
}
function zoom_snapshot(index)
{
	current_snap = index;
	$('img',zoomDialog).attr('src',images_url+'games/large/'+snapshots[index]);
	zoomDialog.dialog('open');
}

//http://flowplayer.org/documentation/configuration/player.html
function playVideo(title,video){
	videoDialog.dialog('option', 'title', title).dialog('open');
	flowplayer("video_container", video_url+"flowplayer-3.1.3.swf",{
		clip:{
			url:video_url+video,
			scaling: 'orig',
			onFinish: function(){
				this.getPlugin("play").hide();
			}
		}
	})
}


$(document).ready(function() {
	progressBar.progressbar();
	zoomDialog.dialog({modal:true,autoOpen:false,width:700,height:580});$('a',zoomDialog).button().bind('click',function(){zoomDialog.dialog('close')});
	videoDialog.dialog({autoOpen: false,modal:true,width:512,height:443});
	tabslength = tabs.tabs({cache:true}).bind('tabsshow', function(event, ui){setBannerFromTab(ui.index)}).tabs('length');
	accordion.accordion({collapsible:true,autoHeight:false,changestart:function(){setBannerFromTab(0)}});
	$(window).resize(function(){if(last_banner_is_tab) setBannerFromTab(tabs.tabs('option', 'selected'))});
	setBannerFromTab(0);
	$("#radio_container").buttonset().css('fontSize','80%');

	$("#remote_login_form").submit(function() {
		var success = false;
		$.ajax({
			async:false,
			type: 'POST',
			url: 'ajax/check_remote_credentials',
			dataType: 'json',
			data: {
				login:$('#login').val(),
				passw:$('#passw').val()
			},
			success: function(data){success = data.success}
		});
		if(! success)
			$("#login_error_message").hide().fadeIn(1000);
		return success;
	});

	//Logo fadeout visual feedback
	$('div span',accordion).hover(
		function () {$("#"+$(this).attr("class")).css({'borderWidth':'1px','margin':'4px'}).siblings('img.logo').stop().fadeTo(190,0.33)},
		function () {$("#"+$(this).attr("class")).css({'borderWidth':'0px','margin':'5px'}).siblings('img.logo').stop().fadeTo(200,1)}
	);

	$(document).bind("contextmenu",function(){return false;});
});
