YAHOO.util.Event.onContentReady("topmenu", function () {
	Interface.init();
});

var Interface = {
	frames: new Array(),
	frame: null,
	delay: 5000, // delay between switching the frames, in milliseconds

	// Basic initialization - render the menu, bootstrap backgrounds preloading if we're on the main page
	init: function() {
		this.initMenus();

		if (YAHOO.util.Dom.get('splash')) {
			// Splash part of the main page detected - start the backgrounds preloading
			//this.Preloader.onImgReady.subscribe(this.addFrame);
			this.Preloader.start();
			// and init product tooltips
			this.initProductInfo();
		}
	},

	// Initializes product info tooltips
	initProductInfo: function() {
		var products = YAHOO.util.Dom.getChildren('producttitles');

		for (i in products) {
			YAHOO.util.Event.addListener(products[i].id, 'mouseover', this.showProductTooltip);
			YAHOO.util.Event.addListener(products[i].id, 'mouseout', this.hideProductTooltip);
		}
	},

	showProductTooltip: function(event) {
		this.className = 'product active';
		YAHOO.util.Dom.get('splash').className = 'active';
		Interface.hideAllInfoBlocks();
		Interface.setDisplayBlock('i' + this.id.substr(1, 100));
	},

	hideProductTooltip: function(event) {
		this.className = 'product';
		YAHOO.util.Dom.get('splash').className = '';
		Interface.hideAllInfoBlocks();
		Interface.setDisplayBlock('default-text');
	},

	hideAllInfoBlocks: function() {
		var infos = YAHOO.util.Dom.getChildren('productinfo');
		for (var i in infos) {
			infos[i].style.display = 'none';
		}
	},

	setDisplayBlock: function(id) {
		YAHOO.util.Dom.get(id).style.display = 'block';
	},

	// Renders the main menu with YUI's MenuBar
	initMenus: function() {
		if (YAHOO.util.Dom.get('topmenu-body')) {
			// Top menu is present, so let's render it
			var oMenuBar = new YAHOO.widget.MenuBar( "topmenu", {
				trigger: document,
				lazyload: true,
				autosubmenudisplay: true
			});
			oMenuBar.render();
		}
	},

	// Event handler that adds a preloaded frame to the list of
	// frames available for switching
	addFrame: function(event, bgname) {
		// Get the div containing our next frame and assign background
		// image to it so it will show up when the div is shown up
		var framediv = YAHOO.util.Dom.get('frame'+Interface.frames.length);
		framediv.style.backgroundImage = 'url("'+bgname+'")';
		// and add new image to the list of available frames
		Interface.frames.push(bgname);

		if (Interface.frames.length == 1) {
			// We've pushed in the first frame, so let's show first frame and
			// start timer-triggered switching
			Interface.frame = 0;
			var fadefirst = new YAHOO.widget.Effects.Appear('frame0');
			fadefirst.onEffectComplete.subscribe(Interface.waitForFrameSwitch);
		};
	},

	// Switches to a next available frame in the framelist, if any.
	// If there's only one frame available - no switching will be performed
	// to avoid unneeded flickering.
	switchFrame: function() {
		if (Interface.frames.length < 2) { Interface.waitForFrameSwitch(); return; } // Check for a single frame

		// Moving to the next frame
		var frame_old = Interface.frame++;
		if (Interface.frame >= Interface.frames.length) Interface.frame = 0; // Looping back to frame zero
		// Fade out the old frame first
		var fadeout = new YAHOO.widget.Effects.Fade('frame'+frame_old);
		fadeout.onEffectComplete.subscribe(function() {
			// And when it's done - fade in the new one
			var fadein = new YAHOO.widget.Effects.Appear('frame'+Interface.frame);
			// when the frame is ready - start waiting to switch to a next frame
			fadein.onEffectComplete.subscribe(Interface.waitForFrameSwitch);
		});
	},

	// Sets a timeout trigger to start switching to a new frame after given period of time
	waitForFrameSwitch: function() {
		setTimeout('Interface.switchFrame();', Interface.delay);
	}

};

// Class implementing basic images preloading functionality.
// Images are given in imgnames array and will be loaded one after another,
// firing Interface.Preloader.onImgReady event after each one
Interface.Preloader = {
	onImgReady: new YAHOO.util.CustomEvent('onImgReady'),

	imgnames: [
	'/images/elements/splash-product.jpg'
	, '/images/elements/header-bg-daclar.gif'
	, '/images/elements/header-bg-drylar.gif'
	, '/images/elements/header-bg-others.gif'
	, '/images/elements/header-bg-vezlar.gif'
	, '/images/elements/header-bg-voxlar.gif'
	, '/images/elements/header-bg-zavlar.gif'
	, '/images/elements/top-line-daclar.gif'
	, '/images/elements/top-line-drylar.gif'
	, '/images/elements/top-line-others.gif'
	, '/images/elements/top-line-vezlar.gif'
	, '/images/elements/top-line-voxlar.gif'
	, '/images/elements/top-line-zavlar.gif'
	],

	// Starts the preloading process
	start: function() {
		//this.onImgReady.subscribe(this.initNextFrame);
		this.loadNextImage();
	},

	// Actually performs the preloading by creating new img object and pulling an image
	// into it. Will pop entries out of the imgnames array and will skip everything is
	// the array is already empty.
	loadNextImage: function() {
		if (this.imgnames.length == 0) return;
		var bgname = this.imgnames.pop();
		var img = new Image();
		YAHOO.util.Event.addListener(img, 'load', function() { Interface.Preloader.onImgReady.fire(this.src); });
		img.src = bgname;
	},

	// Handle the image being loaded, actually by just restarting the loading process
	// to fetch the next image
	initNextFrame: function(event) {
		Interface.Preloader.loadNextImage();
	}
};

// Class to encapsulate operations on announce block
Interface.Announce = {

	fadein: function() {
		var ann = YAHOO.util.Dom.get('announce');
		if (ann) {
			ann.style.opacity = '0.8';
			ann.style.filter = 'alpha(opacity=80)';
		}
	},

	fadeout: function() {
		var ann = YAHOO.util.Dom.get('announce');
		if (ann) {
			ann.style.opacity = '0.5';
			ann.style.filter = 'alpha(opacity=50)';
		}
	}
};
