/*
	fade-picture.js for mootools v1.4
	(c) 2006-2008 Dinesh Sailor <http://www.dineshsailor.com>
*/

var imgActive 		= '';
var imgCollection 	= '';

/* Call inilization */ 
window.addEvent('domready', function() {

	(function () {

		//$('theme').set('href', themeRoot + '/style.php?id=' + Cookie.read('colorName'));

		/* Get complete image collections */
		imgCollection = $$('#pic_loop img');
		
		if(!imgCollection.length)
			return false;

		/* Get real image loaded through css class */
		var i=1;
		imgCollection.each(function(element) {
			element.set('class', 'pic'+i++);
			element.set('opacity', 0);
		});

		new Fx.Tween($('logo'), {duration: 1000} ).start('opacity', 0, 1);

		/* Set first image visible */
//		imgCollection[0].set('opacity', 1);
		var cfx = new Fx.Tween(imgCollection[0], {duration: 2000} ).start('opacity', 0, 1);
		imgActive	= 0;

		/* If more than 1 image, bind fadein/fadeout */
		if(imgCollection.length > 1) {
			/* Fade all image periodically */
			(function () {
				
				var fadeOut = imgActive++;
				var fadeIn 	= (imgActive >= imgCollection.length ? 0 : imgActive);
				imgActive	= fadeIn;
				
				new Fx.Tween(imgCollection[fadeIn], {duration: 2000} ).start('opacity', 0, 1);
				new Fx.Tween(imgCollection[fadeOut], {duration: 2000} ).start('opacity', 1, 0);
		
			}).periodical(10000);
		}

		/* Perform some common task */
		$$('a._blank').each(function(element,index) {  
			element.set('target', '_blank');
		});
		
		/* some effects... */
		/* Main navigation */
		$$('#menu a').each(function(element) {
			element.addEvents({
				'mouseover': function(e){
					e.stop();
					this.tween('margin-left', 0, 10);
				},
				'mouseout': function(e){
					e.stop();
					this.tween('margin-left', 10, 0);
				}
			});
		});

		/* Site Map */
		$$('#sitemap ul li a').each(function(element) {  
			element.addEvents({
				'mouseover': function(e){
					e.stop();
					this.tween('margin-left', 0, 10);
				},
				'mouseout': function(e){
					e.stop();
					this.tween('margin-left', 10, 0);
				}
			});
		});

		/* Layout changer */
		if($('layoutChanger')) {
			$('layoutChanger').addEvent('click', function() {
	
				var layoutType =  $('wrapper').get('class');
				
				if(layoutType == 'layout-fixed') {
	
					layoutType = 'layout-liquid';
					this.set('title', 'Switch to Narrow Layout');
					
				} else {
					
					layoutType = 'layout-fixed';
					this.set('title', 'Switch to Wide Layout');
				}
				
				$('wrapper').set('class', layoutType);
				Cookie.write('layoutType', layoutType, {duration:15, path:'/'});
			});
			
			if($('wrapper').get('class') == 'layout-fixed')
				$('layoutChanger').set('title', 'Switch to Wide Layout');
			else
				$('layoutChanger').set('title', 'Switch to Narrow Layout');
		}

	}).delay(1000);

	/*
		The page isn't supposed to be fully loaded. The DOM is ready when the browser's internal DOM tree has been created, 
		not neccessarily when the browser has finished rendering. Different browsers will fire domready at different times -- 
		Opera for example can be set to render dynamically, so it will render constantly, whether the dom is ready or not.
	*/
});

function changeColor(themeName, colorName) {
//	var css = new Asset.css(themeRoot + '/' + colorName + '/style.css', { });
//	var css = new Asset.css(themeRoot + '/style.php?id=' + colorName, { });
	$('color').set('href', themeRoot + '/style.php?theme=' + themeName + '&id=' + colorName);
	
	Cookie.write('colorName', colorName, {duration:15, path:'/'});
}
