var popup;

var touchMode = false;

$(function(){
	
	var url = document.URL;
	
	if (url.search(/www.homoeoprax.ch+/) != -1) {
	} else {
		window.location.replace('http://www.homoeoprax.ch/'); 
	}
	
	
	
	//
	
	if(	navigator.userAgent.toLowerCase().indexOf('iphone') > -1 || 
		navigator.userAgent.toLowerCase().indexOf('ipad') > -1 || 
		navigator.userAgent.toLowerCase().indexOf('android') > -1) 
	{
		touchMode = true;
	}
	
	
	
	/*
	----------------------------------------------------
	Navigation
	----------------------------------------------------
	*/
	
	var component = $('#page .col.left > ul');
	jQuery.extend(component, navigation_component);
	component.setup();
	
	/*
	----------------------------------------------------
	Popup
	----------------------------------------------------
	*/
	
	popup = $('body');
	jQuery.extend(popup, popup_component);
	popup.setup();
	
	/*
	----------------------------------------------------
	Meta
	----------------------------------------------------
	*/
	
	//popup.openPopup();
	
	$('#page .metanav ul').find('> li.icon').each(function(i) {	
		
		var btn = this;
		
		$('a', btn).bind({
		  click: function() {
			if( $(btn).hasClass('forward') ) {
				popup.openPopup();
				return false;
			}
			
			
		  }
		 
		});
		
		$('a', btn).hover(function() {
			$('span.over', this).show();
		}, function() {
			$('span.over', this).hide();
		});
		
	});
	
});

/*
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Navigation Component 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

var navigation_component = {
	setup: function(){
		
		var component = this;
		
		$(component).find('> li').each(function(i) {												 
			
			var li = this;
			$(li).data("over_count", 0);
			$(li).data("out_count", 0);
			
			//
			if(touchMode==true && $(li).find('> ul').length ) {
				$(li).find('> ul').prepend('<li><a href="' + $('>a', li).attr('href')  + '">Überblick</a></li>')
			}
			
			//
			
			$(li).find('> ul').each(function(i) {
				
				var maxWidth = 0;
			
				$('ul', li).find('li > a').each(function(i) {	

					if($(this).width() > maxWidth) {
						maxWidth = $(this).width(); 
					}
				
				});	
				
				$('ul', li).width(maxWidth + 54);
				$('ul > li > a', li).width('100%');
				$('ul', li).css({opacity : 1}).hide();
				
			});	
			
		});	
		
		if(touchMode==true) {
			
			$('> li > a', component).click(function() {
				
				var li = $(this).closest('li');
				
				//hide other Navigations
				$(component).find('> li').each(function(i) {												 
					component.showHide("out", this );
				});	
				
				if($(li).find('> ul').length) {
					component.showHide("over", li );
					return false;
				} 
				
			
			});	
			
		
		} else {
			
			$('> li > a', component).hover(function() {
				component.showHide("over", $(this).closest('li') );
			}, function() {
				component.showHide("out", $(this).closest('li') );
			});
			
			$('> li > ul', component).hover(function() {
				component.showHide("over", $(this).closest('li') );
			}, function() {
				component.showHide("out", $(this).closest('li') );
			});
			
		}
		
		
		
		
		
	},
	showHide: function(_state, _li){
		
		var component = this;
		
		switch(_state) {
			case "over":
				$(_li).data("over_count", $(this).data("over_count") + 1 );
				break;
			case "out":
			default:
				$(_li).data("out_count", $(this).data("out_count") + 1 );
				break;
		}
		
		if( _state == "over" && $(_li).data("over_count") != $(_li).data("out_count") ) {
			$('> a', _li).addClass('over');
			$('> ul', _li).show();
		} else {
			$('> a', _li).removeClass('over');
			$('> ul', _li).hide();	
		}
		
		
	}	
};	

/*
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Popup Component 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
*/


