var homeDelay = 5000;
/*SHADOWBOX ------------------------------------------------------------------*/
var shadowbox_options = {
   		handleOversize:		'resize',
        displayNav:         true,
        autoplayMovies:     true,
		initialWidth:		50,
		initialHeight:		50,
		resizeDuration:		0.40,
		fadeDuration:		0.40,
		overlayOpacity:		0.6,
		overlayColor:		'#000000',
		counterType:		'default'
    };
$(function() { Shadowbox.init(shadowbox_options); });

/*GENERAL --------------------------------------------------------------------*/
function initGeneral() {
	//cierra layers al hacer click sobre el body
	$('body').click(function(){
	    $('div.layer').fadeOut();
	});
	//funciones del subtoolbar
	if ($('div.subtoolbar').length) {
	    $('div.subtoolbar').parent().css({ height: '50px' }).find('h2').css({ paddingTop: '25px' });
		$('div.subtoolbar a').hover(function() {
			$(this).find('img.off').hide();
			$(this).find('img.on').show();
		}, function() {
			$(this).find('img.off').show();
			$(this).find('img.on').hide();
		});
	}
	//fix para links del menu con IE
	if($.browser.msie) { fixMenu(); }
	//trigger de formularios
	$('a.form_trigger').click(function(){
	    $(this).parents("form").submit();
	});
	//link condiciones legales en layer
	$('a.condiciones_legales').attr({ 'href': _link_condiciones_legales, 'title': 'Condiciones legales', 'rel': 'shadowbox;width=600;height=400' }).click(function(){
	    
	});
}
/*PORTADA --------------------------------------------------------------------*/
function initHomeSlider() {
	var next = '#controller_' + $('table.controller td.active').text();
	if ( $(next).length ) $(next).click();
	else $('#controller_0').click();
	setTimeout(initHomeSlider,homeDelay);
}
function initPortada() {
	$('#header div.marcador').html('<img src="###RUTA###content/images/_tmp/marcador.png" />');
	//home slider
	$('div.slider').each(function(){
	    var s = $(this);
	    if (!$('div.item', s).length) return false;
		var w = $('div.item', s).eq(0).width();
	    $('table.controller td.chgTrigger', s).click(function(){
	        $('table.controller td.active', s).removeClass('active');
	        $(this).addClass('active');
	        var i = parseInt($(this).text(), 10) - 1;
	        var p = parseInt((-1*i*w), 10);
			$('div.container', s).animate({ left: p });
		});
	});
	setTimeout(initHomeSlider,homeDelay);
}

/*MENU FIX FOR IE ------------------------------------------------------------*/
function fixMenu() {
	$('#menu a.menu').click(function(){
	    document.location.href = $(this).attr('href');
	    return false;
	});
}
/*SLIDERS --------------------------------------------------------------------*/
$.fn.customSlider = function(widthPage) {
	return this.each(function() {
		var slideshow = $(this);
		var box = slideshow.find('div.box');
		var elems = slideshow.find('td.page');
		var countPages = elems.length;
		var currentPage = -1;
		var navigator = $('#'+slideshow.attr('rel'));
		var navigatorItems = $('#'+slideshow.attr('rel')+' > a');
		
		function setPage(page) {
			box.animate({'left': -(widthPage)*(page-1)}, 750);
			currentPage = page*1;
		}

		$('#'+slideshow.attr('rel')+' > a').click(function() {
			var t = $(this);
			if (t.hasClass('disabled')) return false;
			page = this.getAttribute('rel');
			if (page=='prev' || page=='next') {
				page = (page=='prev') ? currentPage-1 : currentPage+1;
				t = $('#'+slideshow.attr('rel')+' > a[rel='+page+']');
			}
			if (page < 1 || page > countPages) return false;
		    navigatorItems.removeClass('current');
			t.addClass('current');
			return setPage(page);
			return false;
		});
		setPage(1);
	});
}