// -----------------------------------------------------------------------------------

var vbNavigation = Class.create();

vbNavigation.prototype = {


	// initialize()
    	// Constructor runs on completion of the Page loading.     	//
    
    	initialize: function(bg,popout,keyword) {
    		this.bg = bg;
    		this.popout = popout;
		this.keyword = keyword;
    	 
     		this.contentCount = 0;
    		this.carouselCount = 0;
    		this.carouselHeight = 84;
    		this.carouselFullHeight = 129;
    		this.carouselTop = 0;
    		this.carouselBottom = 0;
    		this.carouselTopAfter = 0;
    		this.carouselMoving = false;
    		this.activeCarousel = 0;
    		this.activeContent = 0;
    		this.sliderCount = 0;
    		this.sliderMoving = false;
    	 
    		this.events = {
			mouseover: this.mouseover.bind(this),
			mouseout: this.mouseout.bind(this),
			showContentSection: this.showContentSection.bind(this)
	    	};
	    	
	    	this.browserQuirks();
	 
		this.enhanceCarousel();
		 
	},
	
	
      //
      // browserQuirks()
      // REQUIRES BROWSER.JS FILE
      // Sorts out quirks we have encountered along the way - contains some placeholder conditions for other browsers.
      //

	  browserQuirks: function() {},
	
	showContentSection: function(e, section) { },


	enhanceCarousel: function() {

		if ($('page-carousel')) {
	
			$('page-carousel').absolutize();
			$('page-carousel-padding').setStyle({'display':'block'});
			
		};	

		var carouselItems = $$('#page-carousel .carousel-item .carousel-content');
		
		if(carouselItems.length > 1) {
				
			carouselItems.each(function(s) {
				this.carouselCount++;
				$(s).absolutize();
				this.idCarousel(s);
				this.activateCarousel(s);
				
			}.bind(this));
			
		this.carouselTop = parseInt(carouselItems[0].getStyle('top'));
		carouselDiff = this.carouselFullHeight - this.carouselHeight;
		this.carouselTopAfter = this.carouselTop - carouselDiff;
		this.carouselBottom = this.carouselTop + this.carouselHeight;
		
		}		
	
	
	},
	
	idCarousel: function(element) {
	
	element.writeAttribute("id", "carousel"+this.carouselCount);
	
	},
	
	activateCarousel: function(element) {
	
		imageSrc = element.down().down().src;
		
		imageOverUrl = this.extractRolloverUrl(element);
		this.preloadImage(imageOverUrl);
	
		Event.observe(element,'mouseover',this.mouseoverCarousel.bindAsEventListener(this, element.id));
		Event.observe(element,'mouseout',this.mouseoutCarousel.bindAsEventListener(this, element.id, imageSrc));
		
	},
	
	
	
	mouseoverCarousel: function(e,id) {
	
			mouseoverUrl = this.extractRolloverUrl(id);
			$(id).down().down().src = mouseoverUrl;
			
			currentHeight = $(id).getHeight();
			intendedHeight = this.carouselFullHeight;
			heightRatio = (intendedHeight/currentHeight)*100;
			$(id).setStyle({'top':this.carouselBottom - currentHeight+'px'});
		

			new Effect.BlindDown(id,{duration:0.2,scaleFromBottom:true,scaleFrom:100,scaleTo:heightRatio,restoreAfterFinish:false,afterFinish: function() {
				$(id).setStyle({'top':this.carouselTopAfter+'px','height':this.carouselFullHeight+'px'});
			}.bind(this)});
			
	
	},
	
	mouseoutCarousel: function(e,id,src) {
	
			$(id).down().down().src = src;
	
			currentHeight = $(id).getHeight();
			intendedHeight = this.carouselHeight;
			heightRatio = (intendedHeight/currentHeight)*100;
			$(id).setStyle({'top':this.carouselBottom - currentHeight+'px'});
	
			new Effect.BlindUp(id,{duration:0.2,scaleFromBottom:true,scaleFrom:100,scaleTo:heightRatio,restoreAfterFinish:false,afterFinishInternal:false,afterFinish: function() {
				this.carouselMoving = false; 
				$(id).setStyle({'top':this.carouselTop+'px','height':this.carouselHeight+'px'});
			}.bind(this)});
			
	
	},
	
	extractRolloverUrl: function(id) {
	
			rolloverStr = $(id).down().rev;
			rolloverStrSpl = rolloverStr.split("[");
			rolloverStrSpl = rolloverStrSpl[1].split("]");
			return rolloverStrSpl[0];
	
	},	
	
     
	mouseover: function(event,target) {
	
		window.clearTimeout(this.timeout);
	
		subnavId = target.rel;
		
		if (subnavId == this.activeNavigation) {
		
		return false;
		
		} else {
	
		this.checkActiveNavigation(subnavId);
		$(subnavId).show();
		
		targetLinks = $$('#'+ subnavId +' a')
		
		targetLinks.each(function(s) {
			
				Event.observe(s,'focus',this.subnavMouseover.bind(this));
				Event.observe(s,'blur',this.subnavMouseout.bind(this));	
				
			}.bind(this));
	
		
		Event.observe(subnavId,'mouseover',this.subnavMouseover.bind(this));
		Event.observe(subnavId,'mouseout',this.subnavMouseout.bind(this));

		this.activeNavigation = target.rel;
		
		}

	},
	
	mouseout: function(event,target) {
	
		this.subnavMouseout();
		
	},
	
	subnavMouseover: function() {

		window.clearTimeout(this.timeout);
	
	},
	
	subnavMouseout: function() {

		subnavId = $(this.activeNavigation);
		window.clearTimeout(this.timeout);
		this.timeout = setTimeout(function() {
			this.checkActiveNavigation(subnavId);
			this.activeNavigation = "";
		}.bind(this), this.subnavTime);
	
	},	
	
	 
	
	activatePopoutPanel: function() {
		if ($('main-panel-lozenge-popout')) {
	
		var page = $('page-body').cumulativeOffset();
		var lozenge = $('main-panel-lozenge-popout').cumulativeOffset();

		var left = page[0] - lozenge[0];
		var top = page[1] - lozenge[1];
		$('main-panel-lozenge-popout').setStyle({'backgroundImage':'url('+ this.popout +')','backgroundPosition':left+'px '+ top +'px'});

		}
		
	},
	
	preloadImage: function(imgUrl) {
	
		imageNumber = "imgpl"+this.imgNum
	
		imageNumber = new Image();
		imageNumber.src = imgUrl;
	
		this.imgNum++;
	
	
	},

	        
 
	
	test: function() {
	
	alert("Hello");
	
	}
	
}
