//---------------------------------------
// PUBLIC VARIABLES
//---------------------------------------
var is_ajaxing = null;
//---------------------------------------
// CONSTRUCTORS
//---------------------------------------
$(document).ready(function() {
   // toggles the more_info box on clicking the More Info &darr; link 
   $('.toggle').click(function(){
     if ($(this).next('blockquote').length > 0) {
        $(this).next('blockquote').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     } else if ($(this).next('.toggle_content').length > 0) {
        $(this).next('.toggle_content').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     }
     return false;
   }).toggleClass("down");

   //hide the div's to begin with
   $('.toggle').next().hide().addClass("toggle_content");
   $('.toggle:first').toggleClass("up").toggleClass("down").next().show();


   ($("label.autopopulate").length > 0) ? autopopulateLabels() : "";
   //turn of top margin for first heading tag (if first child of layout)
   var first_child = $("#layout").find("h2:first");
   (first_child.length > 0 && first_child[0].tagName == "H2") ? $(first_child[0]).attr("style","margin-top: 0") : "";
   var first_child = $("#layout").find(".medContentTitle:first");//
   (first_child.length > 0) ? $(first_child[0]).attr("style","margin-top: 0") : "";
   //wrap all hr's in div's
   $("#layout hr").wrap('<div class="hr"></div>');


   //check font-size on-load
   ajax_update({ajaxURL:'ajax/getFontSize.php',callback:{fn:setevents}});//,params:{target:'#contact_contain'}
   //reset Font Size
   $("#txtReset").click(function(){
      $('html').css('font-size', "10"+"px");
      toggleButtons(parseInt(10));
      ajax_update({ajaxURL:'ajax/saveFontSize.php',data:'f=10'});
    });
   // Increase Font Size
    $("#txtLarger").click(function(){
       if($(this).hasClass("active")) {
          var currentFontSize = $('html').css('font-size');
          var currentFontSizeNum = parseInt(currentFontSize, 10);
          var newFontSize = Math.ceil(currentFontSizeNum*1.1);
          $('html').css('font-size', newFontSize + "px");
          toggleButtons(parseInt(newFontSize));
          ajax_update({ajaxURL:'ajax/saveFontSize.php',data:'f='+newFontSize});//,callback:{fn:setevents}}
          return false;
       }
    });
    // Decrease Font Size
    $("#txtSmaller").click(function(){
       if($(this).hasClass("active")) {
          var currentFontSize = $('html').css('font-size');
          var currentFontSizeNum = parseInt(currentFontSize, 10);
          var newFontSize = Math.ceil(currentFontSizeNum*0.9);
          $('html').css('font-size', newFontSize + "px");
          toggleButtons(parseInt(newFontSize));
          ajax_update({ajaxURL:'ajax/saveFontSize.php',data:'f='+newFontSize});
          return false;
       }
    });
});
//---------------------------------------
// PUBLIC METHODS
//---------------------------------------
function _debug ($string) {
   try {
      console.log($string);
   } catch (err) { 
      $("#footer").append($string + "<div class='hr'><hr /></div>"); 
   }
}
//---------------------------------------
function autopopulateLabels() {
   jQuery.each(jQuery("label.autopopulate"), function(){
      //console.log(jQuery(this).next("input[type='text']"));
      jQuery(this).next("input[type='text']").attr("value", jQuery(this).html());
      jQuery(this).next("input[type='text']").attr("title", jQuery(this).html());
      jQuery(this).hide();
      //REMOVE populated label on a focus
      jQuery(this).next("input[type='text']").focus(function () {
         // If value and title are equal on focus, clear value
			if (this.value == this.title) {
				this.value = '';
				this.select(); // Make input caret visible in IE
			}
      });
      //ADDED it back if field is empty
      jQuery(this).next("input[type='text']").blur(function () {
         if (!this.value.length) { this.value = this.title; }
      });
   });
}
//---------------------------------------
function setevents(fontsize) {
   current_size = fontsize.data;
   toggleButtons(parseInt(current_size));
   current_size = (current_size == "txtMedium") ? 10 : current_size;
   $('html').css('font-size', parseInt(current_size)+"px");
}
//---------------------------------------
function li_reveal(target) {
   var new_txt = "";
   var tmp_txt = $(target).text();
   var tmp_txt = tmp_txt.substr(0,tmp_txt.indexOf(" "));

   switch (tmp_txt) {
      case "View":
         new_txt = "<< Hide More";
         $(target).parent().siblings(":hidden").removeClass("hidden").addClass("shown");
      break;
      default:
         new_txt = "View More >>";
         $(target).parent().siblings(".shown").addClass("hidden").removeClass("shown");
      break;
   }
   $(target).text(new_txt);
}
//---------------------------------------
function toggleButtons (fontsize) {
   if (fontsize >= 15) {
      $('#txtSmaller').addClass("active").removeClass("inactive");
      $('#txtLarger').removeClass("active").addClass("inactive");
   } else if (fontsize < 15 && fontsize > 9) {
      $('#txtSmaller').addClass("active").removeClass("inactive");
      $('#txtLarger').addClass("active").removeClass("inactive");
   } else {
      $('#txtSmaller').removeClass("active").addClass("inactive");
      $('#txtLarger').addClass("active").removeClass("inactive");
   }
}
//---------------------------------------
function ajax_update(_atts) {
   var _data = (_atts.data) ? _atts.data : null;
   var _type = (_atts.type) ? _atts.type : "";
   var _ajax_loader_target = (_atts.load_icon) ? _atts.load_icon : null;
   var _ajax_loader_opts = (_atts.load_icon_opts) ? _atts.load_icon_opts : null;
   var _data_type = (_atts.dataType) ? _atts.dataType : "json";
   var _do_ajax = false;


   switch (_type) {
      case "serialize":
         _data = $('#'+_atts.formId).serialize();
         _do_ajax = true;
      break;
      default:
         _do_ajax = true;
      break;
   }


   if (_do_ajax) {
      //toggle_ajax_loader({target:_ajax_loader_target,options:_ajax_loader_opts});
      ajax_in_action = true;

      //abort any open ajax calls
      if(is_ajaxing) {
         is_ajaxing.abort();
      }

      is_ajaxing = $.ajax({
             type: "POST"
            ,url: _atts.ajaxURL
            ,data: _data
            ,cache: false
            ,success: function(data) {
               ajax_in_action = false;
               is_ajaxing = null;

               if(data.error) {
                  //_debug(data.id);
               } else {
                  if(_atts.callback) {
                     (_atts.callback.params) ? _atts.callback.params.data = data : _atts.callback.params = {data: data};
                     (_atts.callback.params && _ajax_loader_target != null) ? _atts.callback.params.load_icon = _ajax_loader_target : "";
                     _atts.callback.fn(_atts.callback.params);
                  }
                  //hide loading icon
                  //toggle_ajax_loader({target:_ajax_loader_target,options:_ajax_loader_opts},true);
               }
            }//end success
      });
   }//end if
}