
;(function( $ ){

	var $serialScroll = $.serialScroll = function( settings ){
		//$.scrollTo.window().serialScroll( settings );
	};

	$serialScroll.defaults = {//the defaults are public and can be overriden.
		duration:1000, //how long to animate.
		axis:'x', //which of top and left should be scrolled
		event:'click', //on which event to react.
		start:0, //first element (zero-based index)
		step:1, //how many elements to scroll on each action
		lock:true,//ignore events if already animating
		cycle:true, //cycle endlessly ( constant velocity )
		constant:true //use contant speed ?

	};

	$.fn.serialScroll = function( settings ){
		settings = $.extend( {}, $serialScroll.defaults, settings );
		var event = settings.event, //this one is just to get shorter code when compressed
			step = settings.step, // idem
			lazy = settings.lazy;//idem

		return this.each(function(){
			var 
				context = settings.target ? this : document, //if a target is specified, then everything's relative to 'this'.
				$pane = $(settings.target || this, context),//the element to be scrolled (will carry all the events)
				pane = $pane[0], //will be reused, save it into a variable
				items = settings.items, //will hold a lazy list of elements
				active = settings.start, //active index
				auto = settings.interval, //boolean, do auto or not
				nav = settings.navigation, //save it now to make the code shorter
				timer; //holds the interval id

			if( !lazy )//if not lazy, go get the items now
				items = getItems();

			if( settings.force )
				jump( {}, active );//generate an initial call

			// Button binding, optionall
			$(settings.prev||[], context).bind( event, -step, move );
			$(settings.next||[], context).bind( event, step, move );

			// Custom events bound to the container
			if( !pane.ssbound )//don't bind more than once
				$pane
					.bind('prev.serialScroll', -step, move ) //you can trigger with just 'prev'
					.bind('next.serialScroll', step, move ) //for example: $(container).trigger('next');
					.bind('goto.serialScroll', jump ); //for example: $(container).trigger('goto', [4] );
			if( auto )
				$pane
					.bind('start.serialScroll', function(e){
						if( !auto ){
							clear();
							auto = true;
							next();
						}
					 })
					.bind('stop.serialScroll', function(){//stop a current animation
						clear();
						auto = false;
					});
			$pane.bind('notify.serialScroll', function(e, elem){//let serialScroll know that the index changed externally
				var i = index(elem);
				if( i > -1 )
					active = i;
			});
			pane.ssbound = true;//avoid many bindings

			if( settings.jump )//can't use jump if using lazy items and a non-bubbling event
				(lazy ? $pane : getItems()).bind( event, function( e ){
					jump( e, index(e.target) );
				});

			if( nav )
				nav = $(nav, context).bind(event, function( e ){
					e.data = Math.round(getItems().length / nav.length) * nav.index(this);
					jump( e, this );
				});

			function move( e ){
				e.data += active;
				jump( e, this );
			};
			function jump( e, button ){
			
				if( !isNaN(button) ){//initial or special call from the outside $(container).trigger('goto',[index]);
					e.data = button;
					button = pane;
				}

				var
					pos = e.data, n,
					real = e.type, //is a real event triggering ?
					$items = settings.exclude ? getItems().slice(0,-settings.exclude) : getItems(),//handle a possible exclude
					limit = $items.length,
					elem = $items[pos],
					duration = settings.duration;

				if( real )//real event object
					e.preventDefault();

				if( auto ){
					clear();//clear any possible automatic scrolling.
					timer = setTimeout( next, settings.interval ); 
				}

				if( !elem ){ //exceeded the limits
					n = pos < 0 ? 0 : limit - 1;
					if( active != n )//we exceeded for the first time
						pos = n;
					else if( !settings.cycle )//this is a bad case
						return;
					else
						pos = limit - n - 1;//invert, go to the other side
					elem = $items[pos];
				}
/*
				if( !elem || real && active == pos || //could happen, save some CPU cycles in vain
					settings.lock && $pane.is(':animated') || //no animations while busy
					real && settings.onBefore && //callback returns false ?
					settings.onBefore.call(button, e, elem, $pane, getItems(), pos) === false ) return;
*/
				if( settings.stop )
					$pane.queue('fx',[]).stop();//remove all its animations

				if( settings.constant )
					duration = Math.abs(duration/step * (active - pos ));//keep constant velocity


				if (xivato==1) return;
				
				$("#secciones-menu li .title-news").css("display", "none");
				$("#secciones-items li:eq(" + xivpos + ") img").fadeTo(500,0, function () {
					$("#secciones-items li").css("display", "none");
					$("#secciones-menu li .title-news").css("display", "none");
					$(elem).css("display", "inline");
					$(elem).find("img").fadeTo(400,1,function () {
						$("#secciones-menu li .title-news").css("display", "none");
						$("#secciones-menu li a:eq(" + pos + ") .title-news").css("display", "inline");
						
					});
				});

				//$("#secciones-items li img").css("opacity", "0");
				//$("#secciones-items li:eq(" + pos + ") img").fadeTo(1500,1);
				//$("#secciones-items li img").css("display", "none");
		
		
				//$("#secciones #secciones-menu li a:eq(" + xivpos + ")").css("color", "red");
				//$("#secciones #secciones-menu li a:eq(" + pos + ") .title-news").animate({width: 'toggle'});


				$pane.trigger('notify.serialScroll',[pos]);
				xivpos = pos;


				/*
				$pane
					.scrollTo( elem, duration, settings )//do scroll
					.trigger('notify.serialScroll',[pos]);//in case serialScroll was called on this elem more than once.
				*/
			};
			function next(){//I'll use the namespace to avoid conflicts
				$pane.trigger('next.serialScroll');
			};
			function clear(){
				clearTimeout(timer);
			};
			function getItems(){
				return $( items, pane );
			};
			function index( elem ){
				if( !isNaN(elem) ) return elem;//number
				var $items = getItems(), i;
				while(( i = $items.index(elem)) == -1 && elem != pane )//see if it matches or one of its ancestors
					elem = elem.parentNode;
				return i;
			};
		});
	};

})( jQuery );

