/**
 * Javascript library for template ExtremeMagento
 * @copyright 2007 Quick Solution LTD. All rights reserved.
 * @author Giao L. Trinh <giao.trinh@quicksolutiongroup.com>
 */

(function() {
	
// EM.tools {{{
	
if (typeof BLANK_IMG == 'undefined') 
	var BLANK_IMG = '';

// declare namespace() method
String.prototype.namespace = function(separator) {
  this.split(separator || '.').inject(window, function(parent, child) {
    var o = parent[child] = { }; return o;
  });
};


'EM.tools'.namespace();


// EM0012 {{{
	
function decorateSlideshow2() {
	var $$li = $$('#slideshow2 ul li');
	if ($$li.length > 0) {
		
		// reset UL's width
		var ul = $$('#slideshow2 ul')[0];
		var w = 0;
		$$li.each(function(li) {
			w += li.getWidth();
		});
		ul.setStyle({'width':w+'px'});
		
		// private variables
		var previous = $$('#slideshow2 a.previous')[0];
		var next = $$('#slideshow2 a.next')[0];
		var num = 3;
		var width = ul.down().getWidth() * num;
		var slidePeriod = 3; // seconds
		var manualSliding = false;
		
		// next slide
		function nextSlide() {
			new Effect.Move(ul, { 
				x: -width,
				mode: 'relative',
				queue: 'end',
				duration: 1.0,
				//transition: Effect.Transitions.sinoidal,
				afterFinish: function() {
					for (var i = 0; i < num; i++)
						ul.insert({ bottom: ul.down() });
					ul.setStyle('left:0');
				}
			});
		}
		
		// previous slide
		function previousSlide() {
			new Effect.Move(ul, { 
				x: width,
				mode: 'relative',
				queue: 'end',
				duration: 1.0,
				//transition: Effect.Transitions.sinoidal,
				beforeSetup: function() {
					for (var i = 0; i < num; i++)
						ul.insert({ top: ul.down('li:last-child') });
					ul.setStyle({'position': 'relative', 'left': -width+'px'});
				}
			});
		}
		
		function startSliding() {
			sliding = true;
		}
		
		function stopSliding() {
			sliding = false;
		}
		
		// bind next button's onlick event
		next.observe('click', function(event) {
			Event.stop(event);
			manualSliding = true;
			nextSlide();
		});
		
		// bind previous button's onclick event
		previous.observe('click', function(event) {
			Event.stop(event);
			manualSliding = true;
			previousSlide();
		});
		
		
		// auto run slideshow
		// new PeriodicalExecuter(function() {
		// 			if (!manualSliding) nextSlide();
		// 			manualSliding = false;
		// 		}, slidePeriod);
		
		
	}
}

function decorateSlideshow() {
	var slideshow = $('slideshow1');
	if (slideshow) {
		
		// private variables
		var effectDuration = 1;
		var effectInterval = 6;
		var ul = slideshow.select('.slideshow-box ul')[0];
		var $$li = ul.select('li');
		var width = ul.down('li').getWidth();
		var currentIdx = 0;
		var manualAppear = false;
		
		$$li.invoke('hide');
		$$li[currentIdx].addClassName('current').show();
		
		// store slideshow image index into LI
		for (var i = 0; i < $$li.length; i++) {
			$$li[i].slideshowIdx = i;
			//$$li[i].setAttribute('id', 'slideshow2_'+i);
		}
		
		// generate Navigation
		var nav = slideshow.select('.navigation')[0];
		nav.insert('<ul></ul>');
		var nav_ul = nav.down('ul');
		for (var i = 0; i < $$li.length; i++) {
			var attr = '';
			if (i == currentIdx) var attr = 'class="active"';
			nav_ul.insert('<li><a href="#'+i+' " '+attr+'>'+(i+1)+'</a></li>');
		}
		
		// bind onClick event on navigation A element
		var $$nav_li = nav_ul.childElements();
		nav_ul.select('a').each(function(a) {
			a.observe('click', function(event) {
				Event.stop(event);
				if (a.hasClassName('active')) return;
				
				manualAppear = true;
				
				var current = a.up('li');
				var idx_current = $$nav_li.indexOf(current);	
				
				appear(idx_current);
			});
		});
		
		function appear(i) {
			var li_current = ul.down('li.current');
			var idx_current = $$li.indexOf(li_current);
			
			if (typeof i == 'undefined')
				i = idx_current == $$li.length - 1 ? 0 : (idx_current + 1);
			
			new Effect.Parallel([
				new Effect.Fade(li_current, { sync: true }),
				new Effect.Appear($$li[i], { sync: true })
			], { 
				duration: effectDuration,
				beforeStart: function() {
					Element.removeClassName($$nav_li[idx_current].down('a'), 'active');
					Element.addClassName($$nav_li[i].down('a'), 'active');
				},
				afterFinish: function() {
					Element.removeClassName(li_current, 'current');
					Element.addClassName($$li[i], 'current');
				}
			});
		}
		
		// auto run slideshow
		new PeriodicalExecuter(function() {
			if (!manualAppear) appear();
			manualAppear = false;
		}, effectInterval);
	}
}

document.observe("dom:loaded", function() {
	decorateSlideshow();
	decorateSlideshow2();
});

// }}}

})();
