if (typeof Function.prototype.bind !== 'function') {
    Function.prototype.bind = function (context) {
        var args = Array.prototype.slice.call(arguments, 1),
            f = this;
        return function () {
            args.push.apply(args, arguments);
            return f.apply(context, args);
        };
    };
}

(function($,sr){
	// debouncing function from John Hann
	// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
	var debounce = function (func, threshold, execAsap) {
		var timeout;
 
		return function debounced () {
			var obj = this, args = arguments;
				function delayed () {
					if (!execAsap)
						func.apply(obj, args);
					timeout = null; 
				};
	 
				if (timeout)
					clearTimeout(timeout);
				else if (execAsap)
					func.apply(obj, args);
	 
				timeout = setTimeout(delayed, threshold || 100); 
			};
		}
	// smartresize 
	jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
})(jQuery,'smartresize');


$.namespace('navigation');
$.navigation = (function () {
	var _ = {
		bindEvents : function () {
			$.registry.get('navi')
				.find('li')
				.bind(
					'mouseenter focus',
					function (ev) {
						var $this = $(this).addClass('active');
						if ($.browser.msie) {
							$this
								.find('ul')
								.first()
								.show();
							return;
						}
						$.registry.get('navi')
							.find(':animated')
							.stop(true, true);
						$this
							.find('ul')
							.first()
							.fadeIn();
				}
			)
			.bind(
				'mouseleave blur',
				function () {
					var $this = $(this).removeClass('active');
					if ($.browser.msie) {
						$this
							.find('ul')
							.first()
							.hide();
						return;
					}
					$.registry.get('navi')
						.find('ul, :animated')
						.stop(true, true);
					$this
						.find('ul')
						.first()
						.fadeOut();
				}
			);
		},
		addIframeHack : function () {
			$.registry.get('navi').find('.sub-menu').each(function(i){
				var $this = $(this),
					h = $this.outerHeight(),
					w = $this.outerWidth(),
					iframe  = $([
						'<iframe src="javascript:false;"',
							'class="iframe_helper"',
							'style="height: ',
							h + 'px; width: ',
							w +'px;">' +
						'</iframe>'
					].join(''));
				$this.before(iframe);
			});
		}
	};
	return {
		init : function () {
			if (!$('#quickmenu').hasClass("no-js")) {
				_.bindEvents();
				_.addIframeHack();
			}
		}
	};
})();

$.namespace('search');
$.search = (function () {
	var _ = {
		onFocus : function () {
			var $this = $(this);
			if ( $this.val()=='Suche' ) {
				$this.val("");
			}
			$this
				.unbind('focus');
		},
		registerEvents : function () {
			var self = this;
			$.registry.get('searchInput')
				.focus(
					self.onFocus
				).keypress(function (ev) {
					var $this = $(this);
					if ( !ev ) {
						var ev = window.event;
					}
					if ( ev.which && ev.which === 13 ) {
						if ( $.trim( $this.val() ) === "" ) {
							return;
						}
						$this.blur();
						$this.parents().filter('form').first().submit();
					}
					if ( ev.keyCode && ev.keyCode == 27 ) {
						$this
							.blur();
					}
				})
				.blur(function () {
					var $this = $(this);
					if( $.trim( $this.val() )=== "" ) {
						$this.val('Suche')
						.focus(
							self.onFocus
						);	
					}
				});
			$('#searchform').submit(function () {
				var $this = $(this),
					$inp = $.registry.get('searchInput');
				if ( $.trim( $inp.val() ) === "" ) {
					$inp.focus();
					return false;
				}
				return true;
			});
		}
	};
	return {
		init: function () {
			_.registerEvents();
		}
	};
})();

$.namespace('layers');
$.layers = {
	init: function () {
		$.registry.get('gallery').fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'speedIn'		: 500,
			'speedOut'		: 200,
			'overlayColor'	: '#000',
			'overlayOpacity': 0.9,
			'titlePosition' : 'over',
			'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ': &nbsp; ' + title : '') + '</span>';
			},
			padding : 0,
			centerOnScroll : true
		});
	
		$.registry.get('youtube').fancybox({
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'speedIn'		: 500,
			'speedOut'		: 200,
			'overlayColor'	: '#000',
			'overlayOpacity': 0.9,
			'padding'		: 1
		});
	}
};


$.namespace('publicis');
$.publicis = (function () {
	return {
		init : function () {
			$.registry.set('navi', $('#navi').find('ul').first());
			$.registry.set('slider', $(".page_start").first());
			$.registry.set('searchInput', $('#s'));
			$.registry.set('gallery', $("a[rel=gallery]"));
			$.registry.set('youtube', $("a[rel=youtube]"));
			$.navigation.init();
			$.search.init();
			$.layers.init();
		}
	}
})();

$(function() {
	var quick = $('#quickmenu'),
		deviceAgent = navigator.userAgent.toLowerCase(),
		quick_bottom,
		wl = window.location.href;
	//switch css to "js-enabled"-version
	$("body").removeClass("no-js");
	
	//quick navi

	if(deviceAgent.match(/(iphone|ipod|ipad)/)) {
		$("body").addClass("mobile-safari");
	} else{
		quick_bottom = $('#quickmenu_bottom').slideUp(0);
		$('#quickmenu_trigger').click(function() {
			if($('#quickmenu').hasClass('open')){
				//close
				quick.removeClass('open');
				quick_bottom.slideDown('slow');
			}else{
				//open
				quick.addClass('open');
				quick_bottom.slideUp('slow');
			}
		});
	}
	
	$.publicis.init();
	//$.mobile.init();
	
	//content slider
	$.registry.get('slider').scrollable({
		circular: true
	});
	if ( $.registry.get('slider').length > 0 ) {
		$(".prev, .next").click(function(event){
			event.preventDefault();
		});	
	}

	if ( $.isFunction ( $.fn.touchwipe) ) {
		//$("#slide-wrap").touchwipe({
		//	min_move_x: 100
		//});
	}

	//stage info box scroller
	$('.scroller').scrollbar({
		arrows: false
	});
	
	$("#awards").accordion({
		autoHeight : false,
		header:'h4',
		collapsible: true
	});
	$(".accordion").accordion({
		autoHeight : false,
		header:'h4.handle',
		collapsible: true
	});

    $('.jshidden').show();

    $("ul.social").find("a.facebook").attr("href", "http://de.facebook.com/sharer.php?u=" + wl);
    $("ul.social").find("a.twitter").attr("href", "http://twitter.com/home?status=" + wl);
	$('.external').find("a.facebook").attr("href", "http://de.facebook.com/sharer.php?u=" + wl);
	$('.external').find("a.twitter").attr("href", "http://twitter.com/home?status=" + wl);

    $('.media a.pics').click(function () {
        $.registry.get('gallery')
            .first()
            .trigger('click');
    });

    $('a.video').click(function () {
        $.registry.get('youtube')
            .first()
            .trigger('click');
    });
	
	$('h4.ui-accordion-header').hover(function () {
		$(this).find('span.ui-icon').toggleClass('ui-icon-hover');
	});
	
	$('div.news li, div#news .col, #joblist>li').click(
		'click',
		function (ev) {
			if (ev && ev.target && $(ev.target).is('a') === this) {
				return false;
			}
			window.location.href = $(this).find('a').attr('href');
			return true;
		}
	);
	
	TypeHelpers.insertClasses();
	
});

