var fauxScroll = function(){
	var scrollIt = function(){
		$(window).trigger("resize");
	}
	setTimeout(scrollIt,500);
}

var cuanto = 1;
var haypausa = 0;

//alert(cuanto);




var modTop = function(flash){
	//LOADER
var paper = new Raphael(document.getElementById('canvas_container'), 52, 19);
                //var tetronimo = paper.rect(0, 0, 52, 52);
				var tetronimo = paper.image("loader.png", 0, 0, 52, 52);
				 tetronimo.attr({rotation: -148});
				 //   tetronimo.animate({rotation: 0}, 3000, '>');
				 
				 
				 
				 
	$("div.top-tits").css("left",0);	// Mueve los titulos al centro una vez que han sido cargados todos			 
				 
				 
				 
	var current;
	var scope = $("#top_horizontal");
	if(scope.length < 1){return false;}
	var sc = $("div.scrollable",scope).scrollable({size:6,onSeek:fauxScroll,keyboard:true,clickable: true,hoverClass:"hover",prev:".p",next:".n",prevPage:".prev",nextPage:".next"}).scrollable();
	var items = sc.getItems();
	var imgs2 = $(".top-imgs div.cc", scope);
	var tits = $(".top-tits div", scope);
	var ligamain = $(".top-tits div a", scope);
	var selectTop = function(e){
		
		current = items.index(this);
		imgs2.hide().eq(current).show(); // CAMBIA IMAGEN
		tits.hide().eq(current).show(); // CAMBIA TITULO
		
		linkactualmain = ligamain.eq(current).attr("href"); // SACA LAS LIGAS DE LAS PANTALLAS DEL MAIN PANEL
		//alert(ligamain.eq(current).attr("href"));
		
		
		if(cuanto > 1) {
			tetronimo.attr({rotation: -148});
		   if(haypausa == 0) {
			inicialoader();
		   }
		   clearInterval(paraplay);
		   if(haypausa == 0) {
			   paraplay = setInterval(avanza, 10000);
			}

		}
		
		$('#eltimer').width(1);

	//	elloader();
		//alert(current);//
		//alert("HOLA");		
	};
	//MIO Botones siguiente y atras
	$('#siguiente').click(function(){
			//alert(current);
			//if(current
		totales = $("div.scrollable").scrollable().getSize();
		if (current == (totales-1)) {
			ira = 0;
		}else{
			ira = current+1;
		}
			//alert(totales);
			//
		
		
			items.click(selectTop).eq(ira).click();
			clearInterval(paraplay); 
			paraplay = setInterval(avanza, 10000);
			if(haypausa == 1) {
			   clearInterval(paraplay);
			}
		   
		
		
	});


	$('#atras').click(function(){
			//alert(current);
			//if(current
		paginas = $("div.scrollable").scrollable().getPageAmount(); // Nos da la cantidad de páginas
		totales = $("div.scrollable").scrollable().getSize(); // Nos da la cantidad de elementos totales dentro del carrusel
		if (current == 0) {
			ira = totales-1;
			$(".navi a").eq(paginas-1).click();
		}else{
			ira = current-1;
		}
			//alert(totales);
			//
		
		
			items.click(selectTop).eq(ira).click();
			clearInterval(paraplay); 
			paraplay = setInterval(avanza, 10000);
			if(haypausa == 1) {
			   clearInterval(paraplay);
			}
		
		
	});
		
			
		$('#pausa').click(function(){
			$('#pausa').hide();
			$('#portatimer').hide();
			$('#play').show();
			clearInterval(paraplay); 
			clearInterval(paratimer); 
			paraloader();
			haypausa = 1;
		});
		
		$('#play').click(function(){
			$('#pausa').show();
			$('#play').hide();
			$('#portatimer').show();
			$('#eltimer').width(1);
			paratimer = setInterval(eltimer, 100);
			paraplay = setInterval(avanza, 10000);
			inicialoader();
			haypausa = 0;
		});
		$('#play').hide();
	//--------------
		

	//--------------------------------
	
	var keyFn = function(evt) {
		if (evt.keyCode == 37 || evt.keyCode == 39) {					
			var pos = current + (evt.keyCode==37?-1:1);
			if(pos >= items.length){pos = 0;}else if(pos < 0){	pos = items.length-1;}
			//MIO: Aregla paginación cuando se va del primer al último elemento
			if(pos == items.length-1) {
				$(".navi a").eq(paginas-1).click();
			}
			//--
			items.eq(pos).click();
			return false;
		}	
		return true;
	}
	$(document).bind($.browser.mozilla?"keyup.scrollable":"keyup.scrollable", keyFn);
	paginas = $("div.scrollable").scrollable().getPageAmount();
	largonavi = 28*paginas;
	//alert(paginas);
	$("div.navi").css("width",largonavi);	
	items.click(selectTop).eq(0).click();
	
	
var avanza = function() {
	//this.move(2);
	//this.getItems().fadeTo(300, 0.2);
	totales = $("div.scrollable").scrollable().getSize();
		if (current == (totales-1)) {
			ira = 0;
		}else{
			ira = current+1;
		}
			//alert(totales);
			//
		items.click(selectTop).eq(ira).click();

}


		var avanza2 = function() {
	
	//		alert("totales");
		clearInterval(paraplay); 

}
paraplay = setInterval(avanza, 10000);
			
var eltimer = function() {
		
		largotimer = $('#eltimer').width();
		if(largotimer < 101){
			largotimer++;
			$('#eltimer').width(largotimer);
		}
	}

		
	paratimer = setInterval(eltimer, 100);



inicialoader();
cuanto = 2;
//alert(cuanto);
	
	 
                                   
             
 
				
				
			
				  
function inicialoader() {
               
				tetronimo.animate({rotation: 2}, 9500, 'linear', function() {
                    /* callback after original animation finishes */
                    //this.remove();
					reinicialoader()
                });
				
				//tetronimo.clear();
}


function reinicialoader() {
				tetronimo.animate({rotation: -148}, 500, '>', function() {
                    /* callback after original animation finishes */
                    //this.clear();
					//inicialoader();
                });
				//alert("caca");
}


function paraloader() {
				tetronimo.animate({rotation: -148}, 500, '>', function() {
                    /* callback after original animation finishes */
                   //this.clear(); ----------------------------------------------- OJO, lo quité el 21/06/10 porque IE marcaba error, pero no se si va a tener alguna repercusión
					 //inicialoader();
                });
				//alert("caca");
}



};

