/*
   TODO
   - search txt in grey to start with
   - show loader?
   
   
   - FIX :: firefox keyboard killer bug?

*/


/**********************************************************/
/**********************************************************/
/*    BEGIN DROPDOWN SYSTEM   */


function closeDropDown(force, div_el) {
   debug('closeDropDown:: force: '+force+' this: '+this);
   debug('div_el: '+div_el);
   
   var force_close = force===undefined?false:force;
   //debug('force_close: '+force_close);
   //if(this==window) return;
   
   //check if any drop downs are open!
   $.each($('div.std_button ul'), function(i,ul){
      
      /*
$.each($('div.std_button:has(ul)'), function(i,item){
         //debug('unbind and add back event');
         $(item).unbind('click',showDropDown);
         $(item).click(showDropDown);
      });
*/


      
      var open_status = $(ul).data("open_status");
      //debug('uls here is_open: '+open_status); 

      $('.dropdown_overlay').css('top','-3000px');

      $(ul).css('display','none');
      
      $(ul).parent().css('z-index',50);
      
      /*if(open_status == 2) {
         $(ul).css('display','none');
      } else if(open_status == 1) {
         $(ul).data("open_status", 2);
      }*/
      
   });
}

function showDropDown() {
   debug('showDropDown::'+this);
   
   //remove the event
   //$(this).unbind('click',showDropDown);
   
   $.each($(this).find('ul'), function (a, ul) {
      
      //debug('showing dropdown');
      
      //already shown.. clicked same div!
      
      
         
      debug('css: '+$(ul).css('display'));
               
      if($(ul).css('display')=='block') {
         closeDropDown(false, this);
         return;
      }
     
      //close all existing dropdowns.
      closeDropDown(false, this);
     
      
      var curr_state = $(ul).css('display');
      if(curr_state=='none') {

         $(ul).css('display','block');
         $(ul).topZIndex();
         
         var ul_index = $(ul).css('z-index');
         
         //debug('ul_index: '+ul_index);
         
         //add dropdown overlay for flash?
         if(!isset('.dropdown_overlay')) {

            //debug('adding dropdown');

            //$("<div class='dropdown_overlay'>&nbsp;</div>").prependTo($(ul).parent());
            $("<div class='dropdown_overlay'>&nbsp;</div>").appendTo('body');//prependTo('body');
            // CLOSE dropdown menu(s)
            $('.dropdown_overlay').css('top','-3000px');
            $('.dropdown_overlay').css('opacity',0.0);
            
            $('.dropdown_overlay').click(closeDropDown);
         }
         
         
         $('.dropdown_overlay').css('top','0px');
         $('.dropdown_overlay').css('z-index',ul_index-100);
         
         var body_height = $(document).height();//$('body').css('height');
         //debug('body_height: '+body_height);
         
         $('.dropdown_overlay').css('height',body_height);
         
         
         $(ul).parentsUntil('body').each(function(g,par) {
            //debug('parents: '+par.tagName);
            //$(par).topZIndex();
            $(par).css('z-index',ul_index);
         })
         
         $(ul).data("open_status", 1);   
         

      } else {
         closeDropDown();
      }
   });

}

function initDropDowns() {

   //works normally if no flash elements stealing clicks!!
   /*$('body').click(function() {
      debug('clicked the body');
      //check if any drop downs are open!
      $.each($('div.std_button ul'), function(i,ul){
         
         var open_status = $(ul).data("open_status");
         //debug('uls here is_open: '+open_status); 

         if(open_status == 2) {
            $(ul).css('display','none');
         } else if(open_status == 1) {
            $(ul).data("open_status", 2);
         }
         
      });
      
   });*/


   //$(item).find('a:first').hover(
   $.each($('div.std_button'), function(v,div_a) {
      $(div_a).find('a:first').hover(
         function(event) {
            //debug('over');
            $(this).addClass('div_hover');
         },
         function(event) {
            $(this).removeClass('div_hover');
         }
      )
   });


   //select all buttons with dropdown options
   $.each($('div.std_button:has(ul)'), function(i,item){
      
      //set ul MIN-width to same as button width
      
      //debug('we have a button!');
      
      //hide ul by default
      $.each($(item).find('ul'), function (a, ul) {
         $(ul).data("open_status", -1);
      });
      
      $(item).click(showDropDown);
      
      $.each($(item).find('ul'), function(y, ul) {
         $(ul).css('display','none');
      });
            
      $.each($(item).find('ul a'), function(x, tag) {
         var href = $(tag).attr("href");
         //debug('we have a link!: '+href);
         $(tag).removeAttr("href");
         
         
         $(tag).data("link", { url: href });


         //close the parent
         //$(tag).closest('div.std_button ul').css('display','none');
         
         
         //debug('jquery: '+$(tag).data("link").url);
         
         
      
         $(tag).hover(
            function(event) {
               //debug('over');
               $(this).parent().attr('class','li_hover');
            },
            function(event) {
               $(this).parent().attr('class','');
            }
         );
      
         $(tag).click(function(event) {
            var link = $(this).data("link").url;
            debug('clicked a link: url: '+link);
            
            //alert('href: '+link);
            
            //go to url
            //ie looks for !=''... firefox does not?
            if(link!=undefined && link!='') {
               location.href = link;
            }
            
            //hide nav menu (if applicable)
            closeDropDown(true);
            //event.stopPropagation();
            
            
            if (event.stopPropagation) {
               event.stopPropagation();
            } else {
               event.cancelBubble = true;
            } 
            
            
         });
         
      });    
      
   });

}