var xivato=0;
var xivpos=0;
var xivato2=0;

jQuery(function( $ ){

	$(".wp-items-box").bind("mouseover", function(e) { 
		xivato=1;
	});

	$(".wp-items-box").bind("mouseout", function(e) { 
		xivato=0;
	});
	
	
	$("#secciones-menu li a").bind("mouseover", function(e) { 
		if (this['rel'] == xivpos) return;
		$("#secciones-items").trigger('goto', [this['rel']]);
	});

	$('#secciones').serialScroll({
		target:'#secciones-items',
		items:'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		//navigation:'#secciones-menu li a',
		
		//axis:'xy',// The default is 'y' scroll on both ways
		//duration:500,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		interval:4500 // It's the number of milliseconds to automatically go to the next
		//lock:false, // Ignore events if already animating (true by default)	

	});
	
	/* Amaga els continguts que volem expandir/contraure */
	jQuery('div.expand_content').css({'display': 'none'});
	
	/* CSS momentani ... o no .... */
	/*jQuery('div.expand_container a.menuExt').css({'color': 'blue'});*/
	
	jQuery('div.expand_container a.menuCon').css({'text-decoration': 'underline'});
	
	/* Expandeix o contrau, segons com estigui en aquell moment, el contingut del item clicat */
	jQuery("div.expand_container a.menuExt").bind("click", function(e) { 
		if (this.className.indexOf("menuCom") == -1){
			jQuery(this)
				.addClass('menuCom')
				.css({'text-decoration': 'underline'})
				.parent().parent().find('div.expand_content').slideDown("slow");
		}
		else{
			jQuery(this)
				.removeClass('menuCom')
				.css({'text-decoration': 'none'})
				.parent().parent().find('div.expand_content').slideUp("slow");
		}
		return false;
	});
	
	/* Contrau el div requerit */
	jQuery("div.expand_content a.menuCon").bind("click", function(e) {
		jQuery(this).parent().parent()
			.slideUp("slow")
			.parent().find('a.menuExt')
				.removeClass('menuCom')
				.css({'text-decoration': 'none'});

		return false;
	});


	jQuery('#box-hover').hover(function(){
		jQuery(this).find('img.hover').animate({left:'360px'},{queue:false,duration:500});
	}, function(){
		jQuery(this).find('img.hover').animate({left:'0'},{queue:false,duration:500});
	});


	jQuery(".show_revista").bind("click", function(e) { 
		jQuery(this).find('img').fadeTo(700,0,function () {
			jQuery(".iframe-revista").css({'display': 'block'})
			jQuery(this).css({'display': 'none'})
		});
		
		return false;
	});	
	
	jQuery('#nav-menu a').attr('title', '');
	jQuery('#nav-menu img').attr('title', '');
	jQuery('#nav-menu-dos a').attr('title', '');
	jQuery('#nav-menu-dos img').attr('title', '');
	
});
