//init

// Include javascript libraries
includeLibraries();

// Fix bugs
backgroundCache();

// Update links from popout
updateLinksPopout();

// Replace HTML components
replaceComponents();

// Combobox
initCombobox();

// Scrolling
initScrolling(document);

// Wheel
initWheel(document);

// Photos galleries
initPhotosGalleries();

// Drag modules
initDragModule();

// Tab by URL
initTabs();

// Tab by URL
initAlteranteLists();

// Hour sync
hourWrite();
var intHourRefresh = setInterval("hourWrite()", 15000);

/* Combobox */
$(".ctn-combo .ctn-combovalue").click(function() {
	showHideCombo($(this).parent(),1);
});
timerCombo = null;
$(".ctn-combo").hover(
	function() {
		clearTimeout(timerCombo);
	},
	function () {
		timerCombo = setTimeout("showHideCombo($('.ctn-combo').get("+$(".ctn-combo").index(this)+"),0)",1500);
	}
);
$(".ctn-combo .ctn-listvalues a").click(function() {
	selectComboValue(this);
	return false;
});
/* // Combobox */

// Clear athletes search field
$(".fld-searchfield").blur(function() {
	//clearField(this,"Buscar pelo nome",0);
	//clearField(this,"Búsqueda por nombre",0);
	//clearField(this,"Search for name",0);
});
$(".fld-searchfield").focus(function() {
	clearField(this,"Buscar pelo nome",1);
	clearField(this,"Búsqueda por nombre",1);
	clearField(this,"Search for name",1);
});

// Link print
$(".itm-print a").click(function() {
	window.print();
	return false;
});

// Expand collapse
$(".lnk-collapse").click(function() {
	expandCollapse(this);
	return false;
});

// Open layer
$(".lnk-showlayer").click(function() {
	showHideLayer(this,2);
	return false;
});

// Over layer
$(".lnk-layerOver").hover(
	function() {
		showHideOver(this,1);
	} ,
	function() {
		showHideOver(this,0);
	}
);

// Close layer
$(".ctn-layer .btn-close,.ctn-layerform .btn-close").click(function() {
	showHideLayer(this,0);
	return false;	
});

// Header change country
$(".grp-countries a:first").click(function() {
	$(this).parent().find("ul").slideToggle(200);
	return false;
});

/* Toolbox */
$(".ctn-toolbox").hover(
	function() {
		toggleToolbox(1);
	},
	function() {
		toggleToolbox(0);
	}
);
$(".ctn-toolbox a").click(function() {
	toggleSubToolbox(this,1);
	return false;
});
$(".ctn-subtoolbox").hover(
	function() {
		toggleSubToolbox(null,2);
	},
	function() {
		if ($(this).attr("class").indexOf("ctn-share") > -1) {
			toggleSubToolbox(this,0);
		}
	}
);
$(".lnk-closetoolbox").click(function() {
	toggleSubToolbox(null,0);
	return false;
});
$(".btn-toolboxcancel").click(function() {
	toggleSubToolbox(null,0);
});
/* // Toolbox */

// Hide elements on clickout
clickout = false;
$(".clickout, .ctn-combo").hover(
	function(e) {
		clickout = true;
	},
	function(e) {
		clickout = false;
	}
);
$(document).click(function(e) {
	clickOut(e);
});
$(document).click(function(e) {
	clickOut(e);
});
$("a").click(function(e) {
	clickOut(e);
});

// Popout
$(".lnk-popout").click(function() {
	openPopup(window.location.href.replace("#","")+"?popup=true",name,940,600,1);
	return false;
});

// Font resize
$(".lnk-fontresize").click(function() {
	fontResize($(this).attr("href").replace("#",""));
	return false;
});

// Tabs
$(".lst-tabs a").click(function() {
	changeTab(this);
	playPauseGallery(null,0);
	$(".clickout:visible").hide();
	$(".ctn-toolbox a").removeClass("selected");
	if (!$(this).hasClass("anchor"))
		return false;
});

/* Drag modules */
$(".lst-dragmodules").find("input:checkbox").change(function() {
	if ($(this).attr("checked")) {
		toggleModule($("."+$(this).val()),1);
	} else {
		toggleModule($("."+$(this).val()),0);
	}
});
$(".ctn-barvolume a").click(function() {
	return false;
});
$(".lst-dragmodules .chk-replace").hover(
	function() {
		elementOver("over",this,1);
	},
	function() {
		elementOver("over",this, 0);
	}
);
$(".lnk-exposeall").click(function() {
	exposeAllModules();
	return false;
});
$(".lnk-collapsemodule").click(function() {
	toggleModule($(this).parents(".ctn-dragmodule"),2);
	return false;
});
$(".lnk-expandmodule").click(function() {
	toggleModule($(this).parents(".ctn-dragmodule"),3);
	return false;
});
$(".lnk-closemodule").click(function() {
	toggleModule($(this).parents(".ctn-dragmodule"),0);
	return false;
});
$(".lnk-dragmodule").click(function() {
	return false;
});
$(".ctn-dragmodule.highlights li").hover(
	function() {
		elementOver("over",this,1);
	},
	function() {
		elementOver("over",this, 0);
	}
);
$(".ctn-dragmodule.highlights li").click(function() {
	toggleHighlight(this, 1);
	return false;
});
$(".ctn-highlight .lnk-backtolive").click(function() {
	toggleHighlight(this, 0);
	return false;
});
/* // Drag modules */

