var hoveredhintlinkrequest = '';

jQuery('document').ready(function($){
	$('.slider').productslider();
	$('.sidenav li ul li.active').closest('li.parent').removeClass('parent');
	$('.mousetrap').live(
		'click',
		function(e){
/*			$.fancybox({
				href : $(this).closest('#wrap').find('a').attr('href')
			}); */
			$(this).closest('#wrap').find('a').click();
		}
	);
	
//	$('a[data-relrel]').fancybox();
	$('.product-image a, a[data-relrel]').fancybox();
//	$('.product-image a').fancybox();
	$('a[data-relrel]:not(.cloud-zoom)').click(function(){
		$.fancybox.cancel();
	});



	selectFirstOptions($);
	
	$('.catalog-product-view .product-options select').live('change', function(){
		selectFirstOptions(jQuery);
	});
	
	if (!$('.breadcrumbs').length) {
		$('body').addClass('no-breadcrumbs');
	}
	
	$('body.cms-page-view').each(function(i,e){ // auto current class in cms nav
		classes = $(this).attr('class').split(" ");
		$(classes).each(function(ix,ex){
			if (ex != "")
			{
				ex = ex.replace('cms-', '');
				if (ex != 'page-view')
				{
					$('.cms-nav li.' + ex).addClass('current');				
				}
			}
		});
	});


	/* IE7 Nav Fix */	
	$('#nav li').hover(function(){
		$(this).addClass('over');
		$('> ul', this).addClass('shown-sub');
		$('.main-container').css({'z-index' : -1})
	}, function(){
		$(this).removeClass('over');
		$('> ul', this).removeClass('shown-sub');
		$('.main-container').css({'z-index' : 0})
	})
	/* /IE7 Nav Fix */

	/* Welcome MSG Hints */
	
	$('.welcome-msg a').hover(function(e){
		if (hoveredhintlinkrequest) {
			hoveredhintlinkrequest.abort();
		}
		hint = $('<div class="hoverhint"><div class="hoverhint-inner"><div class="hoverhint-loading">&nbsp;</div></div></div>');
		$(this).append(hint);
		hoveredhintlinkrequest = $.get($(this).attr('href'), function(data){
			inside = $('.std', data).html();
			$('.hoverhint-inner').html(inside);
		});
	},
	function(){
		$('.hoverhint, this').removeClass('hoverhint').addClass('hoverhintfading').fadeOut('slow', function(){
			$('.hoverhintfading').remove();
		});
	});
	
	$('.welcome-msg a').live('click', function(e){
		e.preventDefault();
	});
	
	/* /Welcome MSG Hints */	

	$('.catalog-product-view .success-msg a.gotocart:not(":first")').hide();

});

function selectFirstOptions($) {
	$('.catalog-product-view .product-options select').each(function(xxi,e){
		vic = $('option:selected', this);
		if (!parseInt(vic.val(), 10))
		{
			$(vic).remove();
			$('option:nth(0)', this).attr('selected', 'selected').click();

/* PROTOTYPE PART ------------------------- */
			$$('.catalog-product-view .product-options select')[xxi].simulate('change');
/* /PROTOTYPE PART ------------------------ */
		}
	});
}

(function($){
	$.fn.productslider = function(options){
		var settings = {
			'items' : 3
		};
		
		return this.each(function(){
			if (options) {
				$.extend(settings, options);
			}
			
			itemheight	= $('ul li', this).outerHeight(true);
			itemwidth	= $('ul li', this).outerWidth(true);
			maxstep		= $('ul li', this).length - settings.items;
			curstep		= 0;
			
			$(this).data('maxstep', maxstep);
			$(this).data('curstep', curstep);

			
			$(this).css({
				'height' : itemheight + 'px',
				'overflow' : 'hidden',
				'position' : 'relative'
			});

			width_diff	= $(this).innerWidth() - (itemwidth * settings.items);

			$('ul', this).css({
				'height' : itemheight + 'px',
				'width' : '100000px',
				'left' : (width_diff / 2) + 'px',
				'position' : 'absolute'
			});
			
			if (settings.items < $('ul li', this).length)
			{
				$(this).append('<a href="#" class="slidebtn goleft">nach links</a>');
				$(this).append('<a href="#" class="slidebtn goright">nach rechts</a>');	
			
				$('.slidebtn.goleft', this).css({
					'left' : '0',
					'width' : (width_diff / 2) + 'px',
					'height' : itemheight + 'px'
				}).animate({'opacity' : 0.2}, 0);
				$('.slidebtn.goright', this).css({
					'right' : '0',
					'width' : (width_diff / 2) + 'px',
					'height' : itemheight + 'px'
				});
				
				$('.slidebtn', this).click(function(e){
					e.preventDefault();
					slider = $(this).closest('div');
	
					maxstep = $(slider).data('maxstep');
					curstep = $(slider).data('curstep');
					
					nextstep = ($(this).hasClass('goleft')) ? curstep - 1 : curstep + 1;
					if ((nextstep < 0) || (nextstep > maxstep)) return;
					$('> ul', slider).animate({
						left : (($(this).hasClass('goleft')) ? '+' : '-') + '=' + itemwidth
					}, 600, 'easeOutCubic', function(){
						if (curstep == 0)
						{
							$('.slidebtn.goleft', slider).animate({'opacity' : 0.2}, 500);
						}
						else
						{
							$('.slidebtn.goleft', slider).animate({'opacity' : 1}, 500);				
						}
						
						if (curstep == maxstep)
						{
							$('.slidebtn.goright', slider).animate({'opacity' : 0.2}, 500);
						}
						else
						{
							$('.slidebtn.goright', slider).animate({'opacity' : 1}, 500);	
						}
					});
					curstep = nextstep;
					$(slider).data('curstep', curstep);
				});
			}
		});
	}
})(jQuery);


/**
* Event.simulate(@element, eventName[, options]) -> Element
*
* - @element: element to fire event on
* - eventName: name of event to fire (only MouseEvents and HTMLEvents interfaces are supported)
* - options: optional object to fine-tune event properties - pointerX, pointerY, ctrlKey, etc.
*
* $('foo').simulate('click'); // => fires "click" event on an element with id=foo
*
**/
(function(){
  
  var eventMatchers = {
    'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
    'MouseEvents': /^(?:click|mouse(?:down|up|over|move|out))$/
  }
  var defaultOptions = {
    pointerX: 0,
    pointerY: 0,
    button: 0,
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    bubbles: true,
    cancelable: true
  }
  
  Event.simulate = function(element, eventName) {
    var options = Object.extend(Object.clone(defaultOptions), arguments[2] || { });
    var oEvent, eventType = null;
    
    element = $(element);
    
    for (var name in eventMatchers) {
      if (eventMatchers[name].test(eventName)) { eventType = name; break; }
    }

    if (!eventType)
      throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');

    if (document.createEvent) {
      oEvent = document.createEvent(eventType);
      if (eventType == 'HTMLEvents') {
        oEvent.initEvent(eventName, options.bubbles, options.cancelable);
      }
      else {
        oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
          options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
          options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
      }
      element.dispatchEvent(oEvent);
    }
    else {
      options.clientX = options.pointerX;
      options.clientY = options.pointerY;
      oEvent = Object.extend(document.createEventObject(), options);
      element.fireEvent('on' + eventName, oEvent);
    }
    return element;
  }
  
  Element.addMethods({ simulate: Event.simulate });
})()

