$(document).ready(function(){
	Cufon.replace('h1');
	calibrate_popup();
	$('.fnwp_step,.hover_effect').mouseover(function(){
		$(this).fadeTo('fast',0.6);
	});
	$('.fnwp_step,.hover_effect').mouseout(function(){
		$(this).fadeTo('fast',1);
	});
	
});

function twitter(){
	$('#popup_box #twitter_box').attr('id','popup_twitter');
	
		new TWTR.Widget({
		  version: 2,
		  type: 'profile',
		  rpp: 5,
		  interval: 6000,
		  width: 'auto',
		  height: 300,
		 id: 'popup_twitter',
		  theme: {
		    shell: {
		      background: '#636363',
		      color: '#ffffff'
		    },
		    tweets: {
		      background: '#737373',
		      color: '#ffffff',
		      links: '#4aed05'
		    }
		  },
		  features: {
		    scrollbar: false,
		    loop: false,
		    live: false,
		    hashtags: true,
		    timestamp: true,
		    avatars: true,
		    behavior: 'all'
		  }
		}).render().setUser('foodboxers').start();
}

function calibrate_popup(){
	var arrayPageSize = getPageSize();
	var pageHeight = arrayPageSize[1]+100;
	var pageWidth = arrayPageSize[0];
	var left = (pageWidth - 704)/2;
	
	$('#overlay').css({height: pageHeight + 'px', width:'100%',position : 'absolute',top:0+'px',left:0+'px'});
	$('#popup_box').css({left:left + 'px'});
}

var actief_popup ='';
function show_popup(text_id){
	actief_popup = text_id;
	var html = $(text_id).html();
	$(text_id).html('');
	$('#popup_con').html(html);
	if(text_id == '#foodboxers_community_box'){
		twitter();
	}
	
	$("html,body").stop().animate({
	 	scrollTop: '130px'
	 },1000, 'easeOutBounce',function(){
		$('#overlay').fadeTo("slow",0.6,function(){
			Cufon.replace('#popup_content .cufon_after');
			$('#popup_box').fadeIn('fast',function(){
				
			});
		});
	});
	

}

function show_image(url,title,orig){

	$('#pers_image').fadeOut("fast",function(){
		var image = '<p style="text-align:center"><strong><a href="'+orig+'">Klik op de afbeelding voor een grotere versie</a></strong></p><a href="'+orig+'"><img src="'+url+'" title="'+title+'"/></a><p style="text-align:center"><strong><a href="'+orig+'">Klik op de afbeelding voor een grotere versie</a></strong></p>';
		$('#pers_image').html(image);
		$('#pers_image').fadeIn('fast');
			calibrate_popup();
			
			$('#pers_image img').mouseover(function(){
				$(this).fadeTo('fast',0.6);
			});
			$('#pers_image img').mouseout(function(){
				$(this).fadeTo('fast',1);
			});
	});

}


function show_team(){
	var html = $('#popup_con').html();
	$(actief_popup).html(html);
	show_popup('#foodboxers_team');
}

function close_popup(){
	var html = $('#popup_con').html();
	$(actief_popup).html(html);
	
	$('#popup_box').fadeOut("fast",function(){
		$('#overlay').fadeOut("fast");
	});
}

function getPageSize() {

   var xScroll, yScroll;

   if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
     xScroll = document.body.scrollWidth;
     yScroll = document.body.scrollHeight;
   }
   else if (window.innerHeight && window.scrollMaxY) {
     xScroll = window.innerWidth + window.scrollMaxX;
     yScroll = window.innerHeight + window.scrollMaxY;
   }
   // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari.
   else {
     xScroll = document.body.offsetWidth;
     yScroll = document.body.offsetHeight;
   }

   var windowWidth, windowHeight;

   if (self.innerHeight) { // All except Explorer.
     if (document.documentElement.clientWidth) {
       windowWidth = document.documentElement.clientWidth;
     }
     else {
       windowWidth = self.innerWidth;
     }
     windowHeight = self.innerHeight;
   }
   // Explorer 6 Strict Mode.
   else if (document.documentElement && document.documentElement.clientHeight) {
     windowWidth = document.documentElement.clientWidth;
     windowHeight = document.documentElement.clientHeight;
   }
   else if (document.body) { // Other Explorers.
     windowWidth = document.body.clientWidth;
     windowHeight = document.body.clientHeight;
   }


   // For small pages with total height less then height of the viewport.
   if (yScroll < windowHeight) {
     pageHeight = windowHeight;
   }
   else {
     pageHeight = yScroll;
   }


   // For small pages with total width less then width of the viewport.
   if (xScroll < windowWidth) {
     pageWidth = windowWidth;
   }
   else {
     pageWidth = xScroll;
   }

   arrayPageSize = [pageWidth, pageHeight, windowWidth, windowHeight];
   return arrayPageSize;
}



