_t = function(text) {
	params = [];
	for ( var i = 1; i < _t.arguments.length; i++ ) {
		params[i-1] = _t.arguments[i];
	} 
	jQuery.ajaxSetup({
		async: false,
		cache: false,
		data: {
			t: text,
			params: params.join(',')
		},
		type: 'POST',
		url: '/'+GLOBAL.language+'/ajax/_t.php'
	});
	return jQuery.ajax(function(d) { 
		return d;
	}).responseText;
}

jQuery(document).ready( function(){
	jQuery('.pPreisliste').each(function() {
		jQuery(this).click(function() {
			jQuery.ajaxSetup({
				async: true,
				cache: false,
				data: {
				},
				type: 'POST',
				url: '/'+GLOBAL.language+'/ajax/preisliste.php'
			});
			jQuery.ajax({ success: function(d) { 
				
				jQuery(d).dialog({
					bgiframe: true,
					autoOpen: true,
					modal: true,
					draggable: false,
					resizeable: false,
					width: 600,
					buttons: {
						'schließen': function() {
							jQuery(this).dialog('close');
						}
					},
					open: function() {
						var gp = 6.90;
						jQuery('#sites_boxes input[type=checkbox]').click(function() {
							var sum = gp;
							jQuery('#sites_boxes input[type=checkbox]:checked').each(function() {
								var val = parseFloat(jQuery(this).parent().next().find('.price').text().replace(/€ /, ''));
								if ( val > 0 && ! isNaN(val) ) {
									sum += val;
								}
							});
							jQuery('#gesamtpreis').text('€ '+ sum.toFixed(2));
						}).triggerHandler('click');
					}
				});	
				
			}});
			
			return false;
		});
	});
});

