slider = new Array();
big = new Array();
big["width"] = 300;   /* 320 */
big["height"] = 425;  /* 320 */
big["height2"] = big["height"] + 100; /* +28 */

small = new Array();
small["width"] = 105;   /* 160 */
small["height"] = 150;  /* 160 */
small["height2"] = small["height"] + 100; /* +28 */

font = new Array();
font["big"] = 16;
font["small"] = 12;

timer = -1;
numitems = 11;

function equilibrate() {
	$jQ('#slider ul li').each(function(i) {                                                
		d = Math.abs(actual - i);
		var a = $jQ(this);
		var img = a.children('a').children('img');
		var ttl = a.children('.imtitle');
                if (actual == 1){
                    $jQ('#butleft').hide();
                }else {
                    if (actual == numitems){
                      $jQ('#butright').hide();
                    }else{
                      $jQ('#butleft').show();
                      $jQ('#butright').show();
                  }
                }
                
		switch(d) {
			case 0:
				img.animate({width: big["width"], height: big["height"], opacity: 1});
				a.animate({width: big["width"], height: big["height2"]}, 
                                  function () { ttl.css({'font-size': font["big"]+'px', color: '#D9001B'}); });
				showtext(a.attr('id').substr(5));                                
			break;
			case 1:
				ttl.css({'font-size': font["small"]+'px', color: '#6C6D71'});
				//if (a.css('display') == 'none') {
				if (slider[i] == 1) {
					slider[i] = 0;
					a.css({display: 'inline', width: '0px', height: '0px'});
					img.css({width: '0px', height: '0px', opacity: 0});
					ttl.css({opacity: '0', color: '#6C6D71'});
				}
				img.animate({width: small["width"], height: small["height"], opacity: 0.25}, function () { ttl.fadeTo(2000, 1);  }); 
                                /* [Julio] Ponía ttl.fadeTo(5000, 1); */
                                
				a.animate({width: small["width"], height: small["height2"], color: '#6C6D71'});
			break;
			default:
				if (slider[i] != 1) {
					slider[i] = 1;
                                        a.stop(true, true);
					a.hide('fast');
				}
			break;
		}
	});
	directlink();
	rotate();
}
function actual_change(diff) {
	if (actual + diff > 0 && actual + diff < max) {
		actual = actual + diff;
		return true;
	}
        
	return false;
};
function directlink() {
	$jQ('#directlink').attr('href', location.href.replace(location.search, '') + '?show=' + actual);
}
$jQ(document).ready(function(){
	var requete = location.search.substring(1);
	var tab_paires = requete.split("&");
	var tab_elts = new Array();
	for (var i = 0; i < tab_paires.length; i++) {
		temp = tab_paires[i].split("=");
		tab_elts[temp[0]] = unescape(temp[1]);
	}

	$jQ('#slider ul').css('overflow', 'hidden');
	$jQ('#slider ul').prepend('<li><a><img src="/images/empresa/botones/vide.png" alt="" /></a></li>');
	max = $jQ('#slider ul li').size();

	i = (tab_elts["show"]) ? parseInt(tab_elts["show"]) : 0;
	actual = (!isNaN(i) && i > 0 && i < max) ? i : 1;

	$jQ('#slider ul li .text').appendTo("#texts");
	$jQ('#slider ul li').each( function(i) {
                
		d = Math.abs(actual - i);
		var a = $jQ(this);
		var img = a.children('a').children('img');
		var ttl = a.children('.imtitle');
		switch(d) {
			case 0:
				ttl.css({'font-size': font["big"]+'px', color: '#D9001B'});
				img.css({width: big["width"]+'px', height: big["height"]+'px', opacity: 1});
				a.css({height: big["height2"]+'px'});
				showtext(a.attr('id').substr(5));
                                $jQ('#butleft').hide();
			break;
			case 1:
				img.css({ width: small["width"]+'px',
                                          height: small["height"]+'px', 
                                          opacity: .25
                                      });
				a.css({width: small["width"]+'px', height: small["height2"]+'px', color: '#6C6D71'});
			break;
			default:
				a.css({display: 'none', width: '0px', height: '0px', opacity: 1});
			break;
		}
	});
	$jQ('#butleft').click( function () {
		if (actual_change(-1))
			equilibrate();
		timer_stop();
		return false;
	});
	$jQ('#butright').click( function () {
		if (actual_change(+1))
                    equilibrate();

                // this.css({padding: '15px 15px 15px 15px'});                
		timer_stop();
		return false;
	});

	$jQ('#playpause').click( function () {
		var a = $jQ('#playpause').children('img').attr('src');
		a = a.split("/");
		if (a[a.length - 1] == 'pause.png') {
			timer_stop();
		}
		else {
			next();
			timer = window.setInterval("next()", 4000);
			$jQ('#playpause').children('img').attr('src', '/images/empresa/botones/pause.png');
		}

		return false;
	});
	$jQ('#slider ul li a').click( function () {
		timer_stop();
	});
        
        //  Con esto permito que empiece la animación: timer = window.setInterval("next()", 3000);
        
        
        
/*
	$jQ('#slider').mousewheel(function(objEvent, intDelta){
		if (intDelta > 0) {
			if (actual_change(-1))
				equilibrate();
		}
		else if (intDelta < 0) {
			if (actual_change(+1))
				equilibrate();
		}
		return false;
	});
*/
	//timer = window.setInterval("next()", 3000);
});
function timer_stop() {
	var a = $jQ('#playpause').children('img').attr('src');
	if (a != null){
            a = a.split("/");
            if (a[a.length - 1] == 'pause.png') {
                    $jQ('#playpause').children('img').attr('src', '/images/empresa/botones/playred.png'); /* ou play.png */
                    window.clearInterval(timer);
            }
        }
}
function next() {
	if (actual_change(+1))
		equilibrate();
}
function rotate() {
	//alert(max + ' ' + actual + ' ' + (max - actual));
/*
	if (actual < (max / 2)) {
		var a = $jQ('#slider ul li').eq(max-1).remove();
		$jQ('#slider ul').prepend(a);
		actual = actual + 1;
	}
*/
/*
		if (diff < 0) {
			var a = $jQ('#slider ul li:last').remove();
			$jQ('#slider ul').prepend(a);
		}
		else {
			var a = $jQ('#slider ul li:first').remove();
			$jQ('#slider ul').append(a);
		}
*/
}

/**
 * Función en la que se hace un slow del texto relacionado con la imagen.
 */
function showtext(id) {
        $jQ('#texts .text').stop(true, true); // [Julio] Modificado para que el texto no se vaya
	$jQ('#texts .text').each( function (i) {
		if ($jQ(this).css('display') != 'none')
			//$jQ(this).fadeOut('fast');
			$jQ(this).hide();
	});
	$jQ('#texts #text'+id).slideDown('slow');
}
