/***
 * Versi—n Javascript del widget de destacadas para la p‡gina principal
 * 3.0
 * 
 * Autor: Raœl Leal Membrilla
 * 
 * USO:
 * $("#mw").iniMainWidget();
 * 
 * NOTAS:
 * - En esta versi—n si se utilizan otras classes e Id para los tags, puede derivar en errores.
 * - No hay que olvidar a–adir el css
 * 
 * 
 * 
 */


(function($)
{
	var timerID = 0;

	$.selectedItem = 1;			//screen seleccionada al comienzo (1 = primera)
	$.delay = 1000; //6000;				//delay entre transiciones
	$.vLight = 200;				//velocidad del highlightning (milisegundos)
	$.vPageChange = 1000;		//velocidad de la transiciÃ³n de cambio de screenshot (milisegundos)
	$.pause = false;
	$.opacity = 0.4;
	$.numcovers = 20;


	
	function refresh() {
		$('.mw-cover:not(.selected)').stop().animate({
			opacity: $.opacity
		},$.vLight);
		$('.mw-cover.selected').stop().animate({
			opacity:"1"
		},$.vLight);
	}
	
	
	$.fn.getId = function () 
	{
		return $(this).attr('id');
	}
	
	$.fn.getIndex = function ()
	{
		return $(this).getId().substr(9);
	}
	
	$.fn.move = function()
	{
		var desplazamiento = 0;
		desplazamiento = ($.selectedItem.getIndex()-1) * 20 * (-1);
		$('#mw-info').stop().animate({
			marginTop: desplazamiento
		}, $.vLight, 'swing');
	}
	
	$.fn.selectItem = function ()
	{
		$('.mw-cover').removeClass("selected");
		$(this).addClass("selected");
		
		$.selectedItem = $(this);
		
		var desplazamiento = 0;
		desplazamiento = ($.selectedItem.getIndex()-1) * 20 * (-1);
		
		$('#mw-info').stop().animate({
			marginTop: desplazamiento
		}, 200, 'swing');
		refresh();
		
	}
	
	$.fn.overCover = function()
	{

		$(this).stop().animate({
			opacity: "1"
		},$.vLight);
	}
	
	$.fn.outCover = function()
	{
		$(this).stop().animate({
			opacity: $.opacity
		},$.vLight);
	}
	
	$.fn.iniMainWidget = function()
	{

		refresh();
		$(this).resume();
		
		//EVENTS
		$('img.mw-cover').click( function () {
			$(this).gotoURL();
    	});
    	$('img.mw-cover').hover( function () {
    		$(this).selectItem();
    	});
    	$('img.mw-cover').mouseleave( function () {
    		if (!$(this).hasClass("selected")) $(this).outCover();
    	});

    	$('#mw').hover( function() {
    		$(this).pause();	
    	});
    	$('#mw').mouseleave( function() {
    		$(this).resume();	
    	});

	}
	
	$.fn.gotoURL = function() {
		window.location = $(this).attr('href');
	}
	
	$.fn.selectNext = function()
	{

		var selectedItem = $('.mw-cover.selected');
		$('.mw-cover').removeClass("selected");
		
		if(selectedItem.getIndex() < $.numcovers) {
			selectedItem = selectedItem.parent().next().children();
			selectedItem.addClass('selected');	
		}
		else
		{
			$('.mw-cover-list > li:first > img').addClass('selected');
			selectedItem = $('.mw-cover-list > li:first > img');
		}
		$.selectedItem = selectedItem;
		
		$.fn.move();
		refresh();	

	}

	
	$.fn.pause = function () {
		clearInterval(timerID);
		$(this).pause = true;
	}
	
	$.fn.resume =function () {
		
		if($(this).pause) {
			timerID = setInterval($(this).selectNext, $.delay);
			$(this).pause = false;
		}
	}
	
})(jQuery);

