/*
*	HOMEPAGE SLIDESHOW
*	dependencies: imageobserver.js for image preloading
*/
document.observe("dom:loaded", function() {

  var slideshowImageSlides = $$('#slideshowPane .slide');
  var slideshowPane = $('slideshowPane');
  var currentSlide = 0;
  var slideWidth = 960;
  var queueScope = 'slideshow';
  var rotationDelay = 6000;
  
  var numSlides = slideshowImageSlides.length;
  var slideInterval = null;
  var currAnimation = null;
  
  var maxSlides = 5;
  var totalSlidesShown = 1;
 
  
  function changeToSlide(n) {
		var queue = Effect.Queues.get(queueScope);
		//make sure we are not currently animating.
		if(queue.effects.length ==  0) {
			lis = $$('#slideshowNav ul li'); 
			lis.each(function(element){
				element.removeClassName('selected');
			});
			lis[n].addClassName('selected');
			currSlide = slideshowImageSlides[currentSlide];
			currAnimation = new Effect.Fade(currSlide,{duration: 1, from: 1, to: 0, afterFinish: function(){
				new Effect.Appear(slideshowImageSlides[n],{duration: 1, from: 0, to: 1});
				currentSlide = n;
			}});
		}
  }

  function init(){
		var ul = document.createElement('ul');
		$$('#slideshowNav')[0].appendChild(ul);
		
	    slideshowImageSlides.each(function(element,i){
		var li = $(document.createElement('li'));
			//var span = $(document.createElement('span'));
			li.addClassName('default')
			//li.appendChild(span);
			if(i == 0){
				li.addClassName('selected');
				//span.innerHTML = '<img id="dot_image1" src="/media/images/home/small_mouseout_dot.png">';
							
			}
			else if(i == 1) { 
				element.hide();
				//span.innerHTML = '<img id="dot_image2" src="/media/images/home/small_mouseout_dot.png">';
			}
			else if(i == 2) {
				element.hide();
				//span.innerHTML = '<img id="dot_image3" src="/media/images/home/small_mouseout_dot.png">';
			}
			else{element.hide();
				//span.innerHTML = '<img id="dot_image4" src="/media/images/home/small_mouseout_dot.png">';
				}

			ul.appendChild(li);
			li.observe('click', function(){
				li.removeClassName('bigimage');
				if(currentSlide != i){
					changeToSlide(i);
					clearInterval(slideInterval);
					/*slideInterval = setInterval(function(){
						changeToSlide((currentSlide + 1) % numSlides);
					},rotationDelay);*/
				}
			});
			
			  li.observe('mouseover', function(){ 
				if (li.className!="default selected")
			     {
				   li.addClassName('bigimage');
			     }

			});

			li.observe('mouseout', function(){ 
				li.removeClassName('bigimage');
			});
			
			
			
		});
		   

		slideInterval = setInterval(function(){
		    if (maxSlides <= totalSlidesShown) {
                return;
            }
			changeToSlide((currentSlide + 1) % numSlides);
			totalSlidesShown++;
		},rotationDelay);
		
	}

	new ImagesObserver('slideshowPane', {
	 onImagesLoaded: function() { $('slideshowLoading').fade({ duration: 2.0, from: 1, to: 0 });$('slideshowPane').appear({ duration: 2.0 }); init(); }
   });
	
 
});

bb.page.home = {};
document.observe("dom:loaded", function(){
    bb.page.home.init();
});

bb.page.home.init = function() {
    // check for cookie -- if it is not found,
    // display the promo area for 15 s.
    var cookieName = "home_page_seen";
    var len = cookieName.length;
    var cookiePairs = document.cookie.split('; ');
    var homePageSeen = cookiePairs.find( function(cookiePair){
        return cookiePair.substr(0, len) === cookieName;
    });
    if (!homePageSeen) {
        document.cookie = "home_page_seen=true; path=/"
        var openPromos = function(){
            bb.page.openPromos();
        }
        var closePromos = function(){
            if (!isInputFieldActive) {
                bb.page.closePromos();
            }
        }
        setTimeout(openPromos, 500);
        setTimeout(bb.page.closePromos, 15000);
    }
};