//Controle
function check_input_text(extra_id,id,e,val){
		if (!e) e = window.event
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		
		var max_char = 125;
		var number = String(val).length;

		if(number >= 0){
			var left = max_char-number; 
			var text = "You have "+left+" characters left (maximum characters : "+max_char+")";
			$(extra_id + " .text").html(text);
			if($('#popup_box '+extra_id).css("display") != "block"){
				$('#popup_box '+extra_id).slideDown("slow");
			}
			
			$("#popup_box #error_"+id).removeClass("error");		
		}
		if(number == 0){
			$("#popup_box #error_"+id).addClass("error");
		}		
}


function check_email_text(extra_id,id,e,val){
		if (!e) e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (filter.test(val)) {
			$("#popup_box #error_"+id).removeClass("error");		
			$('#popup_box '+extra_id).slideUp("slow");
		}else{
			$("#popup_box #error_"+id).addClass("error");
			
			var text = "Uw email adres is niet correct";
			$('#popup_box '+extra_id + " .text").html(text);
			if($('#popup_box '+extra_id).css("display") != "block"){
				$('#popup_box '+extra_id).slideDown("slow");
			}
			
		}
	
}


function check_input_textarea(extra_id,id,e,val){
	if (!e) e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	var number = String(val).length;
	//alert(code);
	/*if(number == 0){
		
	}*/
	if(number >= 0){
		if($('#popup_box '+extra_id).css("display") != 'block'){
			$('#popup_box '+extra_id).slideDown("slow");
		}

		if($('#popup_box '+id).css("display") != "block"){
			$('#popup_box '+id).slideDown("fast");
		}
		$("#popup_box #error_"+id).removeClass("error");				
	}
	if(number == 0){
		
		$("#popup_box #error_"+id).addClass("error");
		$('#popup_box '+extra_id).slideUp("slow");
	}
}

function check_input_textarea_error(id,e,val){
	var number = String(val).length;
	if(number >= 0){
		$("#popup_box #error_"+id).removeClass("error");				
	}else{
		$("#popup_box #error_"+id).addClass("error");
		
	}
}

function check_contact(){
	var length = $('#popup_box .error');
	if(length.length == 1){
		return true;
	}else{
		$('#popup_box .error_return').show();
		return false;
	}
}

function post_contact(){
	if(check_contact()){
		$('#popup_box .error_return').hide();
		$.ajax({
		  type: 'POST',
		  url: 'post.php',
		  data: { naam: $('#popup_box #naam').val(), email: $('#popup_box #email').val(),booschap : $('#popup_box #boodschap').val(),func : 'contact' },
		  dataType: 'json'
		});
		
		$('#popup_box .main_table').slideUp('slow',function(){
			//$('#popup_box .error_return').html("<div class='input_check'></div>&nbsp;Uw bericht is goed aangekomen. U zal een bevestiging vinden in uw mailbox.");
			$('#popup_box .main_table').html("Uw bericht is goed aangekomen.<br/>U zal een bevestiging vinden in uw mailbox.");
			$('#popup_box .main_table').slideDown("fast");
		});
		
	}
}


function post_wijnhandelaar(){
	var meer_foodboxers= 0;
	var prijzen_fnwp= 0;
	if($('#meer_foodboxers').is(':checked')){
		meer_foodboxers =1;
	}
	if($('#prijzen_foodboxers').is(':checked')){
		prijzen_fnwp =1;
	}
	
	if(check_contact()){
		$('#popup_box .error_return').hide();
		$.ajax({
		  type: 'POST',
		  url: 'post.php',
		  data: { naam: $('#popup_box #naam').val(), email: $('#popup_box #email').val(),adres : $('#popup_box #adres').val(),webadres : $('#popup_box #webadres').val(),booschap : $('#popup_box #boodschap').val(),afspraak : meer_foodboxers,prijzen : prijzen_fnwp,func : 'contact_wijnhandelaar' },
		  dataType: 'json'
		});
		
		$('#popup_box .main_table').slideUp('slow',function(){
			//$('#popup_box .error_return').html("<div class='input_check'></div>&nbsp;Uw bericht is goed aangekomen. U zal een bevestiging vinden in uw mailbox.");
			$('#popup_box .main_table').html("Uw bericht is goed aangekomen.<br/>U zal een bevestiging vinden in uw mailbox.");
			$('#popup_box .main_table').slideDown("fast");
		});
		
	}
}


