
// ------------------------ 10util.js starts here -----------------------------
/*              Utility functions                    */

var isIE6 = false;
var isIE7 = false;
/*@cc_on @*/

/*@if (@_jscript_version <= 5.6)
 // The above conditional compilation for IE is equivalent to
 // a conditional comment in HTML of 'if lte IE 6'
 isIE6 = true;
 /*@end @*/

/*@if (@_jscript_version > 5.6)
 // The above conditional compilation for IE is equivalent to
 // a conditional comment in HTML of 'if lte IE 6'
 isIE7 = true;
 /*@end @*/

var loadEventList = [];
function addEvent(obj, eventType, fn){

	if (typeof obj === "string") {
		obj = document.getElementById(obj);
	}

    /* adds an eventListener for browsers which support it
     Written by Scott Andrew: nice one, Scott */
    if (eventType === "load") {
        //hack me
        loadEventList.addLoadEvent(fn);
        return true;
    }
	if (!obj) {
		return null;
	}
    
    if (obj.addEventListener) {
        obj.addEventListener(eventType, fn, false);
        return true;
    }
    else 
        if (obj.attachEvent) {
            var r = obj.attachEvent("on" + eventType, fn);
            return r;
        }
        else {
            return false;
        }
}


var loadEventList = [];
loadEventList.addLoadEvent = function(fn){
    loadEventList[loadEventList.length] = fn;
};

loadEventList.hasFired = false;
loadEventList.fireLoadEvents = function(){
    for (var i = 0; i < loadEventList.length; i++) {
        loadEventList[i]();
    }
    loadEventList.hasFired = true;
};

/* the following is a hack to replicate DOMContentLoaded in browsers
 other than Firefox.  It is basically copied from
 http://dean.edwards.name/weblog/2006/06/again/
 */
if (/WebKit/i.test(navigator.userAgent)) { // Safari
    var _timer = setInterval(function(){
        if (/loaded|complete/.test(document.readyState)) {
            clearInterval(_timer);
            loadEventList.fireLoadEvents(); // call the onload handler
        }
    }, 100);
}
else 
    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", loadEventList.fireLoadEvents, false);
    }
    else {
    // IE HACK
    /*@cc_on @*/
    /*@if (@_win32)
     document.write("<script id='__ie_onload' defer='defer' src='//:'><\/script>");
     var script = document.getElementById("__ie_onload");
     script.onreadystatechange = function() {
	     if (this.readyState == "complete") {
		     loadEventList.fireLoadEvents(); // call the onload handler
	     }
     };
     /*@end @*/
    }

var safeLoadEventList = [];
var isSafari2 = function () {
	 var userAgentNumber = RegExp("( Safari/)([^ ]+)").exec(navigator.userAgent);
	 if (!userAgentNumber || userAgentNumber.length < 3) {
	 	return false;
	 }
	 var mainVersionNumber = userAgentNumber[2].split('.')[0];
	 if (parseInt(mainVersionNumber) > 500) { //This is Safari 3
	 	return false;
	 }
	 return true;
}
function addSafeLoadEvent(fn) {
	if (!(isIE6 || isIE7)) {
		addEvent(document, 'load', fn);
		return true;
	} else {
		// This is IE6 or 7 and therefore can't have document.body.appendChilds or innerHTMLs until the whole page has loaded.
		// See http://support.microsoft.com/kb/927917 for more information
		safeLoadEventList.push(fn);
		return true;
	}
}

safeLoadEventList.hasFired = false;
safeLoadEventList.fireLoadEvents = function() {
    for (var i = 0; i < safeLoadEventList.length; i++) {
        safeLoadEventList[i]();
    }
    safeLoadEventList.hasFired = true;
};

if ((isIE6 || isIE7)) {
	window.attachEvent("onload", safeLoadEventList.fireLoadEvents);
}

/* fold7 javascript starts here*/

function showImage(container_id, n, fade, xfade) {
  var container = $(container_id);
  var oldVisible = container.down('.visible');
  var visible=$('img_'+n);
  if(oldVisible) {
     oldVisible.removeClassName('visible');
  }
  visible.addClassName('visible');
	if(fade) {
	  if(!xfade && oldVisible) {
	    new Effect.Fade(oldVisible, {duration: 1, afterFinish: function() {
	      new Effect.Appear(visible, {duration: 1});
      }})
	  } else {
	    new Effect.Appear(visible, {duration: 1.0, afterFinish: function() {
    	  if(oldVisible) {
    	    oldVisible.hide();
  	    } 
	    }});
	  }
	} else {
	  visible.show();
	  if(oldVisible) {
	    oldVisible.hide();
    }
	}
}

function setupPostImageObservers() {
  var links= $$('#image-sidebar a.image_link');
  links.each(function(link) {
    link.observe('click', function(event) {
      var id = parseInt(link.id.sub("link_", ""))
      showImage("image-area", id, true, true);
      setSelectedLink('image-sidebar', link);
      Event.stop(event)
    });
  });
  setSelectedLink('image-sidebar', links[0]);
}

function setupCategoryImageObservers() {
  var imageLinks = $$('.image_link');
  imageLinks.each(function(link) {
    link.observe('mouseover', function(event) {
      $("link_" + link.id.sub("image_", "")).addClassName('selected');
    });
    link.observe('mouseout', function(event) {
      $("link_" + link.id.sub("image_", "")).removeClassName('selected');
    });
  });
}

function setSelectedLink(container_id, link) {
  var container = $(container_id);
  var oldLink = container.down('.selected');
  oldLink.removeClassName('selected');
  link.addClassName('selected');
  nextLink = container.down('.next_link');
  var links = $$('#' + container_id + ' a.image_link');
  var currentLink = container.down('.selected');
  var currentId = links.indexOf(currentLink);
  var nextId = (parseInt(link.id.sub("link_", "")) < links.length) ? currentId + 1 : 0    
  nextLink.stopObserving('click');
  nextLink.observe('click', function(event) {
    showImage("image-area", nextId + 1, true, true);
    setSelectedLink('image-sidebar', links[nextId]);
    Event.stop(event);
  });
}
/*
function rotateFrontPageImage() {
  var images = $$('#homepage_image .homepage_image');
  showImage("homepage_image", 0 ,true, true);
  window.setTimeout(function() {
    rotationImage = 1;
    window.setInterval(function(){
      showImage("homepage_image", rotationImage, true, true);
      rotationImage = rotationImage == (images.size() - 1) ? 0 : rotationImage + 1;
    }, 5000)
  }, 3000)
};

function rotateTestimonialImage() {
  var images = $$('#testimonial_container .testimonial');
  showImage("testimonial_container", 0 ,true, false);
  window.setTimeout(function() {
    rotationImage = 1;
    window.setInterval(function(){
      showImage("testimonial_container", rotationImage, true, false);
      rotationImage = rotationImage == (images.size() - 1) ? 0 : rotationImage + 1;
    }, 10000)
  }, 8000)
};*/