/*    END DROPDOWN SYSTEM   */
/**********************************************************/
/**********************************************************/














/**********************************************************/
/**********************************************************/
/*    BEGIN SEARCH SYSTEM   */

var submit_form = false;
var total_searches = 0;

function sendForm() {
   return false;
}

function closeSearch() {
   $("div.search_box").animate({ top: '-445px'}, 'slow'); 
   $('#close_btn').css('display','none');
   $('.overlay_on').css('top','-1000px');
   $('.overlay_on').animate({opacity: 0}, 500);
   closeDropDown(true);
}

function setSearch(el) {
   var search_word = $(el).text();
   search_word = search_word.replace('- ','');
   //debug('search_word: '+search_word);
   $('#search_input').val(search_word);
   searchReady();
   doSearch();
}

function doSearch() {

   var search_val = $('#search_input').val();
   //debug('search_val: '+search_val);
   
   
   var search_val_encoded = search_val.toLowerCase();//'Catá';
   
   search_val_encoded = search_val_encoded.replace(/á/g, 'a');
   search_val_encoded = search_val_encoded.replace(/ã/g, 'a');
   search_val_encoded = search_val_encoded.replace(/é/g, 'e');
   search_val_encoded = search_val_encoded.replace(/í/g, 'i');
   search_val_encoded = search_val_encoded.replace(/¡/g, 'i');
   search_val_encoded = search_val_encoded.replace(/ó/g, 'o');
   search_val_encoded = search_val_encoded.replace(/ú/g, 'u');
   search_val_encoded = search_val_encoded.replace(/ü/g, 'u');
   search_val_encoded = search_val_encoded.replace(/ñ/g, 'n');
   search_val_encoded = search_val_encoded.replace(/¿/g, '');
   
   //debug('search_val_encoded: '+search_val_encoded);
   
   
   
   
   
   
   
   
   total_searches++;
   if(pageTracker) {
      
      if(search_val=='ga_search_test') {
         pageTracker._trackPageview(site_url+"search/search.php?search="+search_val);
      } else {
         pageTracker._trackEvent('search-original', search_val, 'User search', total_searches);
      }
      
      //pageTracker._trackEvent('search-popular', search_val, 'user search', optional_value)
   }

   closeDropDown(true);
   
   if(!$('.overlay_on').length) {
      $("<div class='overlay_on'>&nbsp;</div>").appendTo("body");
   }
   
   if($('#close_btn').css('display') != 'block') {
   
      $('.overlay_on').css('top','0px');
      $('.overlay_on').animate({opacity: 0}, 0);
      $('.overlay_on').animate({opacity: 0.5}, 500);
      $('.overlay_on').click(closeSearch);
      
      
      var body_height = $(document).height();//$('body').css('height');
      //debug('body_height: '+body_height);
      $('.overlay_on').css('height',body_height);
      
      
      $('.overlay_on').css('z-index', 500);
      $('div.search_box').css('z-index',600);//topZIndex();
      
      
      $('div.search_box').parentsUntil('body').each(function(g,par) {
            //debug('parents: '+par.tagName);
            //$(par).topZIndex();
            $(par).css('z-index',600);
         })
      
         
         //var ul_index = $(ul).css('z-index', 500);
   }

   $("div.search_box").animate({ top: '-4px'}, 'slow');
   $('#close_btn').css('display','block');

  
  $.getJSON("search/search.php", {ajax:1,search:search_val_encoded}, function(data){
  
       //debug('ja');
       //debug(data);

       var total_items = 0;
       $("#results_list").empty();        
       $.each(data.items, function(i,item){
         
         //debug('item.page_url: '+item.page_url);
         
         $("<li><a href='"+item.page_url+"'>"+item.page_title+"</a></li>").appendTo("#results_list");

         total_items++;
       });
       
       
       
       $('#results_list a').each(function (i, a) {
         $(a).click(function() {
            //debug('search clicked');
            closeSearch();
         });
       });
       
       //alert("searched.."+total_items);
       
       //debug('data.items: '+data.items);

       var search_txt = '"'+$('#search_input').val()+'"';
      
      $("#last_search").empty();
      //debug('data.items: '+data.items);
      
       if(total_items==0) {
         $("#last_search").append(search_txt+' (0)');
         
         $("<li><a href='"+site_url+"feedback_html.php' target='_blank'>" + TXT_NO_RESULTS_DESC + "</a></li>").appendTo("#results_list");
         
       } else {
         $("#last_search").append(search_txt+' ('+total_items+')');
       }
    
  });

}

function initSearch() {

   /*$.ajaxStart(function() {
   	  //$("div#loading").text("Loading...");
      });
   
      $.ajaxComplete(function() {
   	  //$("div#loading").text("");
      });
      
   */

   $("input#close_btn").click(closeSearch);
   
   $("#search_input").focus(searchReady);
   
   $("input#search_btn").click(function() {
      searchReady();
      doSearch();
   });

}

function searchReady() {
   var val = $("#search_input").val();
   console.log('val: '+val);
   if(val=='Search' || val=='Búsqueda') {
      $("#search_input").attr({ value: '' });
      $("#search_input").attr({ value: '' });
   }
   $("#search_input").removeClass('search_init');
}


/*    END SEARCH SYSTEM   */
/**********************************************************/
/**********************************************************/

function isset(el_str) {
   if($(el_str).length)
      return true;
   return false;
}

DEBUG = is_debug;
//disable in testing env if I want to
DEBUG = false;

function debug(msg) {
   //if(!is_debug) return;
   
   
   $.log(msg);
}




$(document).ready(function() {
   initDropDowns();
   initSearch();
});






