// $Id: googleanalytics.js,v 1.3.2.8 2009/03/04 07:25:47 hass Exp $

Drupal.behaviors.gaTrackerAttach = function(context) {

  // Attach onclick event to all links.
  $('a', context).click( function() {
    var ga = Drupal.settings.googleanalytics;
    // Expression to check for absolute internal links.
    var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
    // Expression to check for special links like gotwo.module /go/* links.
    var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
    // Expression to check for download links.
    var isDownload = new RegExp("\\.(" + ga.trackDownloadExtensions + ")$", "i");

    try {
      // Is the clicked URL internal?
      if (isInternal.test(this.href)) {
        // Is download tracking activated and the file extension configured for download tracking?
        if (ga.trackDownload && isDownload.test(this.href)) {
          // Download link clicked.
          var extension = isDownload.exec(this.href);
          pageTracker._trackEvent("Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, ''));
        }
        else if (isInternalSpecial.test(this.href)) {
          // Keep the internal URL for Google Analytics website overlay intact.
          pageTracker._trackPageview(this.href.replace(isInternal, ''));
        }
      }
      else {
        if (ga.trackMailto && $(this).is("a[href^=mailto:]")) {
          // Mailto link clicked.
          pageTracker._trackEvent("Mails", "Click", this.href.substring(7));
        }
        else if (ga.trackOutgoing) {
          // External link clicked.
          pageTracker._trackEvent("Outgoing links", "Click", this.href);
        }
      }
    } catch(err) {}
  });
}
;

function MM_showHideLayers() { //v3.0
        
   var i,p,obj,args=MM_showHideLayers.arguments;
   
   var css_selector = '#' + args[0];
   var v = args[2];
      
   if (v=='show') {
     $(css_selector).show();
   } else {
     $(css_selector).hide();
   }
                
}


jQuery.cookie = function(name, value, options) {
  if (typeof value != 'undefined') { // name and value given, set cookie
      options = options || {};
      if (value === null) {
          value = '';
          options.expires = -1;
      }
      var expires = '';
      if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
          var date;
          if (typeof options.expires == 'number') {
              date = new Date();
              date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
          } else {
              date = options.expires;
          }
          expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
      }
      // CAUTION: Needed to parenthesize options.path and options.domain
      // in the following expressions, otherwise they evaluate to undefined
      // in the packed version for some reason...
      var path = options.path ? '; path=' + (options.path) : '';
      var domain = options.domain ? '; domain=' + (options.domain) : '';
      var secure = options.secure ? '; secure' : '';
      document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  } else { // only name given, get cookie
      var cookieValue = null;
      if (document.cookie && document.cookie != '') {
          var cookies = document.cookie.split(';');
          for (var i = 0; i < cookies.length; i++) {
              var cookie = jQuery.trim(cookies[i]);
              // Does this cookie string begin with the name we want?
              if (cookie.substring(0, name.length + 1) == (name + '=')) {
                  cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                  break;
              }
          }
      }
      return cookieValue;
  }
};


Drupal.behaviors.achieve_theme = function(context) { 

  //Hide text resize for IE. It's also hidden from CSS, but this is safeguard kindof.  
  //if($.browser.msie) {
  //  $('#text_size_adjust').hide();   
  //}
  
  //------ SEARCH FIELD  -------
  
  $(".search_button img", context).click(function(e) {
    e.preventDefault();    
    redirectToSearch();
    return false;
   });
   
   
  $(".search_form_field", context).keydown(function(e){
    	
    if (e.keyCode == 13) { 
      e.preventDefault();    
      redirectToSearch();            
      return false;
    }
    
  });
   
  function redirectToSearch() {
    var dest_url = $('.search_form_field').val();
    document.location.href = '/search/node/' + dest_url;
  }


  //------ SUBSCRIPTION FIELD  -------
  $(".subscription_button img", context).click(function(e) {
    e.preventDefault();    
    redirectToSubscription();
    return false;
   });
   
   
  $("#subscription_textfield", context).keydown(function(e){
    	
    if (e.keyCode == 13) { 
      e.preventDefault();    
      redirectToSubscription();            
      return false;
    }
  });
   
  function redirectToSubscription() {
    var email = $('#subscription_textfield').val();
    document.location.href = '/subscribe/?email=' + email;
  }
  

  /*********** Text size functions ***********************/  
  //disabled
  //cookieFontSize();
  //$("#text_smaller", context).click(smaller);
  //$("#text_bigger", context).click(bigger);
  
  var offset = 0;


  
  function smaller() {
    return; //disabled
    if($.browser.msie) return; //disable for IE
  	setSizes( -1 );
  	updateCookie(-1);
  }
  
  function bigger() {
    return; //disabled
    if($.browser.msie) return; //disable for IE
  	setSizes( 1 );
  	updateCookie(1);
  }
  
  function cookieFontSize() {
    if($.browser.msie) return; //disable for IE
  	my_offset = $.cookie('achieve_font_size_offset');
  	if(my_offset){
  		setSizes(my_offset);
  	}
  }
  
  function updateCookie(off) {
  	var offset = $.cookie('achieve_font_size_offset');
  	var result = 0;
  	// zero is a valid size
  	if(offset > -1) { result = offset*1 + off*1; }
  	else { result = 1; }
  	$.cookie('achieve_font_size_offset', result, {expires: 365});
  }
  
  /**
   * @param Number offset The change in font size, negative smaller.
   * @param Number max The number of css classes to change 
   */
  function setSizes(off) {
  
    // Array of css classes to change when resizing text 
    var classes = new Array( "#content_wrapper p", "#content_wrapper a", "#achieve_notes a", "#achieve_notes p", "h5", "h4", "h3", "h2", "h1", "h6", ".block-block p", ".block p", ".block-block a", ".block a", ".content-front h2");
    var css_class_count = classes.length;
  
  	for(i=0; i < css_class_count; i++){
  		setFontSize(classes[i], off);
  	}
  }
  
  function setFontSize(id, off) {
  	if($(id).size()){
  		size = $(id).css('font-size');
//  		dimHeight = $(id).innerHeight();
  		parts = size.split('px');
  		new_size = parts[0]*1 + off*1;
//  		alert (id + ' - ' + size + ' - ' + new_size + ' # ' + dimHeight);  		
  		$(id).css('font-size', new_size.toString() + "px");
  		return new_size;
  	}
  }
  
}
 
function changeName(val){
  document.getElementById('sn').innerHTML = val;
}


;