/////////////////////////////// CALENDARIO //////////////////////////////////////////////////////
(function($) { 
   
	function calendarWidget(el, params) { 
		
		var now   = new Date();
		var thismonth = now.getMonth();
		var thisyear  = now.getYear() + 1900;
		
		var opts = {
			month: thismonth,
			year: thisyear
		};
		
		$.extend(opts, params);
		
		var monthNames = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
		var dayNames = ['D', 'L', 'M', 'M', 'J', 'V', 'S'];
		month = i = parseInt(opts.month);
		year = parseInt(opts.year);
		var m = 0;
		var table = '';
		
			// next month
			if (month == 11) {
				var next_month = '<a href="?month=' + 1 + '&amp;year=' + (year + 1) + '" title="' + monthNames[0] + ' ' + (year + 1) + '">' + monthNames[0] + ' ' + (year + 1) + '</a>';
			} else {
				var next_month = '<a href="?month=' + (month + 2) + '&amp;year=' + (year) + '" title="' + monthNames[month + 1] + ' ' + (year) + '">' + monthNames[month + 1] + ' ' + (year) + '</a>';
			}
				
			// previous month
			if (month == 0) {
				var prev_month = '<a href="?month=' + 12 + '&amp;year=' + (year - 1) + '" title="' + monthNames[11] + ' ' + (year - 1) + '">' + monthNames[11] + ' ' + (year - 1) + '</a>';
			} else {
				var prev_month = '<a href="?month=' + (month) + '&amp;year=' + (year) + '" title="' + monthNames[month - 1] + ' ' + (year) + '">' + monthNames[month - 1] + ' ' + (year) + '</a>';
			}		
				
			table += ('<h3 id="current-month">'+monthNames[month]+' '+year+'</h3>');
			// uncomment the following lines if you'd like to display calendar month based on 'month' and 'view' paramaters from the URL
			//table += ('<div class="nav-prev">'+ prev_month +'</div>');
			//table += ('<div class="nav-next">'+ next_month +'</div>');
			table += ('<table class="calendar-month " ' +'id="calendar-month'+i+' " cellspacing="0">');	
		
			table += '<tr>';
			
			for (d=0; d<7; d++) {
				table += '<th class="weekday">' + dayNames[d] + '</th>';
			}
			
			table += '</tr>';
		
			var days = getDaysInMonth(month,year);
            var firstDayDate=new Date(year,month,1);
            var firstDay=firstDayDate.getDay();
			
			var prev_days = getDaysInMonth(month,year);
            var firstDayDate=new Date(year,month,1);
            var firstDay=firstDayDate.getDay();
			
			var prev_m = month == 0 ? 11 : month-1;
			var prev_y = prev_m == 11 ? year - 1 : year;
			var prev_days = getDaysInMonth(prev_m, prev_y);
			firstDay = (firstDay == 0 && firstDayDate) ? 7 : firstDay;
	
			var i = 0;
            for (j=0;j<42;j++){
			  
              if ((j<firstDay)){
                table += ('<td class="other-month"><span class="day">'+ (prev_days-firstDay+j+1) +'</span></td>');
			  } else if ((j>=firstDay+getDaysInMonth(month,year))) {
				i = i+1;
                table += ('<td class="other-month"><span class="day">'+ i +'</span></td>');			 
              }else{
                table += ('<td class="current-month day'+(j-firstDay+1)+'"><span class="day">'+(j-firstDay+1)+'</span></td>');
              }
              if (j%7==6)  table += ('</tr>');
            }

            table += ('</table>');

		el.html(table);
	}
	
	function getDaysInMonth(month,year)  {
		var daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];
		if ((month==1)&&(year%4==0)&&((year%100!=0)||(year%400==0))){
		  return 29;
		}else{
		  return daysInMonth[month];
		}
	}
	
	
	// jQuery plugin initialisation
	$.fn.calendarWidget = function(params) {    
		calendarWidget(this, params);		
		return this; 
	}; 

})(jQuery);
