/*
 * contactable 1.2.1 - jQuery Ajax contact form
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2010-01-18 $
 *
 */
 
//extend the plugin
(function($){

	//define the new for the plugin ans how to call it	
	$.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			auswahl: 'Anfrage',
			name: 'Name',
			firma: 'Firma',
			anschrift: 'Anschrift',
			telefon: 'Telefon',
			email: 'Email',
			message : 'Message',
			sicherheitscode : 'Sicherheitscode',
			subject : 'A contactable message',
			recievedMsg : 'Vielen Dank für Ihre Nachricht!',
			notRecievedMsg : 'Leider ist ein Fehler aufgetreten, bitte aktualisieren Sie die Seite und versuchen Sie es erneut!',
			disclaimer: '<span style="font-weight:bold;">Datenschutzhinweis:</span> Ihre Daten werden nur verwendet, um Ihre Anfrage zu beantworten und werden nicht an Dritte weitergegeben!<br><br><span class="red"> * </span><span style="font-weight:bold;">Pflichtfeld</span>',
			hideOnSubmit: true
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function(options) {
			//construct the form
			$(this).html('<div id="contactable"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><span class="schnell">// Schnell-Kontakt</span><br><br><p><select id="auswahl" name="auswahl" class="contact" style="width:100%;"><option name="auswahl" value="Sonstiges">- Art der Anfrage -</option><option name="auswahl" value="Suche neue Werbeagentur">// Suche neue Werbeagentur</option><option name="auswahl" value="Grafik-Konzept">// Grafik / Konzept</option><option name="auswahl" value="Web-Online-Marketing ">// Web & Online-Marketing </option><option name="auswahl" value="Business-Paket">// Business-Paket</option><option name="auswahl" value="Textil-Werbemittel">// Textil / Werbemittel</option><option name="auswahl" value="Fotostudio">// Fotostudio</option><option name="auswahl" value="Beklebungen">// Beklebungen</option></select></p><br><p><label for="name">// Name <span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /></p><p><label for="firma">// Firma</label><br /><input id="firma" class="contact" name="firma" /></p><p><label for="anschrift">// Anschrift</label><br /><input id="anschrift" class="contact" name="anschrift"/></p><p><label for="telefon">// Telefon <span class="red"> * </span></label><br /><input id="telefon" class="contact" name="telefon"/></p><p><label for="email">// E-Mail <span class="red"> * </span></label><br /><input id="email" class="contact" name="email"/></p><p><label for="comment">// Ihre Nachricht <span class="red"> * </span></label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><label for="sicherheitscode">Übertragen Sie die Zeichen in das nebenstehende Feld <span class="red"> * </span></label><br /><br /><img src="captcha/captcha.php" align="left"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="sicherheitscode" class="sicherheitscode" name="sicherheitscode" style="width: 150px; height: 25px;"/></p><p><input class="submit" type="submit" value="Senden"/></p><p class="disclaimer">'+defaults.disclaimer+'</p></div></form>');
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginLeft": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
				$(this).animate({"marginLeft": "+=387px"}, "slow"); 
				$('#contactForm').animate({"marginLeft": "+=390px"}, "slow"); 
			}, 
			function() {
				$('#contactForm').animate({"marginLeft": "-=390px"}, "slow");
				$(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			
			//validate the form 
			$("#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					telefon: {
						required: true,
					},
					email: {
						required: true,
						email: true
					},
					comment: {
						required: true
					},
					sicherheitscode: {
						required: true,
						minlength: 5
					}
				},
				//set messages to appear inline
					messages: {
						auswahl: "",
						name: "",
						firma: "",
						anschrift: "",
						telefon: "",
						email: "",
						comment: "",
						sicherheitscode: ""
					},			

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('mail.php',{subject:defaults.subject, auswahl:$('#auswahl').val(), name:$('#name').val(), firma:$('#firma').val(), anschrift:$('#anschrift').val(), telefon:$('#telefon').val(), email:$('#email').val(), comment:$('#comment').val(), sicherheitscode:$('#sicherheitscode').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') {
							$('#callback').show().append(defaults.recievedMsg);
							if(defaults.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								$('#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=500px"}, "slow");
								$('div#contactable').animate({dummy:1}, 2000).animate({"marginLeft": "-=500px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								$('#overlay').css({display: 'none'});	
							}
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);


