$(function(){

	/** Kategória menü nyit-csuk */
	$('.webshopTree ul li ul').hide().closest('li').has('ul').click(function(event){ 
		$('.webshopTree ul li ul').slideUp();
		$(this).children('ul').slideDown();
		return false;
	});	
	/** Végpontok kattinthatóak (nem nyit-csuk) */
	$('.webshopTree ul li:not(:has(ul)) a').click(function(event) {
		event.stopPropagation();	
	});
	/** aktuális kategória szülőinek kinyitása */
	$('.webshopTree li.actual').parents('ul').show();
	/*
	if($('#referenciakepek').length) {
		$('#referenciakepek').hide();
	*/	
	var	refLightbox = new CalcunLightbox({
			title : '',
			plugin : 'Layer',
			content: 'html',
			action: 'Plugins/Referenciak/index.php',
			width: 800,
			height: 550,
			callback: function() {
				
			}
		});
	
	jumpToPic = function(num) {
		var gallery = Galleria.get(0);
		gallery.show(num-1);
	}
		
	if($('#referenciak .ref').length) {
		
		
		$('#referenciak .ref a').click(function(){
			$('#referenciak .ref a').removeClass('active');
			$('#referenciakepek').fadeOut('fast', function(){
				$(this).remove();
			});
			var elem = $(this);
			var gal = $('<div id="referenciakepek"></div>');
			elem.addClass('active');
			
			
			$('#referenciak').after(gal);
			gal.load(elem.attr('href'), function(){
				gal.galleria({
					width: 600,
					height: 420
				
				});
			});
			
			return false;
			
		});
	
	}	
		
	/*	
	$('a.showRefGallery').click(function(){
		refLightbox.show();	
	});		
	*/	
	/*
	}
	*/
	
	if($('ul.products').has('li').length) {
		$('ul.products').galleria({
			width: 700,
			height: 540,
			dataConfig: function(img) {
				
				return {
					title: $(img).attr('alt'),
					description: $(img).closest('a').siblings('.description').html() + $(img).closest('a').siblings('.toCart').html()
				}
			},
			extend: function(options) {
				this.bind('image', function(e) {
					Webshop.initAddToCart();	
				});
			}
		});
	}
	
	$('a[href$=.pdf]').attr('target', '_blank');
	

	
});

$(window).load(function() {
	if($('ul.subcategories').length) {
		var scrollers = $('ul.subcategories');
		scrollers.each(function() {
			var container = $(this);
			container.wrap('<div class="slide-wrapper"></div');
			var wrapper = container.closest('.slide-wrapper');
			var elements = container.children('.subcategory');
			var elementsFullWidth = 0;
			
			var scrollRight = $('<a class="forward" href="#"><img src="/Resources/Images/FrontTemplate/rightArrow.png" alt=" "/></a>');
			var scrollLeft = $('<a class="backward" href="#"><img src="/Resources/Images/FrontTemplate/leftArrow.png" alt=" "/></a>');
			
			scrollRight.css('opacity',0.5);
			scrollRight.hover(function(){
				$(this).css('opacity',1);
			}, function(){
				$(this).css('opacity',0.5);
			})
			scrollLeft.css('opacity',0.5);
			scrollLeft.hover(function(){
				$(this).css('opacity',1);
			}, function(){
				$(this).css('opacity',0.5);
			})
			
			wrapper.css('position', 'relative').css('overflow', 'hidden');
			container.css('position', 'relative');
			
			
			var currentElementIndex = 0;
			var elementsWidths = [];
			var maxScroll;
			var onEnd = false;				
			scrollRight.click(function(){
				if(onEnd || currentElementIndex +1 == elements.length) {
					return false;
				}
				
				
				var cpos = container.position();
				if(-cpos.left <= maxScroll)	{
					currentElementIndex++;	
					position = $(elements.get(currentElementIndex)).position();
					var toPos = -position.left;
					container.animate({ left: toPos });
				}
				return false;
			});
			
			scrollLeft.click(function(){
				if(currentElementIndex  == 0) {
					return false;
				}
				onEnd = false;
				currentElementIndex--;
				position = $(elements.get(currentElementIndex)).position();
				container.animate({ left: -position.left });
				return false;
			});
			
			
			elements.each(function(){
				$(this).width($(this).find('img').width());
				elementsFullWidth += $(this).outerWidth(true);
				elementsWidths.push($(this).outerWidth(true));
			});
			container.width(elementsFullWidth);
			maxScroll = elementsFullWidth - wrapper.width();
			if(elementsFullWidth > wrapper.innerWidth()) {
				container.closest('.scrollable').prepend(scrollRight).prepend(scrollLeft);
				var topPos = wrapper.position().top+45;
				scrollRight.css('top',topPos);
				scrollLeft.css('top',topPos);
			}
		});
	}
	
	$flashMessage = $('.calcun-error, .calcun-info');
		if($flashMessage.length) {
			var messages = '';
			var sep = '';
			$flashMessage.each(function(){
				messages += sep + $(this).text();	
				sep = '<br />';
			});
			$flashMessage.first().html(messages);
			$flashMessage.not(':first').remove();
			Calcun.showFlashMessage($flashMessage.html());
			$flashMessage.remove();
		}
		
	$('.webshopOrderForm form').validate();	
})

	var Calcun = {
		
		sessionId : '',
		
		showFlashMessage: function(message) {
			var $flashMessage = $('<div class="flash-message">' + message + '</div>');
			$('body').prepend($flashMessage);
			var wWidth = $(window).width();
			var wHeight = $(window).height();
			var wrap = '<div id="flashMessage"></div>';
			$flashMessage.wrap(wrap);
			var $container = $('#flashMessage');
			if($container.width() > 500) {
				$container.width(500);
			}
			$container.css({
				position: 'fixed',
				left: parseInt((wWidth - $container.outerWidth())/2),
				top: parseInt((wHeight - $container.outerHeight())/2 - 60),
				'z-index': 999
			});
			$close = $('<a class="close" href="#">X</a>').click(function(){
				$container.remove();
				$shadow.fadeOut(function(){
					$(this).remove();
				});	
				return false;
			});
			var $shadow = $('<div id="flashMessageShadow">').css({
				position: 'fixed',
				top: 0,
				left: 0,
				width: '100%',
				height: '100%',
				opacity: 0.6,
				'z-index': 998
			}).click(function() {
				$close.click();
			});
			
			
			$container
				.before($shadow)
				.append('<div class="info-foot"></div>');
				;
			$container.prepend($close);
		},
	
		resetAlertButtons: function() {
			$.calcunalerts.okButton = 'OK';
			$.calcunalerts.cancelButton = 'cancel';
		}
		
	}