var popup_component = {
	setup: function(){
		
		var component = this;
		
	},
	openPopup: function(_link){
		
		
		var component = this;
		
		
		
		//alert(url);
		
		var html = '<div id="popup">' +
  		'<div class="content">' +
		'<div class="contain"></div>' +
		
  		'</div>' +
  		'<div class="background"></div>' +
		'</div>';
		
		//$('body').append(html);
		component.setupForms();
		
		$('#popup .background', component).css({opacity : 0});
		$('#popup .content', component).css({opacity : 0});
		$('#popup', component).css({opacity : 1}).show();
		
		$('#popup .background', component).stop().animate({
			opacity: 0.5
		}, 200, 'easeOutQuad', function() {	
			
		});	
		
		$('#popup .content', component).stop().animate({
			opacity: 1
		}, 200, 'easeOutQuad', function() {	
			
		});	
		
		///
		
		$('#popup .background',component).click(function() {
			component.closePopup();
			return false;
		});	
		
		$('#popup .close_btn',component).click(function() {
			component.closePopup();
			return false;
		});	
		
		
		
	},
	setupForms: function(){
		
		var component = this;
		
		$('#popup form input[name="t_site_url"]',component).val( window.location.href );
		$('#popup form input[name="t_site_name"]',component).val( document.title );
		
		$("#popup .contain.form").show();
		$("#popup .contain.send").hide();
		
		
		$('body').find('input[type=text]').each(function(i) {
			
			$(this).data('value', $(this).attr('title') );
			
			$(this).bind({
			  blur: function() {
				if( $(this).val() == "") {
					$(this).val( $(this).data('value') );
				}
			  },
			  focus: function() {
				if( $(this).val() == $(this).data('value') ) {
					$(this).val('');
				}
			  }
			});
			
			 $(this).val(  $(this).data('value') );
		});
		
		$('body').find('textarea').each(function(i) {
			
			$(this).data('value', $(this).attr('title') );
			
			$(this).bind({
			  blur: function() {
				if( $(this).val() == "") {
					$(this).val( $(this).data('value') );
				}
			  },
			  focus: function() {
				if( $(this).val() == $(this).data('value') ) {
					$(this).val('');
				}
			  }
			});
			
			$(this).val(  $(this).data('value') );
		});
		
		$(".recommend_form form").validate({
		
			rules: 
			{
				
				t_sender_email: { email: true },
				t_recipient_email: { email: true }
						 
			}, 
			messages: 
			{
				t_sender_email: { email: "Email Adresse ist nicht korrekt." },
				t_recipient_email: { email: "Email Adresse ist nicht korrekt." }
				
						 
			}, 
			errorContainer:"#errors", 
			errorLabelContainer:"#errors ul",
			highlight: function(element) {
				$(element).addClass("error");
			},
			unhighlight: function(element) {
				$(element).removeClass("error");
			},
			wrapper:"li", 
			submitHandler: function(form) {
				 
				 var error = false;
				 
				 $("#popup").find('input[type=text]').each(function(i) {
					if( $(this).val() == $(this).data('value') ) {
						$(this).addClass('error');
						error = true;
					}
					
				});
				
				if(error) {
					return false;
				} 
				
				if( $('textarea', form).val() == $('textarea', form).data('value') ) {
					$('textarea', form).val('')
				}
				
				
				var data = $(form).serialize()
			
				$.ajax({
					
					url: $('base').attr('href') + '/wp-content/themes/homoeoprax/ajax/sendForm.php',		
					type: "POST",		
					data: data,
					dataType: "xml",		
		
					success: function (_xml) {				
						
						var msg ='Das Mail wurde an ' + $('input[name="t_recipient_email"]', form).val() + ' versendet.<br/>Vielen Dank fürs Weiterempfehlen.';
						$(".contain.send p.text", form).html(msg);
						
						$(".contain.send p a.back", form).click(function() {
				
							$(".contain.form", form).show();
							$(".contain.send", form).hide();
							
							$('textarea', form).val( $('textarea', form).data('value') );
							$('input[name="t_recipient_name"]', form).val( $('input[name="t_recipient_name"]', form).data('value') );
							$('input[name="t_recipient_email"]', form).val( $('input[name="t_recipient_email"]', form).data('value') );
							
							
							return false;
						
						});	
						
						$(".contain.form", form).hide();
						$(".contain.send", form).show();
					}		
				});
					
					
					
				
				
			},
			
			invalidHandler: function(form) {
				
				 $("#popup").find('input[type=text]').each(function(i) {
					if( $(this).val() == $(this).data('value') ) {					
						$(this).addClass('error');
					}
					
				});
			}
	
		});
		
		 
		
	},
	closePopup: function(){
		
		var component = this;
		
		$('#popup', component).stop().animate({
			opacity: 0
		}, 200, 'easeOutQuad', function() {	
			$('#popup', component).hide();
			//$('#popup', component).remove();
		});	
		
	}
};
