jQuery.fn.extend({
    unobfuscate: function () {
        jQuery(this).each(function (n) {
            var href = jQuery(this).attr('href').replace(/\(AT\)/, '@').replace(/\(PUNKT\)/, '.');
            var text = jQuery(this).text().replace(/\(AT\)/, '@').replace(/\(PUNKT\)/, '.');
            jQuery(this).attr('href', href).text(text);
        });
    },
    hintedForm: function () {
        var that = this;
        
        function HintedMessage(my) {
            var myNode = jQuery(my),
                message = myNode.parents('.field').find('.message');
            
            if (message.length !== 0) {
                message.hide();
                myNode.addClass('validationError');
            }
            
            return {};
        }
    
        jQuery(this).each(function () {
            HintedMessage(this);
        });
    
        return {};
    }
});

EVW = {};
EVW.DIRECTION_RIGHT = 1;
EVW.DIRECTION_LEFT  = -1;

EVW.main = function () {
    function onSlide (direction) {
        var that    = jQuery(this),
            slider  = that.parents('ul'),
            length  = slider.find('li').length,
            myIndex = that.parents('li').prevAll().length;
        
        function getRealIndex (index) {
            if (index >= length) {
                index = 0;
            }
            
            if (index < 0) {
                index = length - 1;
            }
            
            return index;
        }
        
        function slideToIndex (index) {
            index = getRealIndex(index);
            
            var targetItem = slider.find('li').eq(index);
            return slider.animate({left: targetItem.position().left * -1}, 'fast');
        }
        
        return function (event) {
            if (direction === EVW.DIRECTION_RIGHT) {
                slideToIndex(myIndex + 1);
            } else {
                slideToIndex(myIndex - 1);
            }
            
            event.preventDefault();
        };
    }
    
    jQuery('#NavigationLogo img').length && jQuery('#NavigationLogo').cycle({timeout: 2000});
    jQuery('#Slides img').length && jQuery('#Slides').cycle();
    jQuery('a[href^="mailto:"]').unobfuscate();
    jQuery('#Stage div:last').find('> p:last').css('margin-bottom', '50px');
    
    jQuery('#Gallery .move-left').each(function () {
        jQuery(this).click(onSlide.apply(this, [EVW.DIRECTION_LEFT]));
    });
    
    jQuery('#Gallery .move-right').each(function () {
        jQuery(this).click(onSlide.apply(this, [EVW.DIRECTION_RIGHT]));
    });
};

jQuery(EVW.main);

(function ($) {
    $('form input:text, form textarea').hintedForm();
    
    // $('#Form_NewsletterForm').submit(function (e) {
    //     if (e.originalEvent) {
    //         var returnValue = $(this).submit();
    //         $('input:text', this).hintedForm();
    //         
    //         return returnValue;
    //     }
    //     
    //     return false;
    // });
    
	$('.thumbnail, .zooming').concrete({
		onmouseenter: function (event) {
			var link = this.find('a'),
			    img = link.find('img'),
			    offset = img.position(),
			    overlay = $('#zoomOverlay'),
				dimensions = {
					width: img.width(),
					height: img.height()
				};
			
			if (img.length <= 0) {
			    return null;
			}
			
			if (overlay.length === 0) {
				$('body').append('<a id="zoomOverlay" target="_blank">Zoom<\/a>');
				overlay = $('#zoomOverlay');
			}
			
			overlay
			.appendTo(this)
			.css({
				left: offset.left
					+ (dimensions.width / 2)
					- (overlay.width() / 2)
					+ parseInt(img.css('margin-left')),
				top: offset.top
					+ (dimensions.height / 2)
					- (overlay.height() / 2)
					+ parseInt(img.css('margin-top'))
			})
			.attr('href', link.attr('href'))
			.show();
			
			// Trigger for fancybox.
			$('body').trigger('htmlUpdate');
		},
		
		onmouseleave: function (event) {
			$('#zoomOverlay').hide();
		}
	});
	
    $('.Instruments').concrete({
        onmatch: function () {
            this.find('#ManufacturerFilter input:submit').hide();
        },
        
        updateViewCallback: function () {
            var that = this;
            
            return function (data) {
                return that.updateView(data);
            };
        },
        
        updateView: function (data) {
            var tabular = $(data).find('.tabular'),
                target  = this.find('.tabular');
            
            tabular.replaceAll(target);
            $('body').trigger('htmlUpdate');
        },
        
        onchange: function (event) {
            if ($(event.target).is('#ManufacturerFilter select')) {
                $.post(
                    $('#ManufacturerFilter form').attr('action'),
                    {'form[doSubmit]': true,
                     'form[ManufacturerName]': $('#ManufacturerFilter form option:selected').val()
                    },
                    this.updateViewCallback()
                );
            }
            
            event.preventDefault();
        }
    });
})(jQuery);