(function($) {
	jQuery.fn.layerTrigger = function( aUserOptions ) {
		// default setting
		$.fn.layerTrigger.aDefaultOptions = {
		sTemplateSelector: '#idLayerTemplate',
		sFocusSelector: 'input,select',
		sCloseSelector: null,
		iFadeTimeOut: '250',
		iOffsetLeft: '50',
		iOffsetTop: '50',
		contentGetFunction: null,
		aGetFunctionParameters: null
	};

	var aOptions = $.extend({}, $.fn.layerTrigger.aDefaultOptions, aUserOptions);
	var bShowLayerInProgress = false;
	var bInputFieldHasFocus = false;
	var iCurrentLayerBoxID = null;
	var sLayerBoxTemplateClassSelector = 'globalLayerBoxSelector';

	return this.each(function() {
		$(this).hover(
			function() {
				if( bInputFieldHasFocus == true ) return false;

					$('.' + sLayerBoxTemplateClassSelector).hide();
					bShowLayerInProgress = true;
					$(this).attr('id',$(this).attr('id').replace(/^p/,''))
					iCurrentLayerBoxID = 'div#' + $(this).attr("id") + 'Layer';

					if( !($('#' + $(this).attr("id") + 'Layer').length > 0) ) {
						// get new layer
						var oLayerBox = $(aOptions.sTemplateSelector).clone();
						oLayerBox.css('position','absolute');
						oLayerBox.attr({'id': $(this).attr("id") + 'Layer', 'class': oLayerBox.attr("class") + ' ' + sLayerBoxTemplateClassSelector});

						var targetDetailLink = $('a#a'+$(this).attr("id"));
						if(targetDetailLink.attr("href")) {
							var targetDetailContactLink = targetDetailLink.attr("href").replace(/\/angebot\//,'/haendler/');
						} else {
							targetDetailContactLink ="";
						}
						if( $.isFunction(aOptions.contentGetFunction) ) {
							aOptions.contentGetFunction( $(this), oLayerBox, aOptions.aGetFunctionParameters,targetDetailLink,targetDetailContactLink );
						} else {
							return false;
						}
						if( aOptions.sCloseSelector != null && ( $(aOptions.sCloseSelector).attr('class') || $(aOptions.sCloseSelector).attr('id') ) ) {
							oLayerBox.find(aOptions.sFocusSelector).focus(function () {bInputFieldHasFocus = true;});
							oLayerBox.find(aOptions.sCloseSelector).click(
									function () {
										
										bInputFieldHasFocus = false;
										$(iCurrentLayerBoxID).fadeOut("fast");																
									});
						}
						$('body').append(oLayerBox);
						oLayerBox.hover(
							function() {
								if( bInputFieldHasFocus == true ) return false;
								bShowLayerInProgress = true;
							},
							function() {
								if( bInputFieldHasFocus == true ) return false;
								bShowLayerInProgress = false;
								setTimeout(function() {if( bShowLayerInProgress == false ) {
									oLayerBox.fadeOut("fast");
								}}, aOptions.iFadeTimeOut);
							});
					}
					var offset = $(this).offset();
					$(iCurrentLayerBoxID).css("left", parseInt(offset.left)+parseInt(aOptions.iOffsetLeft));
					$(iCurrentLayerBoxID).css("top", parseInt(offset.top)+parseInt(aOptions.iOffsetTop));
					$(iCurrentLayerBoxID).fadeIn("fast");
				},
				function() {
					if( bInputFieldHasFocus == true ) return false;
					bShowLayerInProgress = false;
					setTimeout(function() {if( bShowLayerInProgress == false ) {
						$(iCurrentLayerBoxID).fadeOut("fast");
					}}, aOptions.iFadeTimeOut);
						
				}
			);
		});
	};
	
})(jQuery);

(function($) {
	jQuery.fn.layerImage = function( oTrigger, oLayer, aUserOptions ) {
			var aDefaultOptions = {
					iImageWidth: 64,
					iImageHeight: 64,
					sImageClass: null
				};
			var aOptions = $.extend({}, aDefaultOptions, aUserOptions);
			var oLayerImage = oTrigger.clone();
			oLayerImage.attr("width",aOptions.iImageWidth);
			oLayerImage.attr("height",aOptions.iImageHeight);
			oLayerImage.attr("class",aOptions.sImageClass);
			oLayer.append(oLayerImage);
		};
})(jQuery);


(function($) {
	jQuery.fn.layerContact = function( oTrigger, oLayer, aUserOptions, targetDetailLink , targetDetailContactLink ) {
			var aDefaultOptions = {};
			var selects = '';
			var aOptions = $.extend({}, aDefaultOptions, aUserOptions);
			var sUrl = HOME_URL + 'request/offercontact/id/' + oTrigger.attr('id') + '/type/' + $('input#rcid').val();
			if(rqopt.email !="") { oLayer.find('input#_pemail').val(rqopt.email); }
			if(rqopt.phone !="") { oLayer.find('input#_ptelefon').val(rqopt.phone); }
			if(rqopt.names !="") { oLayer.find('input#_kname').val(rqopt.names); }
			if(jQuery.browser.msie && jQuery.browser.version=='6.0') {
				oLayer.find('img.close').attr('src', HOME_URL+'img/bg/close.gif');
			}

			oLayer.find('.mainpic').attr('src', HOME_URL+'img/bg/tooltip/loader.gif');
			$.getJSON(sUrl,
				function(data){
					if( data.images.length > 0 ) {
						oLayer.find('.mainpic').attr('src', data.images[0]);
						var iter = 1;
						oLayer.find('.thumb').each(function() {
							if( data.images[iter] ) {
								$(this).attr('src', data.images[iter]);
								iter++;
							}
						});

						rqopt.house = (data.contact.company =="") ? data.contact.name : data.contact.company;
						rqopt.title = targetDetailLink.attr("title");
						rqopt.isOn	= true;
						oLayer.find('#priceoffer').text(rqopt.makeOption(1));
						oLayer.find('.tooltip-content-gif').hide();
						oLayer.find('.tooltip-content').show();
						oLayer.find('p#contactName').html('<b>'+data.contact.name+'</b>');
						oLayer.find('p#contactFunc').html(data.contact.func);
						oLayer.find('form#fromlayer').attr('action',targetDetailContactLink);
						oLayer.find('a#contactlink').attr("href",targetDetailContactLink);
						oLayer.find('input#proposedprice').click(function () {
								rqopt.isOn = false;
								selects = oLayer.find('select#contactRequest').html();
								var toption ='';
								for (var i=1; i <= 3; i++) {
									toption += '<option value="'+i+'">Preis -'+i+'% ('+Math.round(data.price*(100-i)/100)+'€)</option>';
								}
								oLayer.find('select#contactRequest').html(toption);
								oLayer.find('textarea#priceoffer').text(rqopt.makePriceText(data.price,1));
							});
						oLayer.find('select#contactRequest').change(function () {
							if(rqopt.isOn == true) {
								oLayer.find('textarea#priceoffer').text(rqopt.makeOption(this.value));
							} else {
								oLayer.find('textarea#priceoffer').text(rqopt.makePriceText(data.price,this.value) );
							}
							});
						// Mein Preisvorschlag für Sie: 40.783 €. Bitte kontaktieren Sie mich.
						oLayer.find('#topOpac').click(function () {window.location.href=data.dlink;});
						var detail = data.dlink.replace(/product\/showoffermember/,'markt/angebot');
						oLayer.find('.tooltip-offerpic').attr("href",targetDetailLink.attr("href"));
						oLayer.find('input#pricequestion').click(function () {
							rqopt.isOn = true;
							oLayer.find('select#contactRequest').html(selects);
							oLayer.find('textarea#priceoffer').text(rqopt.makeOption(1));
							});
					}
				});
			// request contact data and fill the contact layer box
		};
})(jQuery);