/* Expand and collapse table */
$(".tab-collapse .expand-collapse").each(function() {
	$(this).find("tr:first").hover(
		function() {
			elementOver("over",this,1);
		},
		function() {
			elementOver("over",this,0);
		}
	);
	
	$(this).find("tr:first").click(function() {
		expandCollapseTable(this);
	});
});
/* // Expand and collapse table */

// Schedule in athletes
$(".tab-scheduleathlete .row-title").hover(
	function() {
		elementOver("over",this,1);
	},
	function() {
		elementOver("over",this,0);
	}
);
$(".tab-scheduleathlete .row-title").click(function() {
	window.location = $(this).find("a").attr("href");
});

/* Poll */
	// Vote
	$(".ajax-poll").submit(function() {
		votePoll($(this));
		return false;
	});
/* // Poll */

// Back to full article
$(".lnk-fullarticle").click(function() {
	$("a[rel='noticia']").click();
});

/* Share */
// Delicious
$(".ctn-share .itm-delicious").click(function() {
	window.open("http://del.icio.us/post?title="+document.title+"&url="+window.location.href);
	return false;
});

// Facebook
$(".ctn-share .itm-facebook").click(function() {
	window.open("http://www.facebook.com/share.php?u="+window.location.href);
	return false;
});

// Technorati
$(".ctn-share .itm-technorati").click(function() {
	window.open("http://www.technorati.com/search/"+window.location.href);
	return false;
});

// Delicious
$(".ctn-share .itm-yahoo").click(function() {
	window.open("http://myweb2.search.yahoo.com/myresults/bookmarklet?t="+document.title+"&u="+window.location.href);
	return false;
});

// Meneame
$(".ctn-share .itm-meneame").click(function() {
	window.open("http://meneame.net/submit.php?url="+window.location.href);
	return false;
});

// Fresqui
$(".ctn-share .itm-fresqui").click(function() {
	window.open("http://tec.fresqui.com/post?title="+document.title+"&url="+window.location.href);
	return false;
});
/* // Share */

/* Photos gallery */
$(".ctn-scrollingcontent a").click(function() {
	openPhoto($(this));
	playPauseGallery(null,0);
	return false;
});
// Back
$(".ctn-photosgallery .lnk-back:first").click(function() {
	obj = $(this).parents(".ctn-scrollingphoto").parent().find(".ctn-scrollingcontent");
	if (obj.find(".selected").prev().size() <= 0) {
		obj.find("li:last").find("a:first").click();
	} else {
		obj.find(".selected").prev().find("a:first").click();
	}
	playPauseGallery(null,0);
	return false;
});
// Next
$(".ctn-photosgallery .lnk-next:first").click(function() {
	obj = $(this).parents(".ctn-scrollingphoto").parent().find(".ctn-scrollingcontent");
	if (obj.find(".selected").next().size() <= 0) {
		obj.find("li:first").find("a:first").click();
	} else {
		obj.find(".selected").next().find("a:first").click();
	}
	playPauseGallery(null,0);
	return false;
});
// First
$(".ctn-photosgallery .ctn-gallerycontrols .lnk-back:first").click(function() {
	obj = $(this).parents(".ctn-scrollingphoto").parent().find(".ctn-scrollingcontent");
	obj.find("li:first").find("a:first").click();
	playPauseGallery(null,0);
	return false;
});
// Last
$(".ctn-photosgallery .ctn-gallerycontrols .lnk-next:first").click(function() {
	obj = $(this).parents(".ctn-scrollingphoto").parent().find(".ctn-scrollingcontent");
	obj.find("li:last").find("a:first").click();
	playPauseGallery(null,0);
	return false;
});
// Zoom
$(".ctn-photosgallery .lnk-zoomphoto").click(function() {
	playPauseGallery(null,0);
});
$(".ctn-photosgallery .lnk-play").click(function() {
	playPauseGallery($(this).parents(".ctn-scrollingphoto").parent(),1);
	return false;
});
$(".ctn-photosgallery .lnk-pause").click(function() {
	playPauseGallery($(this).parents(".ctn-scrollingphoto").parent(),0);
	return false;
});
/* // Photos gallery */

/* Horizontal scrolling */
$(".horizontalrolling .back").hover(
	function() {
		horizontalScrolling($(this).parents(".horizontalrolling"),0,1);
	},
	function() {
		horizontalScrolling($(this).parents(".horizontalrolling"),0,0);
	}
);
$(".horizontalrolling .forward").hover(
	function() {
		horizontalScrolling($(this).parents(".horizontalrolling"),1,1);
	},
	function() {
		horizontalScrolling($(this).parents(".horizontalrolling"),1,0);
	}
);

$(".horizontalrolling .forward").click(function() {
	return false;
});
$(".horizontalrolling .back").click(function() {
	return false;
});
/* // Horizontal scrolling */

/* Only number */
$(".numeric").keyup(function() {
	$(this).val($(this).val().replace(/\D/g,""));
});
$(".numeric").change(function() {
	$(this).val($(this).val().replace(/\D/g,""));
});