var timeout=20;

function str_replace(string, org, dest) {
    return string.split(org).join(dest);
}

$(document).ready(function() {
	init();
});


var draggablesPosition;
function enableContentBoxDraggable(){
	var positionsJSON = $.cookie("draggablesPosition");
	if (positionsJSON){
		draggablesPosition = $.evalJSON(positionsJSON);
		$('.content_box').each(function(){
			var box = $(this);
			if (draggablesPosition[box.attr("id")]){
				box.offset(draggablesPosition[box.attr("id")]);
			}
		});
	}
	
	$('.content_box').draggable({
		stop: function(event, ui) {
			var box = $(this);
			if (!draggablesPosition){
				draggablesPosition = {};
			}
			draggablesPosition[box.attr("id")]=box.offset();
			$.cookie("draggablesPosition",$.toJSON(draggablesPosition))
    	}
	});		

}

function init(){
	enableContentBoxDraggable();
	
	
	$('#menu1 a[id^="statistics_"]').wowwindow({
	    draggable: true
	});
	
	$('#news a').wowwindow({
	    draggable: true
	});
	
	$('#menu1 a[id^="match_"], #menu1 a[id="all"]').click(function(){
		match_id = $(this).attr("href").split("_ref")[0]+"_div";
		if (match_id == "#match_all_div"){
			showAllMatchs();
		}else{
			hideAllMatchs();
			$(match_id).show();
		}
	});

	//fix problem with ie
	var	movie_container = $('#movie_container');

	$('#menu1 a[id^="video_"]').click(function(event){
		clearTimeout(reloadTimer);
		event.preventDefault();
		var id = $(this).attr("id").split("video_")[1];
		search = $(this).attr("title");
		movie_container.text("");
		movie_container.flash({
	        src: '../flash/busqueda.swf',
	        expressInstall: true,
	        width: 320,
          	height: 180,
          	flashvars: { search: search }	        
	    });
		$('#movie_div').show();
		
	});
	
	autoShowMatch();
	setNewReloadTimmer();
	setNewJumpTimmer();
}

function showAllMatchs(){
	$("#general .match").show();
}

function hideAllMatchs(){
	$("#general .match").hide();
}

function autoShowMatch(){
	idMatch=document.location.href.split("#")[1];
	if (idMatch!=undefined&&idMatch!=""){
		idMatch=idMatch.split("_")[1]
		if (idMatch!="all"){
			hideAllMatchs();
			match_id = "#match_"+idMatch+"_div";
			$(match_id).show();
		}
	}
}

function updateMain(){
	$("#main").load($('#refreshForm').attr("action"),
		function(){
			init();
		}
	);
}

var timeoutTimer;
var reloadTimer;

function setNewJumpTimmer(){
	if (timeoutTimer!=null)	clearTimeout(timeoutTimer);
	timeoutTimer=setTimeout("updateTimers();",1000);
}


function setNewReloadTimmer(){
	if (reloadTimer!=null)	clearTimeout(reloadTimer);
	reloadTimer=setTimeout("updateMain();",timeout*1000);
}

function updateTimers(){
	clearTimeout(timeoutTimer);
	$('#general .seconds[id^="match_timer"]').each(
			function(){
				var seconds = parseInt($(this).text().split('"')[0],10)+1;
				if (seconds>59){
					var minutes_id = $(this).attr("id").split('seconds')[0]+"minutes";
					var minutes_label = $("#"+minutes_id); 
					var minutes = parseInt(minutes_label.text().split("'")[0],10);
					minutes++;
					minutes_label.text(minutes+"'");
					seconds = 0;
				}
				$(this).text(seconds+'"');				
			}
	);
	setNewJumpTimmer();
}

