/*
 * jQuery UI PflichtTextModal
 *
 * Copyright (c) 2011 Felix Nagel (Paints Multimedia Gmbh)
 *
 * Depends:
 *	jquery.ui.core.js
 *	jquery.ui.widget.js
 *  working solution for PNG in some IEs
 */
(function($) {

$.widget("ui.pflichttextmodal", {
	options: {
		overlay: {
			'class': "ui-widget-overlay",
			css: {
				// position fixed in modern browsers
				position: "fixed",
				top: "auto",
				bottom: "10px",
				height: "auto",
				zIndex: 1001
			}
		},
		content: {
			'class': "ui-pflichttextmodal-content",
			css: {
				// position fixed in modern browsers
				position: "fixed",
				top: "auto",
				bottom: "10px",
				width: "100%",
				zIndex: 1002,
				// style
				padding: "10px 0"
			}
		},
		contentWrapper: {
			'class': "ui-pflichttextmodal-contentwrapper",
			css: {
				// style
				background: "white",
				// center position
				margin: "0 auto",
				width: 857,
				top: -16,
				left: -7,
// 				border: "1px solid #D0D0D0",
				// needed because of the close image absolute position
				position: "relative" 
			}
		},
		contentOverflow: {
			css: {
				// style
				padding: "45px 10px 5px 10px",
				// overflow
				overflowY: "auto",
				overflowX: "visible",
				height: $(window).height() / 8
			}
		},
		closeImage: $("<img />", {
			src: "fileadmin/templates/skin_standard/images/close_layer.gif",
			css: {
				position: "absolute",
				top: 9,
				right: 3,
				cursor: "pointer"
			}
		})
	},
	
	_create: function() {
		var self = this;
		if (
			// do not use with IE6
			!($.browser.msie && $.browser.version.substr(0, 1) < 7) 
			&&
			// use only when URL has google adwords parameter
			$.getUrlVar('gclid')
		) {
			self.open();
			// add event
			$("body").bind("click.pflichttextmodal", function() {
				self.destroy();
			});
		}
	},
	
	open: function() {
		// falls Cookie noch nicht gesetzt ist, zeige den Layer
		if ($.cookie) {
			if (!$.cookie('pflichttextClosed')) {
				var self = this, options = this.options, pflichttext = '<div class="ui-pflichttextmodal-pflichttext">Pflichttext</div>';
				
				self.overlay = $("<div />", options.overlay);
				$("body").append(self.overlay);		
				
				// prepare and inject content div
				options.contentOverflow.html = self.element.html();
				options.contentWrapper.html = $("<div />", options.contentOverflow)
				self.contentWrapper = $("<div />", options.contentWrapper);
				options.content.html = self.contentWrapper
				self.content = $("<div />", options.content);
				$("body").append(self.content);
				
				// adjust overlay size
				if (options.overlay.css.height == "auto") self.overlay.css("height", self.content.outerHeight());
				
				self.contentWrapper.prepend(options.closeImage);		
				options.closeImage.wrap('<div class="ui-pflichttextmodal-closeimage"></div>');
				options.closeImage.wrap(pflichttext);
			}
		}
	},
		
	destroy: function() {
		$("body").unbind(".pflichttextmodal");
	
		this.overlay.remove();
		this.content.remove();
		
		// hole aktuelles Datum und setze x * 60 * 1000 Millisekunden drauf (x steht für die Minuten)
		var date = new Date();
 		date.setTime(date.getTime() + (30 * 60 * 1000)); 
		// setze Cookie nach dem Schließen des Layers
		if ($.cookie) $.cookie('pflichttextClosed', true, { expires: date });
		
		$.Widget.prototype.destroy.apply( this, arguments );
	}
});

// extend jQuery with url vars getter
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for (var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

})(jQuery);
