/*--------------------------------------------------------------------------*
 *  image preload 
 *--------------------------------------------------------------------------*/
$(function() {
	$("img[src*='_off']").each(function(){
		this.offSrc = this.src;
		this.onSrc = this.offSrc.replace("_off.", "_on.");
		preloadImage(this.onSrc);
	});
});
preloadImages = [];
function preloadImage(url){
	var p = preloadImages;
	var l = p.length;
	p[l] = new Image();
	p[l].src = url;
};


/*--------------------------------------------------------------------------*
 *  roll over  
 *--------------------------------------------------------------------------*/
$(document).ready(function() {
  $("img[src*='_on']").addClass("current");
  $("img, input").not("#controls img").mouseover(function() {
    if ($(this).attr("src")) {
      $(this).attr("src",$(this).attr("src").replace("_off.", "_on."));
    }
  });
  $("img[class!='current'], input").not("#controls img").mouseout(function() {
    if ($(this).attr("src")) {
      $(this).attr("src",$(this).attr("src").replace("_on.", "_off."));
    }
  });
});


/*--------------------------------------------------------------------------*
 *  slideshow  
 *--------------------------------------------------------------------------*/
$(function() {

	var dim = 1;

	var count = function() {
		var len = $("#slides").children().length;
		if(dim >= len) {
			dim = 1;
			crossFadeF();
		} else {
			dim += 1;
			crossFade();
		}
	};

	var crossFade = function() {
		$("#slide_" + (dim-1)).fadeOut("slow").next().fadeIn("slow");
    $("#control_" + (dim-1) + " img").attr("src", $("#control_" + (dim-1) + " img").attr("src").replace("_on.", "_off."));
    $("#control_" + dim + " img").attr("src", $("#control_" + dim + " img").attr("src").replace("_off.", "_on."));
	};

	var crossFadeF = function() {
		$("#slides li:last").fadeOut("slow").siblings("li:first").fadeIn("slow");		
    $("#controls li:last img").attr("src", $("#controls li:last img").attr("src").replace("_on.", "_off."));
    $("#controls li:first img").attr("src", $("#controls li:first img").attr("src").replace("_off.", "_on."));
	};

  $("#controls li").hover(function() {
		clearInterval(intervalID);
		var temp = this.getAttribute("id").replace("control_", "");
		var idNum = parseInt(temp);
		$("#slides li:visible").hide();
		$("#slide_" + idNum).show();
    $.each($("#controls img[src*='_on']"), function() {
      $(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
    });
    $("#control_" + idNum + " img").attr("src", $("#control_" + idNum + " img").attr("src").replace("_off.", "_on."));
    dim = idNum;
  }, function() {
		intervalID = setInterval(count, 4000);
  });

  $("#slides li").hover(function() {
		clearInterval(intervalID);
  }, function() {
		intervalID = setInterval(count, 4000);
  });
  
  $("#slides li:gt(0)").hide();
  $("#controls li:first img").attr("src", $("#controls li:first img").attr("src").replace("_off.", "_on."));
	var intervalID = setInterval(count, 4000);
	
});


