// javascript for rotating header images 
var rotating_photos_timers = new Array();
var rotating_photos_containers = new Array();
var rotate_photo_delay = 6500;

function getRotatingPhotoIndex(container_id) {
  var photo_index = 0;
  var i = 0;
  while (i < rotating_photos_containers.length) {
    if (rotating_photos_containers[i] == container_id) {
      photo_index = i;
      break;
    }
    i++;
  }
  return photo_index;
}

function initRotatingPhotos(container_id) {
  $(function() {
    rotating_photos_timers[rotating_photos_timers.length] = 0;
    rotating_photos_containers[rotating_photos_containers.length] = container_id;
    
    // bind onclick events to all thumbnail images
    resetRotatingPhotosClickEvents(container_id);

    // start image rotation
    rotating_photos_timers[rotating_photos_timers.length] = setTimeout("rotatePhotos('"+container_id+"')",rotate_photo_delay);
  }); 
}
function resetRotatingPhotosClickEvents(container_id) {
  // unbind onlick for main photos
  $("#" + container_id + " .main_image img").click(function () {});
  
  // bind onclick for thumbnails
  $("#" + container_id + " .thumb_images img").click(function () { 
    showRotatingPhoto(this,container_id);
  });
}
function stopPhotoRotation(container_id) {
  var timer_index = getRotatingPhotoIndex(container_id);
  clearTimeout(rotating_photos_timers[timer_index]);
}
function showRotatingPhoto(obj,container_id) {
  // stop the rotation
  stopPhotoRotation(container_id);
  
  // get class of clicked image position
  var class_id = $(obj).parent().parent().parent().attr("class");
  
  // swap current thumbnail foreground with main foreground
  var main_foreground = $("#" + container_id + " .main_img_fadein").html();
  $("#" + container_id + " .main_img_fadein").html($("#" + container_id + " ." + class_id + " div.fadein_thumb").html());
  $("#" + container_id + " ." + class_id + " div.fadein_thumb").html(main_foreground);

  // copy all foreground images to backgrounds, including main
  $("#" + container_id + " .main_img").html($("#" + container_id + " .main_img_fadein").html());
  $("#" + container_id + " .thumb_1 div.current_thumb").html($("#" + container_id + " .thumb_1 div.fadein_thumb").html());
  $("#" + container_id + " .thumb_2 div.current_thumb").html($("#" + container_id + " .thumb_2 div.fadein_thumb").html());
  $("#" + container_id + " .thumb_3 div.current_thumb").html($("#" + container_id + " .thumb_3 div.fadein_thumb").html());
  
  resetClickEvents(container_id);
  
  var timer_index = getRotatingPhotoIndex(container_id);
  rotating_photos_timers[rotating_photos_timers.length] = setTimeout("rotatePhotos('"+container_id+"')",rotate_photo_delay);
}


function rotatePhotos(container_id) {
  
  // copy thumbnail foreground images to background
  $("#" + container_id + " .thumb_1 div.current_thumb").html($("#" + container_id + " .thumb_1 div.fadein_thumb").html());
  $("#" + container_id + " .thumb_2 div.current_thumb").html($("#" + container_id + " .thumb_2 div.fadein_thumb").html());
  $("#" + container_id + " .thumb_3 div.current_thumb").html($("#" + container_id + " .thumb_3 div.fadein_thumb").html());
  // hide foregrounds on thubmails
  $("#" + container_id + " .thumb_images .fadein_thumb").hide();
  
  // copy main faded image into background
  $("#" + container_id + " .main_img").html($("#" + container_id + " .main_img_fadein").html());
  // hide the foreground
  $("#" + container_id + " .main_img_fadein").hide();
  
  // move main image to first thumb position
  $("#" + container_id + " .thumb_1 div.fadein_thumb").html($("#" + container_id + " .main_img_fadein").html());
  // move first thumb to second thumb position
  $("#" + container_id + " .thumb_2 div.fadein_thumb").html($("#" + container_id + " .thumb_1 div.current_thumb").html());
  // move second thumb to third thumb position
  $("#" + container_id + " .thumb_3 div.fadein_thumb").html($("#" + container_id + " .thumb_2 div.current_thumb").html());
  // now move last image into hidden foreground
  $("#" + container_id + " .main_img_fadein").html($("#" + container_id + " .thumb_3 div.current_thumb").html());
  
  resetRotatingPhotosClickEvents(container_id);
  
  // now fade in the new foreground
  $("#" + container_id + " .main_img_fadein").fadeIn("slow");
  // now fade in the new thumbnail foregrounds
  $("#" + container_id + " .thumb_images .fadein_thumb").fadeIn("slow");
  
  var timer_index = getRotatingPhotoIndex(container_id);
  rotating_photos_timers[rotating_photos_timers.length] = setTimeout("rotatePhotos('"+container_id+"')",rotate_photo_delay);
}


