(function($) {

// !$.fn.inputDefault	
	$.fn.inputDefault = function () {
		$(this).each(function (index, el) {
			var self = this;
			this.element = $(el);
			this.element.data({
				'default': this.element.val()
			});
			this.element.focus(function () {
				if ($(this).val() == $(this).data('default')) $(this).val('');
			});
			this.element.blur(function () {
				if (/^\s*$/.test($(this).val() || ' ')) $(this).val($(this).data('default'));
			});
		});
		return this;
		
	}

// !$.fn.slider	
   $.fn.slider = function (opts) {
        $(this).each(function (index, el) {
            var self = this;
            this.idle = false;
            this.current = 0;
            this.element = $(el);
            this.options = jQuery.extend({
                'container': '.slider-inner',
                'item': '.item',
                'next': '.next',
                'prev': '.prev',
                'nums': '.number',
                'offset': 1,
                'onstart': function () {},
                'onstop': function () {}
            }, opts || {});
            this.items = this.element.find(self.options.container + ' ' + this.options.item).each(function (i, el) {
                $(el).addClass('item-' + i);
            });
            this.activateRelated = function () {
                this.element.find(this.options.nums).removeClass('active').eq(self.current).addClass('active');
            }
            this.scrollTo = function (offset) {
                if (!self.idle) {
                    self.idle = true;
                    self.options.onstart(self);
                    self.activateRelated();
                    self.element.find(self.options.container).animate({
                        'scrollLeft': offset
                    }, self.options.duration, function () {
                        self.idle = false;
                        self.options.onstop(self);
                    });
                }
            }
            this.getOffset = function (index) {
                return self.element.find(self.options.container).scrollLeft() + $(self.items[index ? index : self.current]).position().left
            }
            this.scrollNext = function () {
                self.current = (self.current + self.options.offset >= self.items.size()) ? 0 : self.current + self.options.offset;
                self.scrollTo(self.getOffset());
            }
            this.scrollPrev = function () {
                self.current = (self.current - self.options.offset < 0) ? self.items.size() - self.options.offset : self.current - self.options.offset;
                self.scrollTo(self.getOffset());
            }
            this.element.find(this.options.next).click(function (ev) {
                ev.preventDefault();
                self.scrollNext();
            });
            this.element.find(this.options.prev).click(function (ev) {
                ev.preventDefault();
                self.scrollPrev();
            });
            this.element.find(this.options.nums).each(function (index, el) {
                $(el).addClass('number-' + index).removeClass('active').click(function (ev) {
                    ev.preventDefault();
                    self.current = index;
                    self.scrollTo(self.getOffset());
                })
            });
            this.activateRelated();
        });
        return this;
	}
	
// !$.fn.replaceSelect		
	$.fn.replaceSelect = function (fn) {
		$(this).each(function (index, el) {
			var self = this;
			this.element = $(el);
			this.callback = fn || $.noop;
			this.prepare = function () {
				var opt = this.element.find('option');
				var inp = '<div class="select-container">';
				inp += '<input type="hidden" name="' + this.element.attr('name') + '" value="' + this.element.val() + '" />';
				inp += '<div class="dropdown-container clearfix">';
				inp += '<div class="dropdown-select">';
				inp += '<a class="selector" href="#">' + opt.filter(':selected').text() + '</a>';
				inp += '</div>';
				inp += '<ul class="dropdown-options hide">';
				opt.each(function (i, o) {
					var c = '';
					if (i == 0) c += ' first';
					if (i == opt.length - 1) c += ' last';
					inp += '<li class="' + c + '"><a href="javascript: void(0);" rel="' + $(o).val() + '">' + $(o).text() + '&#160;</a></li>';
				});
				inp += '</ul>';
				inp += '</div>';
				inp += '</div>';
				return $(inp);
			}
			this.element = this.prepare().replaceAll(this.element).dropdown();
			this.element.find('li a').click(function () {
				self.element.find('input:hidden').val($(this).attr('rel'));
				self.element.find('a.selector').text($(this).text());
				$(this).parents('.dropdown-options').hide();
				if ($(this).parents('li.select').attr('id') == 'enquiry_country_id_input') {
					if ($(this).attr('val') != 132) {
						$('#enquiry_zip_input, #enquiry_city_input').removeClass('required').find('abbr[title=required]').remove()
					}
					else {
						$('#enquiry_zip_input, #enquiry_city_input').addClass('required').find('label').append('<abbr title="required">*</abbr>')
					}
				}
				self.callback(self);
			});
		});
	}
	
//	$.fn.dropdown
	$.fn.dropdown = function (opts) {
        $(this).each(function (index, el) {
            var self = this;
            this.timeout = false;
            this.element = $(el);
            this.options = jQuery.extend({
                'select': '.dropdown-select .selector',
                'options': '.dropdown-options',
                'animation': 'slideUp',
                'timeout': 1250,
                'duration': 250
            }, opts || {});
            this.element.find(this.options.select).click(function (ev) {
                ev.preventDefault();
                self.element.find(self.options.options).toggle();
            });
            this.element.find(this.options.options).hover(function () {
                if (self.timeout) window.clearTimeout(self.timeout);
            }, function () {
                self.timeout = window.setTimeout(function () {
                    self.element.find(self.options.options)[self.options.animation](self.options.duration);
                }, self.options.timeout);
            });
        });
        return this;
    }

	
})(jQuery);

$(document).ready(function() {

	if ($(".stage .article-inner > div.block").size() > 1) {
    	$('.stage').append('<a href="#" class="prev" title="vorherige Seite">&laquo; vorherige Seite</a><a href="#" class="next" title="nächste Seite">nächste Seite &raquo;</a>');
		$('.stage ').slider({
		    'container': '.article-outer',
		    'item': '.block:not(.mod_article)'
		});
    }
   	if ($(".ce_text_image .float_left img").size() > 0) $('.ce_text_image .float_left img').rotate(-2);
    if ($(".ce_image .float_left img").size() > 0)$('.ce_image .float_left img').rotate(-2);
    if ($(".ce_text_image .float_right img").size() > 0)$('.ce_text_image .float_right img').rotate(22);
    
	if ($(".gallery div.item").size() > 1) {
    	$('.gallery').append('<a href="#" class="prev" title="vorherige Seite">&laquo; vorherige Seite</a><a href="#" class="next" title="nächste Seite">nächste Seite &raquo;</a>');
		$('.gallery ').slider({
		    'container': '.outer-wrapper',
		    'item': '.item'
		});
    }
    if ($(".gallery div.item").size() > 0) $(".gallery div.item").each(function(index,el){
 		$(el).children().rotate(-2);    
    });
	
	$('input.text').inputDefault();
	
	// öffnet das Kontaktformular
	$('a.m32, a.m8, a.m107, a.m108, li.m32 a, li.m108 a, li.m245 a, a.m236').attr('href', self.location.href + '#beforeHeader').click(function() {
		$('#beforeHeader').show('fast');
		 $('html, body').animate({scrollTop:0}, 'fast');		
	})
	// schließt das Kontaktformular
	$('#ff-close').click(function() {
		$('#beforeHeader').hide('fast');
	})
	
	$('select.replace-select').replaceSelect();
	
});

