//
//


var pics = {
  init: function(){
    pics.img1 = $('main-slide');
		pics.img2 = $('back-slide');
		pics.fade = true;
    setTimeout("pics.launch();",300);
	  pics.pos = 1;
		pics.total = this.getPicsLength();
	  this.setThemeRoot();
  },
  
  launch: function(){
		pics.showing = pics.img1;
		if(!pics.showing) return;
		var src = pics.showing.src.split('/images/');
		//pics.showing.src = src[0] + "/images/mains/001.jpg";
		pics.notShowing = pics.img2;
			$A($('thumbnails').getElementsByClassName('thumb')).each(function(el){
				el.observe('mouseover', pics.mouseOver);
		});
		new Effect.Opacity('right', { from: 0, to: 1, duration: .6 });
		// set the autochange if we're on the homepage slideshow
		//if(window.location.toString().indexOf('home') != -1){
			pics.refreshInterval();
		//}
  },
	
	getPicsLength: function(){
		var l, b = false, loc = window.location.toString();

		l = loc.replace("www.",'');

		if(l.indexOf('home') != -1 ||
			 l == 'http://adventurebase.com/'){
		  return 11;
		} else if(l == 'http://adventurebase.com/snow' ||
			 l == 'http://adventurebase.com/sun' ||
			 l == 'http://adventurebase.com/sea'){
			
		  return 6;
		} else {
		  return 5;
		}
		
	},
	
	setThemeRoot: function(){
	  var r = '', l, loc = window.location.toString();

		l = loc.replace("www.",'');

		if(l == 'http://adventurebase.com/snow' || l == 'http://localhost/adventurebase/snow'){
		  r = 'snow/';
		} else if(l == 'http://adventurebase.com/sun' || l == 'http://localhost/adventurebase/sun'){
		  r = 'sun/';
		} else if(l == 'http://adventurebase.com/sea' || l == 'http://localhost/adventurebase/sea'){
		  r = 'sea/';
		}
		pics.themeRoot = r;
	},
	
	getThemeRoot: function(){
	  return pics.themeRoot;
	},
	
	refreshInterval: function(){
	  clearInterval(pics.interval);
		pics.interval = setInterval(pics.nextImage, 5000);
	},
	
	nextImage: function(){
	  pics.pos++;
		if(pics.pos > pics.total) pics.pos = 1;
		var path = pics.pos.toString();
		path = (path.length == 1)? '00' + path + '.jpg' : '0' + path + '.jpg';
		var src = pics.notShowing.src.split('/slides/');
		var s = src[0] + '/slides/';
		s += pics.getThemeRoot();
		s += path;
		pics.notShowing.src = s;
		if(pics.showing == pics.img1){
			new Effect.Opacity('main-slide', { from: 1, to: 0, duration: 0.8 });
		} else {
			new Effect.Opacity('main-slide', { from: 0, to: 1, duration: 0.8 });
		}
		pics.swapShowing();
		// if we're off the homepage there are thumbnails too need to update the 'on' class
		

		
	},
  
  mouseOver: function(e){
		// reset the autochange if we're not on the homepage slideshow
		if(window.location.toString().indexOf('home') == -1){
			pics.refreshInterval();
		}
		// reset all thumbs to off
		$A($('thumbnails').getElementsByClassName('thumb')).each(function(el){
				el.removeClassName('on');
		});
		var el = Event.element(e);
		el.addClassName('on');
		var src = el.src.split('/thumbnails/');
		number = src[1];
		//number = path[1];
		pics.notShowing.src = src[0] + '/slides/' + number;
		var href = $(pics.notShowing).up().href;
		var hrefpath = href.split('/main/');
		$(pics.notShowing).up().href = hrefpath[0] + '/main/' + number;
		if(pics.showing == pics.img1){
			new Effect.Opacity('main-slide', { from: 1, to: 0, duration: 0.4 });
		} else {
			new Effect.Opacity('main-slide', { from: 0, to: 1, duration: 0.4 });
		}
		pics.swapShowing();
  },
  
  swapShowing: function(){
    if(pics.showing == pics.img1){
			pics.showing = pics.img2;
			pics.notShowing = pics.img1;
		} else {
			pics.showing = pics.img1;
			pics.notShowing = pics.img2;
		}
  }
};

//if(window.location.toString().indexOf('home') ==-1){
  Event.observe(window, "load", pics.init.bindAsEventListener(pics));
//}


