/**
 * 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();
function decorateCatalogProductView() {
	if (Element.hasClassName(document.body, 'catalog-product-view')) {
		$$('.short-description h2, .product-collateral h2').each(function(el) {
			el.innerHTML = '<span>'+el.innerHTML+'</span>';
		});
	}
}
/*slideshow bestseller product*/

function decorateSlideshow() {
	$$('.slideshow-products-1').each(function(div) {
		var $$li = $(div).select('.slideshow-b ul li');
		if ($$li.length > 0) {

			// reset UL's width
			var ul = $(div).select('.slideshow-b ul')[0];
			var w = 0;
			$$li.each(function(li) {
				w += li.getWidth();
			});
			ul.setStyle({'width':w+'px'});

			// private variables
			var previous = $(div).select('.slideshow-b a.previous')[0];
			var next = $(div).select('.slideshow-b a.next')[0];
			var num = 4;
			var width = ul.down().getWidth() * num;
			var manualSliding = false;

			// next slide
			function nextSlide() {
				new Effect.Move(ul, { 
					x: -width,
					mode: 'relative',
					queue: 'end',
					duration: 1.5,
					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.5,
					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'});
					}
				});
			}

			// 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();
			});
		}
	});
	
}

/*slideshow new product*/
function decorateSlideshow1() {
	$$('.slideshow-products-2').each(function(div) {
		var $$li = $(div).select('.slideshow-n ul li');
		if ($$li.length > 0) {
		
			// reset UL's width
			var ul = $(div).select('.slideshow-n ul')[0];
			var w = 0;
			$$li.each(function(li) {
				w += li.getWidth();
			});
			ul.setStyle({'width':w+'px'});
		
			// private variables
			var previous = $(div).select('.slideshow-n a.previous')[0];
			var next = $(div).select('.slideshow-n a.next')[0];
			var num =6;
			var width = ul.down().getWidth() * num;
			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'});
					}
				});
			}
			
			// 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();
			});	
		}
	});
}
/*slideshow banner*/
function decorateSlideshow2() {
	var slideshow = $('slideshow2');
	if (slideshow) {

		// private variables
		var ul = slideshow.select('.slideshow-box ul')[0];
		var $$li = ul.select('li');
		var width = ul.down('li').getWidth();
		var slidePeriod = 5; // seconds
		var manualSliding = false;
		var currentIdx = 0;
		
		// reset slideshow UL's width
		ul.setStyle({ width: width*$$li.length+10+'px' });
		
		// 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 == 0) var attr = 'class="active"';
			nav_ul.insert('<li class="cls_'+i+'"><a href="#'+i+' " '+attr+'></a></li>');
		//	nav_ul.insert('<li ><a href="#'+i+' " '+attr+'></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;
				
				manualSliding = true;
				
				var current = a.up('li');
				var active = nav_ul.select('a.active')[0].up('li');
				var idx_current = $$nav_li.indexOf(current);
				var idx_active = $$nav_li.indexOf(active);
				
				if (idx_current > idx_active)
					nextSlide(idx_current - idx_active);
				else
					previousSlide(idx_active - idx_current);
			}.bind(a));
		}.bind(this));
		
		// next slide
		function nextSlide(n) {
			if (typeof n == 'undefined') n = 1;
			
			new Effect.Move(ul, { 
				x: -width*n,
				mode: 'relative',
				//queue: 'end',
				duration: 1.0,
				transition: Effect.Transitions.sinoidal,
				beforeSetup: function() {
					// set current slide indicator
					nav_ul.select('a.active')[0].removeClassName('active');
					nav_ul.down('li', ul.down().next(n-1).slideshowIdx).down('a').addClassName('active');
				},
				afterFinish: function() {
					for (var i = 0; i < n; i++)
						ul.insert({ bottom: ul.down() });
					ul.setStyle('left:0');
				}
			});
		}
		
		// previous slide
		function previousSlide(n) {
			if (typeof n == 'undefined') n = 1;
			new Effect.Move(ul, { 
				x: width*n,
				mode: 'relative',
				//queue: 'end',
				duration: 1.0,
				transition: Effect.Transitions.sinoidal,
				beforeSetup: function() {
					// set current slide indicator
					nav_ul.select('a.active')[0].removeClassName('active');
					var li = ul.down('li:last-child');
					if (n > 1) li = li.previous(n-2);
					nav_ul.down('li', li.slideshowIdx).down('a').addClassName('active');
					
					for (var i = 0; i < n; i++)
						ul.insert({ top: ul.down('li:last-child') });
					ul.setStyle({'position': 'relative', 'left': -width*n+'px'});
				}
			});
		}
		
		// auto run slideshow
		new PeriodicalExecuter(function() {
			if (!manualSliding) nextSlide();
			manualSliding = false;
		}, slidePeriod);
			
	}
}

