/**
 * Compact labels plugin
 */
(function($){$.fn.compactize=function(){return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){label.hide();}).blur(function(){if(input.val()===''){label.show();}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		gmap : {			
			init : function(){
				if (GBrowserIsCompatible()) {
					mapNode1 = document.getElementById('google-map-headoffice');
					mapNode2 = document.getElementById('google-map-workshop');
					if(mapNode1){						
						map1 = new GMap2(mapNode1);
						// markers
						center1 = new GLatLng(52.380228,16.83249);
						marker1 = new GMarker(center1);
						html1   ='<p><strong>LUDMER s.c.</strong><br />' +
								'<strong>Adres:</strong> ul. Nowosolska 13, 60-171 Poznań<br />' +
								'<strong>tel/fax:</strong> +48 061 8671775<br /><strong>gsm:</strong> +48 601 568 928<br /><strong>e-mail:</strong> <a class="email" href="mailto:biuro@ludmer.pl" title="Napisz do nas">biuro@ludmer.pl</a></p>';
						map1.setCenter(center1, 16);			
						// controls
						map1.addControl(new GSmallMapControl());
						map1.addControl(new GMapTypeControl());
						map1.addControl(new GOverviewMapControl());					
						map1.addOverlay(marker1);
						marker1.openInfoWindowHtml(html1);					
						$(window).unload(GUnload);
					}
					if(mapNode2){						
						map2 = new GMap2(mapNode2);
						// markers
						center2 = new GLatLng(52.381349,16.816678);
						marker2 = new GMarker(center2);
						html2   ='<p><strong>LUDMER s.c.</strong><br />' +
								'<strong>Adres:</strong> ul. Nasturcjowa 5, 60-175 Poznań<br />' +
								'<strong>tel/fax:</strong> +48 061 8678927</p>';
						map2.setCenter(center2, 16);			
						// controls
						map2.addControl(new GSmallMapControl());
						map2.addControl(new GMapTypeControl());
						map2.addControl(new GOverviewMapControl());					
						map2.addOverlay(marker2);
						marker2.openInfoWindowHtml(html2);					
						$(window).unload(GUnload);
					}
				}
			}			
		},
		forms : {			
			validRequired : function(id,er){ $(id).parent().find('span.error').remove(); if(!($(id).val())||($(id).val()==0)||($(id).val()=='')){ $(id).after('<span class="error">* '+er+'</span>');	return false; } return true; },		
			validEmail : function(id,erReq,erValid){ $(id).parent().find('span.error').remove(); if(!($(id).val())){	$(id).after('<span class="error">* '+erReq+'</span>'); return false; } else { reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test($(id).val()) == false) { $(id).after('<span class="error">* '+erValid+'</span>'); return false;}} return true; },
			getErrors : function(){
				errors = new Array();				
				errors.push(new Array('default','required','pl','Pole jest wymagane.'));
				errors.push(new Array('default','required','en','Field is required.'));
				errors.push(new Array('default','required','de','Field is required.'));
				
				errors.push(new Array('default','invalid','pl','Dane są niepoprawne.'));
				errors.push(new Array('default','invalid','en','Data are invalid.'));
				errors.push(new Array('default','invalid','de','Data are invalid.'));
				
				errors.push(new Array('default','default','pl','Dane są niepoprawne.'));
				errors.push(new Array('default','default','en','Data are invalid.')); 
				errors.push(new Array('default','default','de','Data are invalid.')); // default	
				
				errors.push(new Array('fname','required','pl','Imię jest wymagane.'));
				errors.push(new Array('fname','required','en','First name is required.')); 
				errors.push(new Array('fname','required','de','First name is required.')); // fname
				
				errors.push(new Array('lname','required','pl','Nazwisko jest wymagane.'));
				errors.push(new Array('lname','required','en','Last name name is required.'));
				errors.push(new Array('lname','required','de','Last name name is required.')); // lname
				
				errors.push(new Array('email','required','pl','E-mail jest wymagany.'));
				errors.push(new Array('email','required','en','E-mail address is required.'));
				errors.push(new Array('email','required','de','E-mail address is required.'));
				errors.push(new Array('email','invalid','pl','E-mail jest niepoprawny.'));
				errors.push(new Array('email','invalid','en','E-mail address is invalid.'));
				errors.push(new Array('email','invalid','de','E-mail address is invalid.')); // email
				errors.push(new Array('subject','required','pl','Temat jest wymagany.'));
				errors.push(new Array('subject','required','en','Please choose a subject.'));
				errors.push(new Array('subject','required','de','Please choose a subject.')); // subject
				errors.push(new Array('message','required','pl','Wiadomość jest wymagana.'));
				errors.push(new Array('message','required','en','Message is required.'));
				errors.push(new Array('message','required','de','Message is required.')); // message
				return errors;
			},
			handleErrors : function(fieldname,ertype,lang){if(!fieldname) fieldname = 'default'; if(!ertype) ertype = 'default'; if(!lang) lang = 'en'; errors = Engine.forms.getErrors(); for(i=0;i<errors.length;i++){ if(errors[i][0]==fieldname && errors[i][1]==ertype && errors[i][2]==lang) errorMessage = errors[i][3];}; return errorMessage; },
			validateContactForm : function(){
				lang = 'en'; if($('#f-lang')) lang = $('#f-lang').val();				
				$('#contact_first_name').blur(function(e){ 	Engine.forms.validRequired('#contact_first_name',Engine.forms.handleErrors('fname','required',lang));	});				
				$('#contact_last_name').blur(function(e){  	Engine.forms.validRequired('#contact_last_name',Engine.forms.handleErrors('lname','required',lang)); });
				$('#contact_email').blur(function(e){ 	   	Engine.forms.validEmail('#contact_email',Engine.forms.handleErrors('email','required',lang),Engine.forms.handleErrors('fname','invalid',lang)); });
				$('#contact_subject').blur(function(e){ 	Engine.forms.validRequired('#contact_subject',Engine.forms.handleErrors('subject','required',lang)); });
				$('#contact_message').blur(function(e){ 	Engine.forms.validRequired('#contact_message',Engine.forms.handleErrors('message','required',lang)); });
			}
			
		}		
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.forms.validateContactForm();
	
});
