/*
	Manage common elements in shop pages
	as footer popups and header navigation
*/

// link to footer popup array 
var footerPopupFor = { "shipping-popup-shadow-link" : "shipping-popup-shadow",
					   "payment-popup-shadow-link" : "payment-popup-shadow",
					   "returns-popup-shadow-link" : "returns-popup-shadow",
					   "orders-popup-shadow-link" : "orders-popup-shadow",
					   "infoscenter-popup-shadow-link" : "infoscenter-popup-shadow"
					 };

function addShadow() {
	if ($('eshop-home-content')) $('eshop-home-content').addClass('shadow');
	if ($('productfinder-content')) $('productfinder-content').addClass('shadow');
	if ($('eshop-footer')) $('eshop-footer').addClass('shadow');
}

function topnavPopupFor(item){
	return item+"-menu-container";
}

function getLocalizedText(text){
	if ($('localization') && $('localization').getElement('.'+ text)) {
		return $('localization').getElement('.'+ text).get('text');
	} else {
		return '';
	}
}

window.addEvent('loginready', function(){

	// define animation duration
	var morphDuration = 250;

	// set animation for each footer popup element as a loop
	$$('#eshop-footer ul li').each(function(item,index) {
		$(footerPopupFor[item.id]).setStyles({
			'opacity':0,
			'display':'none'
		});

		item.addEvent('click',function(){
			if($(footerPopupFor[item.id]).getStyle('opacity') == 1){
				var myEffect2 = new Fx.Morph($(footerPopupFor[item.id]), {duration: morphDuration, transition: Fx.Transitions.linear});
				myEffect2.start({
					'opacity': 0,
					'display':'none'
				});				
			} else {
				$$('#eshop-footer-popups ul li.main-footer-popups').each(function(item,index) {
					if(item.getStyle('opacity') == 1){
						var myEffect2 = new Fx.Morph(item, {duration: morphDuration, transition: Fx.Transitions.linear});
						myEffect2.start({
							'opacity': 0,
							'display':'none'
						});	
					}
					item.addEvent('click',function(e){
						e.stopPropagation();
					});	
				});
				var myEffect1 = new Fx.Morph($(footerPopupFor[item.id]), {duration: morphDuration, transition: Fx.Transitions.linear, onComplete: function () {
					$(document.body).addEvent('click',function(){
						$$('#eshop-footer-popups ul li.main-footer-popups').each(function(item,index) {
							if(item.getStyle('opacity') == 1){
								var myEffect2 = new Fx.Morph(item, {duration: morphDuration, transition: Fx.Transitions.linear});
								myEffect2.start({
									'opacity': 0,
									'display':'none'
								});						
							}
							item.addEvent('click',function(e){
								e.stopPropagation();
							});
						});
						$(document.body).removeEvent('click');
					});
				}});
				myEffect1.start({
					'opacity': 1,
					'display':'block'
				});
			}
		});
		
		$$('.eshop-footer-popup-close').addEvent('click',function(){
				var myEffect1 = new Fx.Morph(footerPopupFor[item.id], {duration: morphDuration, transition: Fx.Transitions.linear});
				myEffect1.start({
					'opacity': 0,
					'display':'none'
				});						
		})
	});
});

window.addEvent('load',function(){
	// set animation for each nav popup element as a loop
	var morphDuration = 250;
	var activeMenu = null;
	var topnavOver = false;
	$$('#eshop-topnav ul li.topnav-menuitem').each(function(item,index) { // display/masks the menu. Masks all other menus if displayed
			// gets coord of popup (we need the height before it is set to zero) 
			var coordPopup = $(topnavPopupFor(item.id)).getCoordinates($('eshop-content'));

			$(topnavPopupFor(item.id)).setStyles({
				'opacity':1,
				'overflow':'hidden',
				'height':0
			});

			// sets popup position			
			var coord = item.getPosition($('eshop-content'));
			$(topnavPopupFor(item.id)).setStyles({
				'left':coord.x.toInt()+5+"px",
				'top':coord.y.toInt()+35+"px"
			});
			item.addEvent('mouseenter', function() { // event on rollover on nav elements (links)
				(function() {
					if (activeMenu && activeMenu == item.id) {
						var actualObj = item;
						if($(topnavPopupFor(item.id)).getStyle('height') == '0px'){
							$$('#eshop-topnav-menus ul li.eshop-topnav-container').each(function(item,index) {
								
								if(item != $(topnavPopupFor(actualObj.id))) { // applies effect only for other elements than main
									var myEffect2 = new Fx.Morph(item, {duration: morphDuration, transition: Fx.Transitions.linear});
										myEffect2.start({
													'opacity':0,
													'height':"0px"
										});	
								}
							});
							// applies effect only for main element
							var myEffect1 = new Fx.Morph($(topnavPopupFor(item.id)), {duration: morphDuration, transition: Fx.Transitions.linear});
							myEffect1.start({
										'opacity':1,
										'height':coordPopup.height+"px"
							});
						}
					}
				}).delay(200);
				$('eshop-topnav').addEvent('mouseleave', function(){
					(function() {
						if (!topnavOver) {
							var myEffect2 = new Fx.Morph($(topnavPopupFor(item.id)), {duration: morphDuration, transition: Fx.Transitions.linear});
							myEffect2.start({
										'opacity':0,
										'height':"0px"
							});
						}
					}).delay(50);
					activeMenu = null;
				});
				$('eshop-topnav-menus').addEvent('mouseenter', function(){
					topnavOver = true;
				});
				$('eshop-topnav-menus').addEvent('mouseleave', function(){
					var myEffect2 = new Fx.Morph($(topnavPopupFor(item.id)), {duration: morphDuration, transition: Fx.Transitions.linear});
					myEffect2.start({
						'opacity':0,
						'height':"0px"
					});
					topnavOver = false;
				});
				activeMenu = item.id;
			});
	});

// Set footer input fields on click
	$$('.footerInput').addEvent('click',function(){ 
		this.value = '';
	});									  

// Set ORDERS popup
	if ($('eshop-footer-popup-content-loggedin')) {
		if (shopUserState){
			$('eshop-footer-popup-content-loggedin').setStyle('display','block');
			$('eshop-footer-popup-content-loggedin').getElement('input.footerSubmit').addEvent('click', function(){
				var jsonRequest = new Request.JSON({
					url: "/"+locale+"/shop/services/GetOrderPageUrl?orderid="+$$('.footerInput')[0].value,
					noCache: true,
					onComplete: function(answer){
						if(answer.RESULT.STATUS !="SUCCESS"){
							if (!$('eshop-footer-error')) {
								errorP = new Element('p',{'id': 'eshop-footer-error', 'html':answer.RESULT.ERRORDESC});
								errorP.inject($('eshop-footer-popup-content-loggedin'),'bottom');
							} else {
								$('eshop-footer-error').set('html', answer.RESULT.ERRORDESC);
							}
						}
						else
							document.location.href = '/'+locale+answer.ORDERURL;
					}
				}).get();
			});
		} else {
			$('eshop-footer-popup-content-notloggedin').setStyle('display','block');
		}
	}

// IE6 bug
	var strChUserAgent = navigator.userAgent;
	var intSplitStart = strChUserAgent.indexOf("(",0);
	var intSplitEnd = strChUserAgent.indexOf(")",0);
	var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
	if (strChMid.indexOf("MSIE 6") == -1) {
		addShadow();
	}
	
});

