/* Author: 

*/

if( $("#search").length > 0){
   
   
	   $("#search").blur(function () {
			if($(this).val() == ""){
				//$(this).val("Buscar...");
				$("#resultbox").css("display", "none"); 
			}
		});
		
		$("#search").focus(function () {
			/*if ($(this).val() == "Buscar...") {
				$(this).val("");
			}*/
		});
		
		$("#search").keyup(function () {
			$("#resultbox").css("display", ""); 
			$("#main-content").fadeOut();
			
			$(".googleheader").html('<img src="images/loading.gif" width="16" height="16" style="margin-right:8px; vertical-align:-15%;" />Loading Results from Google...');
			
		
			
			/* === Google Search === */
			var sc =  new google.search.CustomSearchControl('004121538745642376602:huq1im2zrru');
			
			sc.addSearcher(new GwebSearch());
			sc.addSearcher(new GvideoSearch());
			sc.addSearcher(new GblogSearch());
			sc.addSearcher(new GnewsSearch());
	
			var drawOptions = new GdrawOptions();
			drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
			
			var foo;
			sc.setSearchCompleteCallback(foo, function(){
				$(".googleheader").html('Resultados:');
			});
			sc.setNoResultsString(GSearchControl.NO_RESULTS_DEFAULT_STRING);
	
			sc.draw(document.getElementById("googlesearch"), drawOptions);
			sc.execute(document.getElementById("search").value);
			/* === End Search === */
			
			if($(this).val() == ""){
				$("#resultbox").slideUp(700, function(){
					$("#main-content").fadeIn();
				});
			}
		});
   }
   
   $.fn.simpleSpy = function (interval,maxspace,url) {
	   
    interval = interval || 4000;
    maxspace = maxspace || -1;
	url = url || '#';
    
    return this.each(function () {
        // 1. setup
            // capture a cache of all the list items
            // chomp the list down to limit li elements
        var $list = $(this),
            items = [], // uninitialised
            currentItem = 1, //se inicializa en el segundo
            total = 0, // initialise later on
			alt = [],
			contador = 0,
			containerHeight =0;
            
        // capture the cache
		
		
        $list.find('> li').each(function (index) {
            items.push('<li id= '+index+'>' + $(this).html() + '</li>');
			alt[index] = $(this).height();
        });
        
        total = items.length;
		
		//alert(location.host);
		
		if(total > 1) { //si hay mas de un elemento
		
			$list.find('> li').filter(':gt(0)').remove();
				
			function spy() {
				
				var $insert = $(items[currentItem]).css({ //el siguiente se inserta en la lista que se muestra
					opacity : 0,
					display: 'none',
					cursor : 'pointer'
				}).click(function() {
					
					window.location.href = location.host + url;
					
				}).appendTo($list);
				
			
				
				// fade the LAST item out
				$list.find(' > li:first').animate({ opacity : 0}, 500, function () {
					
					//alert("lola "+alt[currentItem]);
					
					$(this).css(
						{ 	display: 'none'}
					);
					
					$insert.animate({ height : alt[contador] },0).animate({ opacity : 1 }, 600);
					
					$(this).remove();
				
				});
				
				//alert("debe ser igual: "+alt[currentItem]);
				
				currentItem++;
				contador++;
					
				if (currentItem >= total) {
					currentItem = 0;
				
				}
				
				if (contador >= total) {
					contador = 0;
				}
				
				setTimeout(spy, interval)
		
			}
			
			spy();
		}
    });
};

if( $("#acreditaciones").length > 0){
		$('#acreditaciones').simpleSpy(9000, 200, 'quienes-somos-reconocimientos.html');
	}
	
if( $("#maternidad-box").length > 0){
		$('#maternidad-box').simpleSpy(9000, 200, '#');
	}

