//accesible hidden elements
document.write("<style type='text/css'>.hidden { display: none;}</style>");

$(document).ready(function(){
	
	//rounded corners fallback in IE >= 7
	if ($.browser.msie && $.browser.version >= 7) {
	
		var border_radius_tpl = "<div class='b-radius tl'></div> <div class='b-radius tr'></div> <div class='b-radius bl'></div> <div class='b-radius br'></div>";
		$(".mod:not(.mod-simple)").append(border_radius_tpl);
			
	};
	
	
	//tabs, variables to pass as string sizzle selectors => container, nav, content, selected, active
	tabbed.init(".mod-tabs", ".min-tabs", ".t-content", "selected");
	
	
	//placeholder
	placeholder.init();
	
	//PLACEHOLDER INPUTS
	$(".placeholder .i-text").each(function(){
	
		if(this.value=='') {
			
			} else {
				
				$(this).parent().find("label").hide();
				
			}
	
	});
	
	$(".placeholder .i-text").live("focus",function(){
		
		$(this).parent().addClass("focused");
		$(this).parent().removeClass("blured");
		$(this).parent().find("label").fadeOut(70);
	
	});
	
	$(".placeholder .i-text").live("blur",function(){
		
		$(this).parent().removeClass("focused");
		$(this).parent().addClass("blured");
		
		if(this.value=='') {
			
			$(this).parent().find("label").fadeIn(70);
			
			} else {
				
				$(this).parent().find("label").hide();
				
			}
	});
	
	//sub-nav show / hide => custom animations
	$("#nav .sub-nav").bind({
	
	  	mouseenter: function() {
	  		
	  		$(this).addClass("hovered");
  			$(this).find(".sn-inner").css({ "right": "-20px", "display": "block", "opacity": "0" }).stop().animate({
  			
  				"right": "0",
  				"opacity": "1"
  			
  			}, "fast");
	  		
	  
	  	},
	  	mouseleave: function() {
	   		
	   		$(this).removeClass("hovered");		  			
  			$(this).find(".sn-inner").stop().animate({
  			
  				"right": "-20px",
  				"opacity": "0"
  			
  			}, "fast", function(){ $(this).css({ "display": "none" }) });
	  		
	  	}
	});
	
	
	if ( $(".rotatore").length >= 1 ) {
	
		//rotatore main
		$('.mod-main-rotatore .rotatore .m-util').append("<ul class='u-pager'></ul>").parent().find(".r-clip ul").cycle({ 
		    fx:     'fade', 
		    speed:  'slow', 
		    timeout: 9000,
		    pager:  '.mod-main-rotatore .rotatore .u-pager', 
		    next:   '.mod-main-rotatore .rotatore .r-controls .r-next', 
		    prev:   '.mod-main-rotatore .rotatore .r-controls .r-prev' 
		});
		
		
		//rotatore featured schools
		$('.mod-schools-featured .rotatore .r-clip ul').cycle({ 
		    fx:     'scrollHorz', 
		    speed:  'fast', 
		    timeout: 0,
		    next:   '.mod-schools-featured .rotatore .r-controls .r-next', 
		    prev:   '.mod-schools-featured .rotatore .r-controls .r-prev' 
		});
		
		
		//rotatore agenda
		$('.mod-agenda .rotatore .r-clip ul').cycle({ 
		    fx:     'scrollVert', 
		    speed:  'slow', 
		    timeout: 9000,
		    next:   '.mod-agenda .rotatore .r-controls .r-next', 
		    prev:   '.mod-agenda .rotatore .r-controls .r-prev' 
		});
		
		
	}
	
	
	//rotatore tabbed
	$(".rotatore-tabbed .col-l li").live("click", function(){
	
		var pos = $(this).index();
		
		$(this).parents(".rotatore-tabbed .col-l").find("li").removeClass("selected");
		$(this).addClass("selected");
		
		$(this).parents(".rotatore-tabbed").find(".col-r li").fadeOut();
		$(this).parents(".rotatore-tabbed").find(".col-r li:eq("+pos+")").fadeIn();
		$(this).parents(".rotatore-tabbed").find(".col-r li:eq("+pos+")").find(".m-txt").css({"bottom": "-100px"}).animate({ "bottom": "0" });
	
	});
	
	//auto rotating => mm-methods.js to alter this function
 	if ($(".rotatore-tabbed").length > 0) {
 		
 		setInterval('rotatore_tabbed()',6000);	
 	
 	}
	
	
	//photo viewer
	$(".photo-viewer li a").live("click", function(){
		
		var path = $(this).attr("href");
		
		$(this).parents(".photo-viewer").find(".selected").removeClass("selected");
		$(this).parent().addClass("selected");
		
		$(this).parents(".photo-viewer").find(".pv-main img").attr("src", path);
		
		return false;
	});
	
	
	//browser targeting
	if ($.browser.msie && $.browser.version < 8 ) {
		
		if( $(".map-selector").parent().find(".mod-filter").length ) {
			
			$(".map-selector").hide();
		} else {
		
			$(".mod-map").hide()
		}
		
	} else {
	
		//map selector
		$(".map-selector > li > a").bind({
		
			mouseenter: function() {
			
				var text = $(this).text();
				var tooltip_tpl = "<p class='u-tooltip hidden'><span>"+text+"</span></p>";
				
				$(this).parents("ul:not(.ms-selected)").find(" > li").removeClass("selected");
				$(this).parents("ul:not(.ms-selected)").find(".u-tooltip").hide();
					
				$(this).parent().addClass("hovered");
				
				if ($(this).parent().find(".u-tooltip").length > 0) {
					
					$(this).parent().find(".u-tooltip").show();
					
				} else {
					
					$(this).parent().append(tooltip_tpl);
					$(this).parent().find(".u-tooltip").show();
					
				}
			
			},
			
			mouseleave: function() {
				
				$(this).parent().removeClass("hovered");
				$(this).parent().find(".u-tooltip").hide();
				
				
			},
			
			click: function() {
				
				if ($(this).parent().find("ul").length > 0 ) {
				
					var left_default = $(this).parents("li").css("left");
					var top_default = $(this).parents("li").css("top");
					
					$(this).parents(".map-selector").addClass("ms-selected");
					$(this).parent().addClass("selected").find("div").css({ "right": "-105px", "display": "block", "opacity": "0"}).animate({ "right": "-125px", "opacity": "1" }, "fast");
					$(this).parents(".map-selector").find(" > li:not(.selected)").animate({ "opacity": "0.5" });
					
					
					$(".map-selector .u-close a").click(function(){
						
						$(this).parents(".map-selector").find(" > li:not(.selected)").animate({ "opacity": "1" });
						$(this).parents("li").find("div").animate({ "right": "-105px", "opacity": "0" }, "fast", function(){ 
							
							$(this).css({ "display": "none"});
							$(this).parents("li").removeClass("selected");
							
						});
							
						$(this).parents(".map-selector").removeClass("ms-selected");
						
						$(".map-selector .u-close a").unbind("click");
						
						return false;
					});
					
					
					return false;
					
				} 
			
			}
		
		});
	}
	
		
	//calendar tooltip show
	$(".mod-calendar table td a").bind({
		
		mouseenter: function(){
		
			$(this).parent().find(".u-tooltip").show();
		},
		
		mouseleave: function(){
			
			$(this).parent().find(".u-tooltip").hide();
		}
	
	});
	
	//view more filters
	$(".l-filter .u-more a").live("click", function(){
	 	
     	
     	if ($.browser.msie ) {
     		
     		$(this).parents(".u-more").slideUp();
     		$(this).parents("li").find(".hidden").slideDown();
     		
     	} else {
     	
	     	$(this).parents(".u-more").animate({"margin": "-35px", "opacity": "0" }, function(){ $(this).hide(); });
	     	
	     	var items = $(this).parents("li").find(".hidden");
		 	var i = 0;
		 	
		 	items.css({ "display": "block", "margin-left": "-35px", "margin-top": "-10px", "opacity": "0" });
		 	
	     	
	     	(function displayImages() {
	     		
			     items.eq(i++).animate({ "margin-left": "0", "margin-top": "0", "opacity": "1" }, 100, displayImages);  
			     
			  })(); 
	     	 
		}
		
		return false;
	});
	
	
	//view more categories
	$(".mod-categories .u-more").live("click", function(){
		
		var more_text = "mostrar subcategor\u00EDas";
		var less_text = "ocultar subcategor\u00EDas";
		
		if ($(this).is(".u-less")) {
			
			$(this).removeClass("u-less");
			$(this).find("a").text(more_text);
			$(this).parents(".mod-categories").find(".l-categories").slideUp();
			
		} else {
		
			$(this).addClass("u-less");
			$(this).find("a").text(less_text);
			$(this).parents(".mod-categories").find(".l-categories").slideDown();
		}
		
		return false;
		
	});
	
	
	/*animated font resizer
	
	<ul class="u-text">
		<li class="reset"><a title="Tamaño de texto predeterminado" href="#" class="">reset</a></li>
		<li class="less"><a title="Reducir el tamaño del texto" href="#" class="">Reducir el tamaño del texto</a></li>
		<li class="more"><a title="Aumentar el tamaño del texto" href="#" class="">Aumentar el tamaño del texto</a></li>			
	</ul>
	*/
	$('.u-text li:not(".disabled") a').live("mouseup", function(){
	 
	 	$(this).removeClass("pressed");
	 	
	 }).live("mousedown", function(){
	 
	 	$(this).addClass("pressed");
	 	
	});
	
	 $('.u-text li a').live("click", function(){
    
    	if ($(this).parent().is(":not('.disabled')")) {
    	
    		var ourText = $('.mod-detail .m-content .c-body');
			var currFontSize = ourText.css('fontSize');
			var currFontSizeNum = currFontSize.slice(0, -2);
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);
			if($(this).parent().attr("class") == 'more') {
				
				if (currFontSizeNum > 20) {
					
					$(this).parent().addClass("disabled");
				}
				
				else {
					
					$(this).parent().parent().find(".less").removeClass("disabled");
					
				}
			
				finalNum *= 1.2;
			}
			else if ($(this).parent().attr("class") == 'less'){
				
				if (currFontSizeNum < 10){
						
						$(this).parent().addClass("disabled");
					}
					
					else {
					
						$(this).parent().parent().find(".more").removeClass("disabled");
						
						
					}
			
				finalNum /=1.2;
				
			} else if ($(this).parent().attr("class") == 'reset'){
				
				
				$(this).parent().parent().find("li").removeClass("disabled");
				finalNum =10;
			}
			
			ourText.animate({fontSize: finalNum + stringEnding},300);
    	
    	}
    	
		
		return false;
	});




});