// javascript for selecting provinces/states
function setFormProvinceState() {
  var country = $("#mm_contact_form-input-primary_address_country").val();
  if (country=="CA") {
    $("#mm_contact_form-form-item-state").hide();
    $("#province_placeholder").hide();
    $("#mm_contact_form-form-item-province").show();
  }
  else if ((country=="US")||(country=="UM")) {
    $("#mm_contact_form-form-item-province").hide();
    $("#province_placeholder").hide();
    $("#mm_contact_form-form-item-state").show();
  }
  else {
    $("#mm_contact_form-form-item-province").hide();
    $("#mm_contact_form-form-item-state").hide();
    $("#province_placeholder").show();
    $("#mm_contact_form-input-primary_address_state").val("");
  }
}
function setSugarState(obj) {
  // set hidden value to value of selected state/province
  var state = $(obj).val();
  $("#mm_contact_form-input-primary_address_state").val(state);
}

function mmInitPageEditor() {
  $(function() {
    // set hover for content blocks
    $(".content_block_editor").hover(
      function () {
        $(this).find(".content_block_links").show();
      }, 
      function () {
        $(this).find(".content_block_links").hide();
      }
    );

  }); 
}

// global onready inits
$(function() {
  // expland left menus
  $("#secondary_menu li.active > ul, #secondary_menu li.active-trail > ul").show();
}); 

// open all external links in new window
if (typeof $ != 'undefined') {
  $(document).ready(function() {
    // Strip the host name down, removing subdomains or www.
    var host = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
    var subdomain = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$1');

    // Determine what subdomains are considered internal.
    //if (Drupal.settings.extlink.extSubdomains) {
    if (1) {
      var subdomains = "([^/]*)?";
    }
    else if (subdomain == 'www.' || subdomain == '') {
      var subdomains = "(www\.)?";
    }
    else {
      var subdomains = subdomain.replace(".", "\.");
    }

    // Build regular expressions that define an internal link.
    var internal_link = new RegExp("^https?://" + subdomains + host, "i");

    // Find all links which are NOT internal and begin with http (as opposed
    // to ftp://, javascript:, etc. other kinds of links.
    // When operating on the 'this' variable, the host has been appended to
    // all links by the browser, even local ones.
    // In jQuery 1.1 and higher, we'd us a filter method here, but it is not
    // available in jQuery 1.0 (Drupal 5 default).
    var external_links = new Array();
    var mailto_links = new Array();
    $("a").each(function(el) {
      try {
        var url = this.href.toLowerCase();
        if (url.indexOf('http') == 0 && !url.match(internal_link)) {
          external_links.push(this);
        }
        else if (url.indexOf('mailto:') == 0) {
          mailto_links.push(this);
        }
      }
      // IE7 throws errors often when dealing with irregular links, such as:
      // <a href="node/10"></a> Empty tags.
      // <a href="http://user:pass@example.com">example</a> User:pass syntax.
      catch(error) {
        return false;
      }
    });
    
    var extlink_class = 'ext';
    if (extlink_class) {
      // Apply the "ext" class to all links not containing images.
      if (parseFloat($().jquery) < 1.2) {
        $(external_links).not('[img]').addClass(extlink_class);
      }
      else {
        $(external_links).not($(external_links).find('img').parents('a')).addClass(extlink_class);
      }
    }
    
    var mailto_class = 'mail';
    if (mailto_class) {
      // Apply the "mailto" class to all mailto links not containing images.
      if (parseFloat($().jquery) < 1.2) {
        $(mailto_links).not('[img]').addClass(mailto_class);
      }
      else {
        $(mailto_links).not($(mailto_links).find('img').parents('a')).addClass(mailto_class);
      }
    }
    
    var extlink_target = '_blank';
    if (extlink_target) {
      // Apply the target attribute to all links.
      $(external_links).attr('target', extlink_target);
    }
  });
}


