/***********************************************************************************
  * Duckstein Javascript - www.duckstein.de
  * @author: Kai Hommel <kaho@db-n.com>, deepblue networks AG
  **********************************************************************************/

/* Sticky Column */
/**********************************************************************************/
var StickyColumn = {
	init: function(elm) {
		this.elm = elm;
		this.elmHeight = elm.height();
		this.winHeight = $(window).height() - 120;
		var parentWidth = elm.parent().width();
		this.setPosition(parentWidth);
	},
	setPosition: function(parentWidth) {
		var _this = this;
		if(_this.elmHeight > _this.winHeight) {
			_this.elm.css({'position': 'static'});
			$('body').css({'background-attachment': 'scroll'});
			
		} else {
			_this.elm.css({'position': 'fixed', 'width':parentWidth+'px'});
			$('body').css({'background-attachment': 'fixed'});
		}
	}
};
/**********************************************************************************/

/* Layer */
/**********************************************************************************/
var Layer = {
	init: function(elm, overlay, center) {
		$('.layer').hide();
	},
	open: function(elm, overlay, center) {
		var _this = this;
		_this.windowWidth = $(window).width();
		_this.windowHeight = $(window).height();
		if(overlay === true) {
			var overlayObj = _this.buildOverlay();
			overlayObj.css({'opacity':0.75});
			$('body').append(overlayObj);
			overlayObj.click(function() {
				$(elm).hide();
				$(this).remove();
			});
		}
		if(center === true) {
			var elmWidth = $(elm).width();
			var elmHeight = $(elm).height();
			if(elmHeight < _this.windowHeight) {
				var posTop = (_this.windowHeight - elmHeight) / 2
				$(elm).css({'position':'fixed'});
			} else {
				var posTop = 10;
				$(elm).css({'position':'absolute'});
			}
			$(elm).css({'z-index':10000,'top':posTop+'px','left':'50%','margin-left':'-'+(elmWidth / 2)+'px'});
			$('body').append($(elm));
		}
		$(elm).append('<a href="#close" class="closeButton" onclick="$(this).parent(\'.layer\').hide();$(\'#overlay\').remove(); return false;">schliessen</a>');
		$(elm).show();
	},
	buildOverlay: function() {
		var _this = this;
		var overlay = '<div id="overlay" style="width:'+_this.windowWidth+'px;height:'+_this.windowHeight+'px;position:fixed;top:0px;left:0px;z-index:9000;background:#000000;"></div>'
		return $(overlay);
	}
};
/**********************************************************************************/  

/* Contactform */
/**********************************************************************************/
var ContactForm = {
	selector: '.subject',
	formelements: '.contact_form_elements',
	formelement: '.form_group',
	init: function(id) {
		var _this = this;
		this.id = id;
		var subject = $('#'+id+' '+this.selector);
		this.showElements(subject.val());
		subject.change(function() {
			_this.showElements(subject.val());
		});
	},
	showElements: function(s) {
		var _this = this;
		
		if(s == "0") { // Bitte wählen...
			$('#'+_this.id+' '+_this.formelements).hide();
			$('#'+_this.id+' .werbemittel').hide();
			$('#'+_this.id+' .sponsoring').hide();
		} else {
			$('#'+_this.id+' '+_this.formelements).show();
			switch (s) {
				case "2": // Produktbeanstandung
					$('#'+_this.id+' .produktbeanstandung').show();
					$('#'+_this.id+' .werbemittel').hide();
					$('#'+_this.id+' .sponsoring').hide();
				break;
				case "6": // Sponsoring
					$('#'+_this.id+' '+_this.formelements).hide();
					$('#'+_this.id+' .werbemittel').hide();
					$('#'+_this.id+' .sponsoring').show();
				break;
				case "7": // Werbemittel
					$('#'+_this.id+' '+_this.formelements).hide();
					$('#'+_this.id+' .sponsoring').hide();
					$('#'+_this.id+' .werbemittel').show();
				break;
				default:
					$('#'+_this.id+' .produktbeanstandung').hide();
					$('#'+_this.id+' .werbemittel').hide();
					$('#'+_this.id+' .sponsoring').hide();
				break;
			}
		}	
	}
};
/**********************************************************************************/

/* Tooltips */
/**********************************************************************************/
var Tooltips = {
	wrapper:	'li',
	togglelink:	'.toggle',
	box:		'.tooltip',
	init: function(id) {
		var _this = this;
		this.id = id;
		this.togglelinks = $('#'+this.id+' '+this.wrapper);
		this.togglelinks.each(function(index) {
			$(this).addClass('tooltip_'+index);
		});
		this.togglelinks.hover(
			function() { _this.showTooltip(this); },
			function() { _this.hideTooltip(this); }
		);
	},
	showTooltip: function(obj) {
		var _this = this;
		$(obj).addClass('hover');
		var link = $(obj).children(_this.togglelink);
		$(obj).addClass(link.attr('rel'));
		link.next(_this.box).show();
	},
	hideTooltip: function(obj) {
		var _this = this;
		$(obj).removeClass('hover');
		var link = $(obj).children(_this.togglelink);
		$(obj).removeClass(link.attr('rel'));
		link.next(_this.box).hide();
	}
};
/**********************************************************************************/

/***********************************************************************************
  * Call by document.ready
  **********************************************************************************/
$(function(){

	/* Navigation */
	/*****************************************************************************/
	$('.navigation ul').supersubs({ 
		minWidth:	10,   // minimum width of sub-menus in em units 
		maxWidth:	40,   // maximum width of sub-menus in em units 
		extraWidth:	0     // extra width can ensure lines don't sometimes turn over 
		// due to slight rounding differences and font-family
	}).superfish({
		autoArrows:	false
	});
	Layer.init();
	/*****************************************************************************/
	
	/* Custom form elements */
	/*****************************************************************************/
	$("input[type=radio], input[type=checkbox], input[type=file], select").uniform({fileDefaultText: '', fileBtnText: 'Durchsuchen'});
	/*****************************************************************************/
	
	/* Tooltips */
	/*****************************************************************************/
	$("ul.tooltips").each(function(index) {
		$(this).attr('id', 'tooltip_'+index);
		Tooltips.init($(this).attr('id'));
	});
	/*****************************************************************************/
	
	/* Stick Column */
	/*****************************************************************************/
	StickyColumn.init($('.sticky'));
	$(window).resize(function() {
		StickyColumn.init($('.sticky'));
	});
	/*****************************************************************************/

	/* Contact Form */
	/*****************************************************************************/
	$('form.contact_form').each(function(index){
		$(this).attr('id', 'contactform_'+index);
		ContactForm.init($(this).attr('id'));
	});
	/*****************************************************************************/

});
/***********************************************************************************
  * END Call by document.ready
  **********************************************************************************/