/* Slide Show for Archive Prod 
*	Params : the first params is a array of the class of div which have slide show.Slide show elements must be wrapped by <ul> tag.Params must have style like ".myclass"
*	Each ele is wrapped by <li> tag	
*/

function initdecorateSlideshowMany() {

	//make sure your array is valided
	var _validClass=new Array();
	for(var i = 0; i < arguments[0].length ; i++ ){
		var myli = $$(arguments[0][i] + " ul li");
		myli.length > 0 ? _validClass.push(arguments[0][i]) : 1;
	}
	//_validClass contain all class have valid li
	
	var num = 1;
	var slidePeriod = 10; // seconds
	var manualSliding = false;
	// next slide
	
	function nextAll() {
		for(var i = 0 ; i < _eleToSlide.length ; i++ ){
		nextSlide(_eleToSlide[i][0],_eleToSlide[i][1]);
		}
	}
	
	function nextSlide(ul,width) {
		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(ul,width) {
		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'});
				}
			});
	}
			
	
	_eleToSlide=new Array();
	for(var i = 0; i < _validClass.length ; i++ ){
	

		//make all selector
		var $$li = $$(_validClass[i] + " ul li");
		var ul = $$(_validClass[i] + ' ul')[0];
		var previous = $$(_validClass[i] + ' a.previous')[0];
		var next = $$(_validClass[i] + ' a.next')[0];

		
		// reset UL's width
		var w = 0;
		$$li.each(function(li) {
			w += li.getWidth();
		});
		ul.setStyle({'width':w+'px'});
		
		// private variables
		var width = ul.down().getWidth() * num;
		
		
		$$li.length > 0 ? _eleToSlide.push([ul,width]) : 1;
		
		// bind next button's onlick event

		$$(_validClass[i] + ' a.next')[0].observe('click', function(event) {
			var index = _validClass.indexOf("." + $(this).up(0).readAttribute('class'));
			Event.stop(event);
			manualSliding = true;
			nextSlide(_eleToSlide[index][0],_eleToSlide[index][1]);
		});
		
		// bind previous button's onclick event
		$$(_validClass[i] + ' a.previous')[0].observe('click', function(event) {
			var mindex = _validClass.indexOf("." + $(this).up(0).readAttribute('class'));
			Event.stop(event);
			manualSliding = true;
			previousSlide(_eleToSlide[mindex][0],_eleToSlide[mindex][1]);
		});	
		
	}

	//auto your slide show
	  new PeriodicalExecuter(function() {
			if (!manualSliding) nextAll();
			manualSliding = false;
		}, slidePeriod);

}