jQuery.fn.extend({
//Example Input 
	eInput: function() {
		jQuery('input[title]').each(function() {
			if(jQuery(this).val() === '') {
				jQuery(this).val($(this).attr('title')).addClass('example');
			}

			jQuery(this).focus(function() {
				if(jQuery(this).val() === jQuery(this).attr('title')) {
					jQuery(this).val('').removeClass('example');
				}
			});

			jQuery(this).blur(function() {
				if(jQuery(this).val() === '') {
					jQuery(this).val(jQuery(this).attr('title')).addClass('example');
				}
			});
		});
	},
//Pic Fading Detailansicht
	fadePic: function() {
		if( jQuery('.mainPic') ) {
			this.each(function(){
				var src = jQuery(this).attr('src').replace(/small/i, 'main');
				var fadePic = jQuery('<img src="'+src+'" />').css({
					opacity: 0,
					position: 'absolute',
					left: 0,
					zIndex: 0
				}).appendTo(jQuery('.mainFadePic').parent());
				jQuery('.mainFadePic').css({
					position: 'relative',
					zIndex: 1
				});
				jQuery(this).hover( function() {
					jQuery(this).queue('fx', []).animate({
						opacity: '0.5'
					});
					jQuery('.mainFadePic').queue('fx', []).animate({
						opacity: 0
					}, 800);
					fadePic.queue('fx', []).animate({
						opacity: 1
					}, 800);
				}, 
				function() {
					jQuery(this).queue('fx', []).animate({
						opacity: '1'
					});
					jQuery('.mainFadePic').queue('fx', []).animate({
						opacity: 1
					}, 800);
					fadePic.queue('fx', []).animate({
						opacity: 0
					}, 800);
				});
			}).css({
				cursor: 'pointer'
			});
			return false;
		}
	},
//Bookmark Handling
	bookmark: function() {
		if (jQuery.browser.opera) {
			 this.attr({
			 	title: window.document.title,
			 	rel: 'sidebar'
			 });
		} else {			
			this.click(function(){
				site = {};
				site['url'] = window.location.href;
				site['title'] = window.document.title;

	 			if (jQuery.browser.mozilla) {
	 				window.sidebar.addPanel(site['title'], site['url'],'');
				} else if (jQuery.browser.msie ) {
	 				window.external.AddFavorite(site['url'], site['title']);
	 			} else {
					alert(_t('STRG + D'));
				}
		 		return false;
			});
		}
	},
//Merkzettel
	notepad: function(action) {
		switch(action) {
			case 'add':	
				this.click(function(){
					var ids = new Array();
					if(jQuery.cookie(this.rel) == null ) {
						ids.push(this.title); 
					} else {
						ids = jQuery.json.decode(jQuery.cookie(this.rel));
						if(jQuery.inArray(this.title, ids) < 0) {
							ids.push(this.title);
						} 	
					}
					if(ids.length > 0) {
						list =  jQuery.json.encode(ids);
						jQuery.cookie(this.rel,list,{expires: 356, path: '/'});
					} else {
						jQuery.cookie(this.rel,'',{expires: -1});
					}
					
					var url = "/"+ GLOBAL.language+"/ajax/merk_counter.php";
					jQuery.getJSON(url, {id: this.title, art: this.rel}, function(data){
					});
					
					jQuery('<div id="successNotepad" title="Merkzettel">'
					+'	<center>'
					+		_t('history_save')+'!'
					+'	</center>'
					+'</div>').dialog({
						bgiframe: true,
						autoOpen: true,
						modal: true,
						draggable: false,
						resizeable: false,
						open: function() {
							window.setTimeout(function(){
								jQuery('#successNotepad').dialog('close');
							},2500);
						}
					});	
					return false;
				});
				break;
			case 'del':
				this.click(function(){
					ids = new Array();
					if(jQuery.cookie(this.rel) != null ) {
						zw = jQuery.json.decode(jQuery.cookie(this.rel));
						delid = this.title;
						jQuery.each(zw, function(k, i) {
							if(i != parseInt(delid)) {
								ids.push(i);
							}
						});
						if(ids.length > 0) {
							list =  jQuery.json.encode(ids);
							jQuery.cookie(this.rel,list,{expires: 356, path: '/'});
						} else {
							jQuery.cookie(this.rel, '', {expires: -1, path: '/'});
						}
						jQuery(jQuery(this).parents('.boxen, .box').get(0)).remove();
					}
					return false;
				});
				break;
		}
	},
//Gebot ADD on Merkzettel
	gebotAdd: function() {
		this.click(function(){
			var ids = new Array();
			if(jQuery.cookie(this.rel) == null ) {
				ids.push(this.title); 
			} else {
				ids = jQuery.json.decode(jQuery.cookie(this.rel));
				if(jQuery.inArray(this.title, ids) < 0) {
					ids.push(this.title);
				} 	
			}
			if(ids.length > 0) {
				list =  jQuery.json.encode(ids);
				jQuery.cookie(this.rel,list,{expires: 356, path: '/'});
			} else {
				jQuery.cookie(this.rel,'',{expires: -1});
			}
			href = this.href;
			jQuery('<div id="successNotepad" title="Merkzettel">'
			+'	<center>'
			+		_t('history_save_gebot')+'!'
			+'	</center>'
			+'</div>').dialog({
				bgiframe: true,
				autoOpen: true,
				modal: true,
				draggable: false,
				resizeable: false,
				width: 400,
				open: function() {
					window.setTimeout(function(){
						window.location.href = href;
					},3500);
				}
			});	
			return false;
		});
	},
//Popups
	printLayout: function() {
		jQuery(this).click(function(){
			printSite = this.href;
			if ( jQuery.browser.msie || jQuery.browser.opera ) {
				window.open(printSite,"", "dependent=yes,height:750,width=745,toolbar=no,status=no,menubar=no,location=no,scrollbars=yes");
			} else {
				jQuery('<div id="Print" title="Druck Layout">'
				+'	<iframe width="720" height="700" src="'+printSite+'" border="0" frameborder="0" style="border:none;"></iframe>'
				+'</div>').dialog({
					bgiframe: true,
					autoOpen: true,
					modal: true,
					draggable: false,
					resizeable: false,
					width: 745,
					height: 750
				});	
			}
			return false;
		});
	},
	picPopup: function(o){
		if (o == 'remove') {
			return this.unbind('hover');
		}
		else {
			return this.hover(function(e) {
				var that = this;
				var popup = jQuery('<div class="PicPopup"><img src="/design/images/loading.gif" /></div>').css({
					opacity: 0,
					position: 'absolute',
					top: jQuery(this).offset()['top'],
					left: jQuery(this).offset()['left'] + jQuery(this).outerWidth() + 10
				}).appendTo('body').animate({
					opacity: 1
				}, 500, function() {
					var img = new Image();
					img.onload = function() {
						var img = this;
						popup.animate({
							height: this.height,
							width: this.width
						}, 500, function() {
							popup.children('img').remove();
							jQuery('<img src="' + img.src + '" alt="big picture" />').appendTo(popup[0]);
						});
					};
					img.src = jQuery(that).attr('src').replace(/smallmain/i, 'main');
				});
			}, function(){
				jQuery('.PicPopup').queue('fx', [])
				jQuery('.PicPopup').fadeOut(500, function(){
					jQuery(this).remove();
				});
			});
		}
	},
	picPopupPaket: function(o){
		if (o == 'remove') {
			return this.unbind('hover');
		}
		else {
			return this.hover(function(e) {
				var that = this;
				var popup = jQuery('<div class="PicPopup"><h4>'+this.name+'</h4><br /><center><img src="/design/images/loading.gif" /></center></div>').css({
					opacity: 0,
					position: 'absolute',
					top: jQuery(this).position()['top'],
					left: jQuery(this).position()['left'] + jQuery(this).outerWidth() + 15
				}).appendTo('body').animate({
					opacity: 1
				}, 500, function() {
					var img = new Image();
					img.onload = function() {
						var img = this;
						popup.animate({
							height: this.height + 40,
							width: this.width + 110
						}, 500, function() {
							popup.children('center').children('img').remove();
							jQuery('<img src="' + img.src + '" alt="big picture" />').appendTo(jQuery(popup[0]).children('center'));
						});
					};
					img.src = jQuery(that).attr('src');
				});
			}, function(){
				jQuery('.PicPopup').queue('fx', [])
				jQuery('.PicPopup').fadeOut(500, function(){
					jQuery(this).remove();
				});
			});
		}
	},
	pYoutube: function() {
		jQuery('#pYoutube').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width: 600,
			buttons: {
				'schließen': function() {
					jQuery(this).dialog('close');
				}
			},
			close: function() {
				jQuery('#myytPlayer')[0].stopVideo();
			}
		});
		jQuery(this).click(function(){
			jQuery('#pYoutube').dialog('open');
			return false;
		});
	},
	pSafty: function() {
		jQuery('#pSafty').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width: 600,
			buttons: {
				'schließen': function() {
					jQuery(this).dialog('close');
				}
			}
		});
		jQuery(this).click(function(){
			jQuery('#pSafty').dialog('open');
			return false;
		});
	},
	pFinanz: function() {
		jQuery('#pFinanz').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width: 675,
			buttons: {
				'schließen': function() {
					jQuery(this).dialog('close');
				}
			}
		});
		jQuery(this).click(function(){
			jQuery('#pFinanz').dialog('open');
			return false;
		});
	},
	pAgb: function() {
		jQuery('#pAgb').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width:800,
			height: 600,
			buttons: {
				'abbrechen': function() {
					jQuery(this).dialog('close');
				},
				'Ok': function() {
					jQuery('input[name="agb"]').attr("checked","checked");
					jQuery(this).dialog('close');
				}	
			}
		});
		jQuery(this).click(function(){
			jQuery('#pAgb').dialog('open');
			return false;
		});
	},
	pVertrags: function() {
		jQuery('#pVertrags').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width:800,
			height: 600,
			buttons: {
				'abbrechen': function() {
					jQuery(this).dialog('close');
				},
				'Ok': function() {
					jQuery('input[name="vertrags"]').attr("checked","checked");
					jQuery(this).dialog('close');
				}	
			}
		});
		jQuery(this).click(function(){
			jQuery('#pVertrags').dialog('open');
			return false;
		});
	},
	pResetCounter: function() {
		var data = {};
		var that;
		if ( jQuery(this) ) {
			jQuery('<div id="ResetCounter" title="'+_t('counterreset')+'">'
				+'	<center>'
				+		_t('counter_reset')+'?'
				+'	</center>'
				+'</div>').dialog({
				bgiframe: true,
				autoOpen: false,
				modal: true,
				draggable: false,
				resizeable: false,
				width:300,
				buttons: {
					'abbrechen': function() {
						jQuery(this).dialog('close');
					},
					'Ok': function() {
						jQuery.post(data.url, {id: data.id}, function(data){
							jQuery(that).html(data);
							jQuery('#ResetCounter').dialog('close');
						});
					}		
				}
			});
		}
		jQuery(this).click(function(){
			/*jQuery('#ResetCounter').dialog('open');
			that = this;
			data = {
				url: this.href,
				id: this.rel
			};*/
			return false;
		}).css({
			cursor: 'default',
			color: '#000',
			textDecoration: 'none'
		});
	},
	pToggleStatus: function() {
		$(this).click(function() {
			var that = $(this);
			jQuery('<div id="successNotepad" title="'+_t('status aendern')+'?">'
			+'	<center>'
			+		_t('wirklich_status')		
			+'	</center>'
			+'</div>').dialog({
				bgiframe: true,
				autoOpen: true,
				modal: true,
				draggable: false,
				resizeable: false,
				buttons: {
					"Nein": function() {
						jQuery(this).dialog('close');
					},
					"Ja": function() {
						$.getJSON(that.attr('href'), function(data) {
							if ( data.success ) {
								that.html(data.msg);
							} else if ( data.msg ) {
								alert(data.msg);
							}
						});
						jQuery(this).dialog('close');
					}
				}
			});	
			return false;
		});
	},
	pPaket: function() {
		jQuery('#pPaket').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width:800,
			insert: jQuery('#iPaket'),
			zIndex: 9999,
			buttons: {
				'schließen': function() {
					jQuery(this).dialog('close').hide();
				}	
			}
		});
		jQuery(this).click(function(){
			jQuery('#pPaket').dialog('open').show();
			return false;
		});
	},
	pInteressent: function() {
		jQuery('#pInteressent').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			buttons: {
				'Send': function() {
					var param = jQuery('form',this).serialize();
					var fields = jQuery('form',this).serializeArray();
					var errors = {};
					var valid = true;
					var that = this;
					jQuery.each(fields,function(k, i) {
						if( i.value != null && i.value != '') {
							var preg = -1;
							switch(i.name) {
								case 'id':
									preg = i.value.search(/([0-9])+/);
									break;
								case 'type':
									preg = i.value.search(/([a-z])+/);
									break;
								case 'name':
									preg = i.value.search(/([a-zA-Z ])+/);
									break;
								case 'tel':
									preg = i.value.search(/([0-9])+/);
									break;		
								case 'mitteilung':
									preg = 0;
									break;
								case 'email':
									preg = i.value.search(/([a-zA-Z0-9\-\_\.])+([@])([a-zA-Z0-9\-\_\.])+([\.])([a-zA-Z0-9])+/);
									break;
							}
							if(preg < 0) {
								errors[i.name] = true;
								valid = false;
							}
							
						} else {
							errors[i.name] = true;
							valid = false;
						}
					});
					if(valid) {
						var url = "/"+ GLOBAL.language+"/ajax/interessent.php";
						jQuery.getJSON(url, param, function(data){
							if( parseInt(data) == 1 ) {
								jQuery(that).html('<center>'+_t('success')+'</center>');
								window.setTimeout( function() { 
									jQuery(that).dialog('close');
								}, 1500);
							} else {
								jQuery.each(errors, function(k, i) {
									jQuery('*[name="'+k+'"]', that).css({
										borderSytle: 'solid',
										borderWidth: '1px',
										borderColor: '#df0000'
									}).focus( function(){
										jQuery(this).css({
											border: ''
										}).unbind('focus');
									});
								});
							}
						});
					} else {
						jQuery.each(errors, function(k, i) {
							jQuery('*[name="'+k+'"]', that).css({
								borderSytle: 'solid',
								borderWidth: '1px',
								borderColor: '#df0000'
							}).focus( function(){
								jQuery(this).css({
									border: ''
								}).unbind('focus');
							});
						});
					}
				},
				'abbrechen': function() {
					jQuery(this).dialog('close');
				}
			}
		});
		
		jQuery(this).click(function(){
			jQuery('#pInteressent').dialog('open');
			return false;
		});
	},
	pEmpfehlung: function() {
		jQuery('#pEmpfehlung').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			draggable: false,
			resizeable: false,
			width: 350,
			buttons: {
				'senden': function() {
					var param = jQuery('form',this).serialize();
					var fields = jQuery('form',this).serializeArray();
					var errors = {};
					var valid = true;
					var that = this;
					jQuery.each(fields,function(k, i) {
						if( i.value != null && i.value != '') {
							var preg = -1;
							switch(i.name) {
								case 'id':
									preg = i.value.search(/([0-9])+/);
									break;
								case 'type':
									preg = i.value.search(/([a-z])+/);
									break;
								case 'name':
									preg = i.value.search(/([a-zA-Z ])+/);
									break;
								case 'email':
									preg = i.value.search(/([a-zA-Z0-9\-\_\.])+([@])([a-zA-Z0-9\-\_\.])+([\.])([a-zA-Z0-9])+/);
									break;
								case 'name2':
									preg = i.value.search(/([a-zA-Z ])+/);
									break;
								case 'email2':
									preg = i.value.search(/([a-zA-Z0-9\-\_\.])+([@])([a-zA-Z0-9\-\_\.])+([\.])([a-zA-Z0-9])+/);
									break;
								case 'mitteilung':
									preg = 0;
									break;
							}
							if(preg < 0) {
								errors[i.name] = true;
								valid = false;
							}
							
						} else {
							errors[i.name] = true;
							valid = false;
						}
					});
					if(valid) {
						var url = "/"+ GLOBAL.language+"/ajax/empfehlung.php";
						jQuery.getJSON(url, param, function(data){
							if( parseInt(data) == 1 ) {
								jQuery(that).html('<center>'+_t('success')+'</center>');
								window.setTimeout( function() { 
									jQuery(that).dialog('close');
								}, 1500);
							} else {
								jQuery.each(errors, function(k, i) {
									jQuery('*[name="'+k+'"]', that).css({
										borderSytle: 'solid',
										borderWidth: '1px',
										borderColor: '#df0000'
									}).focus( function(){
										jQuery(this).css({
											border: ''
										}).unbind('focus');
									});
								});
							}
						});
					} else {
						jQuery.each(errors, function(k, i) {
							jQuery('*[name="'+k+'"]', that).css({
								borderSytle: 'solid',
								borderWidth: '1px',
								borderColor: '#df0000'
							}).focus( function(){
								jQuery(this).css({
									border: ''
								}).unbind('focus');
							});
						});
					}
				},
				'abbrechen': function() {
					jQuery(this).dialog('close');
				}
			}
		});
		
		jQuery(this).click(function(){
			jQuery('#pEmpfehlung').dialog('open');
			return false;
		});
	},
