/**
 * 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();


// EM0022 {{{
	
function decorateSlideshow() {
	var $$li = $$('#slideshow ul li');
	if ($$li.length > 0) {
		
		// reset UL's width
		var ul = $$('#slideshow ul')[0];
		var w = 0;
		$$li.each(function(li) {
			w += li.getWidth();
		});
		ul.setStyle({'width':w+'px'});
		
		// private variables
		var previous = $$('#slideshow a.previous')[0];
		var next = $$('#slideshow a.next')[0];
		var num = 1;
		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) previousSlide();
			manualSliding = false;
		}, slidePeriod);*/
		
		
	}
}

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 = 6; // 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><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;
				
				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);*/
			
	}
}


function decorateSlideshow1() {
	var $$li = $$('#slideshow1 ul li');
	if ($$li.length > 0) {
		
		// reset UL's width
		var ul = $$('#slideshow1 ul')[0];
		var w = 0;
		$$li.each(function(li) {
			w += li.getWidth();
		});
		ul.setStyle({'width':w+'px'});
		
		// private variables
		var previous = $$('#slideshow1 a.previous')[0];
		var next = $$('#slideshow1 a.next')[0];
		var num = 1;
		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) previousSlide();
			manualSliding = false;
		}, slidePeriod);*/
		
		
	}
}
function decorateSlideshow3() {
	var $$li = $$('#slideshow3 ul li');
	if ($$li.length > 0) {
		
		// reset UL's width
		var ul = $$('#slideshow3 ul')[0];
		var w = 0;
		$$li.each(function(li) {
			w += li.getWidth();
		});
		ul.setStyle({'width':w+'px'});
		
		// private variables
		var previous = $$('#slideshow3 a.previous')[0];
		var next = $$('#slideshow3 a.next')[0];
		var num = 1;
		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) previousSlide();
			manualSliding = false;
		}, slidePeriod);*/
		
		
	}
}

function loadmenu(){
   
   
   var count=0;  // count ul li
   
   $$(".menu").each(function(elem) {
    $count=(elem.childElements()).length;
	});
	if($count!=0){
		$$(".menu").each(function(elem) {
			    $i=0;
                elem.childElements().each(function(li) {
				li.getElementsBySelector('[class="column"]').each(function(e){
					if(e.childNodes.length !=0)
					{
                        check_column=e.hasClassName('column');
						$set=$count-$i-1;
                        if(check_column==true){
								if($i==0){
									e.addClassName("dropdown_3columns");
								}
								else
								{
									if($i==$count-1){
										e.addClassName("dropdown_3columns align_right last");
										li.addClassName(" right");
									}
									else
										e.addClassName("dropdown_"+$set+"columns");
										if($set==1)
										{
											e.childElements().each(function(f){
												if(typeof f.down('ul')!='undefined')
												{
													f.down('ul').addClassName('levels');	
												}
											});
										}
								}
							}
						}
				});
				
				li.getElementsBySelector('[class="full"]').each(function(e){
					if(e.childNodes.length !=0)
					{
						check_full=e.hasClassName('full');
						if(check_full==true && $i==0 && $i<$count-1)
							e.addClassName(" dropdown_fullwidth_first");
						if(check_full==true && $i==1 && $i<$count-1)
							e.addClassName(" dropdown_fullwidth_item2");
						if(check_full==true && $i==2 && $i<$count-1)
							e.addClassName(" dropdown_fullwidth_item3");
						if(check_full==true && $i==3 && $i<$count-1)
							e.addClassName(" dropdown_fullwidth_item4");
						if(check_full==true && $i==4 && $i<$count-1)
							e.addClassName(" dropdown_fullwidth_item5");
						if(check_full==true && $i==5 && $i<$count-1)
							e.addClassName(" dropdown_fullwidth_item6");
						if(check_full==true && $i==$count-1 &&$i!=0)
							e.addClassName(" dropdown_fullwidth_item_right");
					}		
				});
				$i++; 
			});
                      
		});
	/*
		if($$(".menu .column")!=""){
			$$(".menu").each(function(elem) {
				alert(elem.down('li'));	
			});
		}
	*/
	}
}
 function showshopping(){
$$("a.top-link-cart").invoke("observe", "mouseover", function() {
	$('block-cart').addClassName(' display-cart');
	$('topcartlink').addClassName(' shop');
});
}
function productdetail()
{
	
		if($$('.box-reviews .customer-reviews')=="")
		{
			if($('customer-reviews')!=null)
			{
				$('customer-reviews').addClassName('full-reviews');
			}
		}
		else
		{
			if($('customer-reviews')!=null)
			{
				$('customer-reviews').removeClassName('full-reviews');
				}
		}
	
	if($$('.product-view-group .block-related')=="" && $$('.product-view-group .box-tags')=="" || $$('.product-view-group .box-tags')=="" && $$('.product-view-group .box-up-sell')=="" || $$('.product-view-group .block-related')=="" && $$('.product-view-group .box-up-sell')=="")
	{
		$$('.product-view-group').each(function(e){  e.addClassName('full-box-tags') });
	}
	else
	{
		$$('.product-view-group').each(function(e){  e.removeClassName('full-box-tags') });
	}
	
}
function display()
{
		$$('.block-cart').invoke('observe', 'mouseover', function() {
            $('block-cart').addClassName('display-cart');
			$('topcartlink').addClassName(' shop');		
        })
	  $$('.block-cart').invoke('observe', 'mouseout', function() {
            $('block-cart').removeClassName('display-cart');
			$('topcartlink').removeClassName(' shop');		
        })
		
}
function menu()
{
	var Width_ul=0;
	var Width_li=0;
	var Width_before=0;
	var Width_div=0;
	var Width=0;
    $$(".menu").each(function(elem) {
		Width_ul=elem.getWidth();
		elem.childElements().each(function(li) {
            Width_li=li.getWidth();
			Width=Width_ul-Width_before;
			Width_before+=Width_li;
			$div=li.select('div')[0];
			if(typeof $div != 'undefined'){
				Width_div=$div.getWidth();
				sub=Width_div-Width;
				if(sub>0){
					$div.addClassName(' position-right')
					li.addClassName('position-right-li')
				}
			}
        });
		
	});
}



document.observe("dom:loaded", function() {
    menu();
	decorateSlideshow();
	decorateSlideshow1();
	decorateSlideshow3();
	decorateSlideshow2();
	showshopping();
	display();
	productdetail();
	//loadmenu();
});

// }}}

})();
