// JavaScript Document

// Load this script when page loads
$(document).ready(function(){
  
  // Set up a listener so that when anything with a class of 'tab' 
  // is clicked, this function is run.
	$('.tab').click(function () {

	  // Remove the 'active' class from the active tab.
	  $('.tabs > li.active').removeClass('active');
	  
	  // Add the 'active' class to the clicked tab.
    $(this).parent().addClass('active');
    
    // Remove the 'tab_contents_active' class from the visible tab contents.
    $('#tabs_container > .tab_contents_container > div.tab_contents_active').removeClass('tab_contents_active');
    
    // Add the 'tab_contents_active' class to the associated tab contents.
    $(this.rel).addClass('tab_contents_active');
    
  });
});

/*<![CDATA[*/

function Disable(cb,but){
 var cbs=document.getElementsByName(cb.name);
 but=cbs[0].form[but]
 but.setAttribute('disabled','disabled');
 for (var zxc0=0;zxc0<cbs.length;zxc0++){
  if (cbs[zxc0].checked){
   but.removeAttribute('disabled');
   break;
  }
 }

}
/*]]>*/

$(document).ready(function(){


  $('#banner_swapper').click(function() {
    $('#banner').slideToggle(1000);
    return false;
  });
  
  $('#swapper').click(function() {
    $('#advancedSearch').slideToggle(1000);
    return false;
  });
  
    $('#promo_swapper').click(function() {
    $('#promo').slideToggle(1000);
    return false;
  });

//AUTOCOMPLETE SHIT


var specs = $.ajax({
					  url: "ajax/specs.php",
					  async: false
					  }).responseText.split("#");
	
	
	var data = "Adjuntas#Aguada#Aguadilla#Aguas Buenas#Aibonito#Añasco#Arecibo#Arroyo#Barceloneta#Barranquitas#Bayamón#Cabo Rojo#Caguas#Camuy#Canóvanas#Carolina#Cataño#Cayey#Ceiba#Ciales#Cidra#Coamo#Comerío#Corozal#Culebra#Dorado#Fajardo#Florida#Guánica#Guayama#Guayanilla#Guaynabo#Gurabo#Hatillo#Hormigueros#Humacao#Isabela#Jayuya#Juana Díaz#Juncos#Lajas#Lares#Las Marías#Las Piedras#Loíza#Luquillo#Manatí#Maricao#Maunabo#Mayagüez#Moca#Morovis#Naguabo#Naranjito#Orocovis#Patillas#Peñuelas#Ponce#Quebradillas#Rincón#Río Grande#Sabana Grande#Salinas#San Germán#San Juan#San Lorenzo#San Sebastián#Santa Isabel#Toa Alta#Toa Baja#Trujillo Alto#Utuado#Vega Alta#Vega Baja#Vieques#Villalba#Yabucoa#Yauco#Todos".split("#");
	
	$("#pueblo").autocomplete(data);
	$("#especialidad").autocomplete(specs);
	
	$("#submit").click(function(){
		$(".error").hide();
		
		var hasError = false;
		var pueblo = $("#pueblo").val();
		var spec = $("#especialidad").val();
		
		if(spec == '' || spec == 'Introduce una especialidad...'){
			//$("#especialidad").after('<span class="error">Llene el campo de Especialidad.</span>');
			hasError = true;	
		}
		//if(pueblo == '')	{
			//$("#pueblo").after('<span class="error">Escriba un pueblo.</span>');
		//	hasError = true;
		//}
		
		if(hasError == false){ return true; }
		if(hasError == true){ return false; }
	});		

  
});
//CODIGO DE VERSION IMPRIMIBLE

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

//TIMED NOTICES
$(function() {

jQuery.fn.delay = function(time,func){
    return this.each(function(){
        setTimeout(func,time);
    });
};

$('.notice').delay(15500, function(){$('.notice').fadeOut(1500)})
$('.error').delay(150500, function(){$('.error').fadeOut(1500)})
$('.success').delay(15500, function(){$('.success').fadeOut(1500)})


});   