//AJAX Form send
	Interessent_Form: function() {
		jQuery(this).submit( function() {
			var param = jQuery(this).serialize();
			var fields = jQuery(this).serializeArray();
			var errors = {};
			var valid = true;
			var that = this;
			jQuery.each(fields,function(k, i) {
				if( i.value != null && i.value != '') {
					var preg = -1;
					switch(i.name) {
						case 'id':
							preg = i.value.search(/([0-9])+/);
							break;
						case 'type':
							preg = i.value.search(/([a-z])+/);
							break;
						case 'name':
							preg = i.value.search(/([a-zA-Z ])+/);
							break;
						case 'tel':
							preg = i.value.search(/([0-9+_()\/-])+/);
							break;	
						case 'betreff':
							preg = 0;
							break;	
						case 'mitteilung':
							preg = 0;
							break;
						case 'email':
							preg = i.value.search(/([a-zA-Z0-9\-\_\.])+([@])([a-zA-Z0-9\-\_\.])+([\.])([a-zA-Z0-9])+/);
							break;
					}
					if(preg < 0) {
						errors[i.name] = true;
						valid = false;
					}
					
				} else {
					errors[i.name] = true;
					valid = false;
				}
			});
			if(valid) {
				var url = "/"+ GLOBAL.language+"/ajax/interessent.php";
				jQuery.getJSON(url, param, function(data){
					if( parseInt(data) == 1 ) {
						jQuery(that).html('<center>'+_t('success_email')+'</center>');
						window.setTimeout( function() { 
							jQuery(that).dialog('close');
						}, 1500);
					} else {
						jQuery.each(errors, function(k, i) {
							jQuery('*[name="'+k+'"]', that).css({
								borderSytle: 'solid',
								borderWidth: '1px',
								borderColor: '#df0000'
							}).focus( function(){
								jQuery(this).css({
									border: ''
								}).unbind('focus');
							});
						});
					}
				});
			} else {
				jQuery.each(errors, function(k, i) {
					jQuery('*[name="'+k+'"]', that).css({
						borderSytle: 'solid',
						borderWidth: '1px',
						borderColor: '#df0000'
					}).focus( function(){
						jQuery(this).css({
							borderColor: '#000000'
						}).unbind('focus');
					});
				});
			}
			return false;
		});
	},