/* End */
function decorateNav() {
	try {
		var LEVEL2_LI_MAX_ROWS = 25;
		var LEVEL0_LU_MAX_WIDTH = 1000;
	
		// stretch out UL.level1
		$$('#nav ul.level0').each(function(ul) {
			ul.setStyle({'width': '1000px'});
		});
	
		// split UL.level2 which have many LI into smaller ULs
		if (LEVEL2_LI_MAX_ROWS > 0) 
			$$('#nav ul.level1').each(function(ul) {
				var li_list = ul.childElements();
				for (var i = LEVEL2_LI_MAX_ROWS; i < li_list.length; i++) {
					var li = li_list[i];
					if (i % LEVEL2_LI_MAX_ROWS == 0) {
						var new_ul = new Element('ul');
						new_ul.addClassName('level1');
						ul.parentNode.appendChild(new_ul);
					}
					new_ul.appendChild(li);
				}
			});
	
		// calculate LI.level1 width
		$$('#nav li.level1').each(function(li) {
			var width = parseInt(li.getStyle('padding-left')) + parseInt(li.getStyle('padding-right')) + 1;
					
			li.childElements().each(function(ul) {
				if (ul.tagName == 'UL' || ul.tagName == 'OL' || ul.tagName == 'DIV')
					width += ul.getWidth();
			});
		
			width = Math.max(width, li.getWidth());
			li.setStyle({'width': width+'px'});
		});
	
		// add class 'clear' to some li.level1
		// and set max width of UL.level0
		if (LEVEL0_LU_MAX_WIDTH > 0)
			$$('#nav ul.level0').each(function(ul) {
				var max_width = 0;
				var spacing_width = parseInt(ul.getStyle('padding-left')) + parseInt(ul.getStyle('padding-right')) + 1;
				var width = spacing_width;
				ul.childElements().each(function(li) {
					if (width + li.getWidth() > LEVEL0_LU_MAX_WIDTH) {
						li.addClassName('clear');
						max_width = Math.max(max_width, width);
						width = spacing_width;
					}
					width += li.getWidth();
				});
				max_width = Math.max(max_width, width);
			
				// set ul.level0 width as well
				ul.setStyle({'width': max_width+'px'});
			});
	
		// add class 'first' or 'last', 'parent' to LI
		$$('#nav', '#nav ul').each(function(ul) {
			var first_li = null;
			var last_li = null;
			ul.childElements().each(function(li) {
				if (li.tagName == 'LI') {
					if (first_li == null) first_li = li;
					last_li = li;
				}
				if (li.down('ul')) li.addClassName('parent');
			});
			if (first_li) first_li.addClassName('first');
			if (last_li) last_li.addClassName('last');
		});
	
		// add class 'first' or 'last' to UL, OL, DIV
		$$('#nav li').each(function(li) {
			var first_ul = null;
			var last_ul = null;
			li.childElements().each(function(ul) {
				if (ul.tagName == 'UL' || ul.tagName == 'UL' || ul.tagName == 'DIV') {
					if (first_ul == null) first_ul = ul;
					last_ul = ul;
				}
			});
			if (first_ul) first_ul.addClassName('first');
			if (last_ul) last_ul.addClassName('last');
		});
	
		// add class to align-right to the mega menu UL.level0 if neccessary 
		var right = $('nav').cumulativeOffset()[0] + $('nav').getWidth();
		$$('#nav ul.level0').each(function(ul) {
			if (ul.up().cumulativeOffset()[0] + ul.getWidth() > right)
				ul.addClassName('a-right');
		});
	} catch (e) {};
}
function decorateMenuWidth() {	
var k;
var ul = document.getElementById('newproduct');
var nummer_li=ul.getElementsByTagName('li').length;

var divheight=0;
	var newcontent = $('newcontent1');
	var divmax=newcontent.down('h3').getHeight()+newcontent.down('h4').getHeight();
	for(k=2;k<=nummer_li;k++)
	{
	
	 newcontent = $('newcontent'+k);
	 divheight=newcontent.down('h3').getHeight()+newcontent.down('h4').getHeight();
	 
		if(divmax < divheight)
			{	
				divmax=divheight;
			}
	}
	var heightdiv=divmax+150;
	var newproduct = $('newproduct');
	var newproductli = $$('#newproduct li');
	newproductli.each(function(li) {	
	li.down('.newcontent').setStyle('height:'+heightdiv+'px');	
	});
	
}
 document.observe('dom:loaded', function() {		
	 function main() {
	//	  initdecorateSlideshow(".slideshow11",true);
	//	  initdecorateSlideshow(".slideshowbrand",true);
		 initdecorateSlideshowMany([".slideshow11",".slideshowbrand"]);

	}
	if (Prototype.Browser.IE && Prototype.Version < '1.6.1')
		window.setTimeout(main, 100);
	else
		main();
});

document.observe('dom:loaded', function() {		
	function main() {
		decorateNav();
		decorateCatalogProductView();
		decorateSlideshow();
		decorateSlideshow1();
		decorateSlideshow2();
		decorateMenuWidth();
	}
	// work around dom:loaded bug on IE8
	if (Prototype.Browser.IE && Prototype.Version < '1.6.1')
		window.setTimeout(main, 100);
	else
		main();
});

})();



