/*--------------------------------------------------------------

You must declare the path, alt text and 
links for the slideshow in the page. 
JS EXAMPLE:
  var heroPath = new Array();
  heroPath[0] = "/images/home/slides/home-hero-0.jpg";
  heroPath[1] = "/images/home/slides/home-hero-1.jpg";
      
  var heroAlt = new Array();
  heroAlt[0] = "Alt Text 1";
  heroAlt[1] = "Alt Text 2";

  
  var heroLink = new Array();
  heroLink[0] = "http://www.link1.com";
  heroLink[1] = "http://www.link2.com/";

   //first image is loaded when the page loads (true) 
  //next images will not be loaded (false), 
  //end with 'notset' to stop loading.
  var heroLoaded = new Array(true,false,'notset');
  
HTML EXAMPLE:
<div id="heroslide0" class="heroslide"><a href="http://www.link.com"><img src="/images/home/slides/home-hero-0.jpg" alt="Alt Text 1" title="Alt Text 1" /></a></div>

-----------------------------------------------------------*/
$(function() {
  $.ajaxSetup({cache: false});
});

var heroImg = new Array();
var loadHeroX = 1;
var loadHeroN = 0;
var hasController = true;

//Load all the images into divs for the slideshow
var loadSlideShow = function(){
  
  if(loadHeroX < heroPath.length) { 
  loadHeroN = loadHeroX - 1;
  nextHTML = '<div id="heroslide'+loadHeroX+'" class="heroslide"></div>';
    
  //append the next slide div
  $("#heroslide"+loadHeroN).after(nextHTML);
    
  heroImg[loadHeroX] = new Image();
  $(heroImg[loadHeroX]).load(function() { 
    if (hasController) {
      addControllerImage();
    }
    //happens after the image is loaded
    $("#heroslide"+loadHeroX).html(this);
    if(heroLink[loadHeroX] !='') { 
      $(this).wrap('<a href="' + heroLink[loadHeroX] + '"></a>');
    }  
    $(this).attr("alt",heroAlt[loadHeroX]);
    $(this).attr("title",heroAlt[loadHeroX]);
    heroLoaded[loadHeroX] = true;
    loadHeroX = loadHeroX+1;
    loadSlideShow();
  }).attr('src',heroPath[loadHeroX]).error(function () {
       alert('there was an error loading the image: '+heroPath[loadHeroX]);
    });
  }
}

var imgCount = heroPath.length;
var currIdx = 0;
var nextIdx = 1;
var autoplaying = true;
var fadeIsDone = true;

//Begin the crossfading slideshow action
var slideShowAction = function(idx) {
		if (idx != null && idx != undefined) {
			autoplaying = false;
			//alert("not a number:" +idx);
		if ( isNaN(idx) ) {
			//alert("not a number:" +idx);
			if (idx == 'next') {
			nextIdx = currIdx + 1; 
				//alert("got here");
			}
			if (idx == 'prev') {
				if(currIdx == 0) {
					nextIdx = imgCount - 1; 
				} else { 
					nextIdx = currIdx - 1;
				}
			}
		}
		else {
			fadeIsDone = true;
			//alert("slideShowAction called: "+idx+"user interacted");
			nextIdx = idx;
		}
		}
		else if (!autoplaying) {
		//call automatically, but user has taken charge.
			return;
		}
		if (nextIdx >= imgCount) {
			nextIdx = 0;
		}
		switch (heroLoaded[nextIdx]) {
		case true:
			if(fadeIsDone == true) { 
				fadeIsDone = false;
				setDot(nextIdx);
				//Next image is loaded - fade to it
				//alert(nextIdx + " to the top, " + currIdx + " to the back.\n" + "nextIdx is: " + $("#heroslide"+nextIdx).css("z-index") + " \n" + "currIdx is: " + $("#heroslide"+currIdx).css("z-index") + " \n" );
				$("#heroslide"+nextIdx).css("display","none");
				$("#heroslide"+nextIdx).css("z-index","40");
				$("#heroslide"+nextIdx).fadeIn("slow",function(){
					fadeIsDone = true;																									 
					$("#heroslide"+currIdx).css("display","none");
					$("#heroslide"+currIdx).css("z-index","5");
					$("#heroslide"+nextIdx).css("z-index","30");
					//nextIdx = nextIdx+1;
					//if(heroLoaded[nextIdx]=='notset') {
					//  nextIdx = 0;
					//}
					currIdx = nextIdx;
					if (autoplaying) {
						nextIdx++;
						var t = setTimeout(function(){ slideShowAction(); }, 4000);
					}
				});
			}
		break;
		case false:
			//Next image is not loaded - try again
			var t = setTimeout(function(){ slideShowAction(); }, 250);
			break;
		}
}

//Load the slideshow Images and start the slideshow once the window loads
  $(window).load(function(){
    if(heroPath.length > 1) {
      loadSlideShow();
      setTimeout(function(){ slideShowAction(); }, 2000);
    }
  });
  
var setDot = function(idx) {
  $("#slidenav img").removeClass("active");
  $("#slidenav img:eq("+(idx+1)+")").addClass("active");
}
var fadeIsDone = true;
var currentIdx = '';
var goToImg = function(idx) {
	if (fadeIsDone == true){
		if (currentIdx != idx){
			fadeIsDone = false;
			currentIdx = idx;
			slideShowAction(idx-1);
		} 
		else {
			currentIdx = idx;
		}
	}
}

//var spacerImg = '/images/common/blank.gif';
var spacerImg = 'images/common/blank.gif';
var zr_imgCt = 0;
var addControllerImage = function() {
  var ct = document.getElementById("slidenav");
  if (ct.innerHTML =="") {
    var i = new Image;
    i.src = spacerImg;
    i.className = 'leftarrow';
    var y = 0;
    with ({ "y": y }) {
      i.onclick = function () { slideShowAction('prev'); }
    }
    ct.appendChild(i);
    var i = new Image;
    i.src = spacerImg;
    i.className = "dot active";
    i.id = "img"+y;
    with ({ "y": y }) {
      i.onclick = function () { goToImg(1); }
    }
    ct.appendChild(i);
    var i = new Image;
    i.src = spacerImg;
    i.className = 'rightarrow';
    with ({ "y": y }) {
      i.onclick = function () { slideShowAction('next'); }
    }
    ct.appendChild(i);
  }
  var cn = ct.childNodes;
  var cnct = cn.length;
  for (var m=0; m<cnct; m++) {
	if (cn[m].className == "rightarrow") {
      // got right arrow, add new dot before
      var iii = new Image;
      iii.src = spacerImg;
      iii.className = "dot";
      iii.id = "img"+m;
      with ({ "m": m }) {
        iii.onclick = function () { goToImg(m); }
      }
      ct.insertBefore(iii,cn[m]);
    }
  }
}   
    



  
  
  