//Hover for Admintables
	tablehighlight: function() {
		var c = 0;
		jQuery("tbody > tr", this).each(function() {
			var color = '#fff';			
			if ( (c % 2) != 0 ) {
				if ( GLOBAL.tableColor != undefined ) {
					color = GLOBAL.tableColor;
				} else {
					color = '#DAE6EB';
				}
			}
			jQuery(this).css('backgroundColor', color).hover(function(){
				jQuery(this).css('backgroundColor', '#ffc');
			},function(){
				jQuery(this).css('backgroundColor', color);
			});
			if ( jQuery(this).next('tr').css('display') != 'none' ) {
				++c;
			}
		});
	},
//ToDo Error Handling for Forms
	errorDialog: {
		init: function() {
			jQuery('#errorDialog').dialog({
				autoOpen: false,
				bgiframe: true,
				draggable: false,
				dialogClass: 'ui-state-error',
				position: ['right', 'center'],
				width: 450,
				open: function() {
					jQuery(this).parent().css('position','fixed');
				}
			});
			return true;
		},
		open: function() {
			jQuery('#errorDialog').dialog('open');
			return true;
		}	
	},
//Wirklich Löschen
	reallydel: function() {
		jQuery(this).click(function() {
			href = this.href;
			jQuery('<div id="successNotepad" title="'+_t('loeschen')+'?">'
			+'	<center>'
			+		_t('wirklich_loeschen')		
			+'	</center>'
			+'</div>').dialog({
				bgiframe: true,
				autoOpen: true,
				modal: true,
				draggable: false,
				resizeable: false,
				buttons: {
					"Nein": function() {
						jQuery(this).dialog('close');
					},
					"Ja": function() {
						window.location.href = href;
					}
				}
			});	
			return false;
		});
	},
	reallyCopy: function() {
		jQuery(this).click(function() {
			href = this.href;
			jQuery('<div id="successNotepad" title="'+_t('eintrag duplizieren')+'?">'
			+'	<center>'
			+		_t('wirklich_kopieren')		
			+'	</center>'
			+'</div>').dialog({
				bgiframe: true,
				autoOpen: true,
				modal: true,
				draggable: false,
				resizeable: false,
				buttons: {
					"Nein": function() {
						jQuery(this).dialog('close');
					},
					"Ja": function() {
						window.location.href = href;
					}
				}
			});	
			return false;
		});
	}
});
