$(document).ready( function() {
	
	$('#zaloguj_button').click(function(){		
		$('#auth_form').submit();		
		return false;
	});
	
	$('.reklama').click(function(){
		var id=$(this).attr('id');
		$.ajax({
			  type: 'GET',
			  url: "/ajax_reklama/click?id="+id,
			  //data: fdata,
			  //dataType: "json",
			  success: function(msg){
					//alert(msg); 
			  }
			});
	}); 	
	

	$('.window .close').click(function (e) {
		e.preventDefault();		
		$('#mask').hide();
		$('.window').hide();
	});
	
	//usun maske i pozamykaj okna modalne
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
	
	dodaj_maske = function(){
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});				
		//transition effect
		//$('#mask').fadeIn(500);
		$('#mask').fadeTo("slow",0.6);	
	};
	
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {		
		$('.error').remove();
		$('#guestbook_dialog').css('height', 350 );				
		e.preventDefault();//Cancel the link behavior				
		var id = $(this).attr('href');//Get the A tag			
		dodaj_maske();
		//Get the window height and width
		//var winH = e.pageY+300; //$(window).height(); //+e.pageY;
		//var winW = $(window).width();		             
		//Set the popup window to center
		var cw=document.body.clientHeight -100 - (e.pageY + 450);
		var top = ( cw < 0) ? e.pageY + cw : e.pageY;
		var left = e.pageX-240;
		$(id).css({'left':left,'top':top});
		$('#guestbook_dialog.window').css({'left':left,'top':top});
		
		
		//$(id).css('top',  e.pageY+10); //(winH/2-$(id).height()/2)+'px');
		//$(id).css('left', e.pageX-10); //(winW/2-$(id).width()/2)+'px');
		$(id).fadeIn(1000);
	});

	// send to guestbook
	$('#guestbook_send').click(function () {
		$('#guestbook_dialog.window').hide();	
		var dataString = $("#guestbook_form").serialize();
		$.ajax({
		   type: "POST",
		   url: "ajax_guestbook/dodaj",
		   data: dataString,
		   scriptCharset: "utf-8" ,		   				   
		   success: function(msg){
			   if (msg[0]=='<'){
				   $('#guestbook_dialog_content').html(msg);			 	
				   $('#guestbook_dialog.window').show();
				   $('#guestbook_dialog').css('height', $('#guestbook_dialog_content').height()+35 );
			   }else{
				   window.location.reload();
			   }
		   }
		 });
	});


	$("#inhalt").keyup(function() {
		var max = 500;
		var box=$(this).val();
		var main = box.length *100;
		var value= (main / max);
		var count= max - box.length;
		
		if(box.length <= max) {
			$('#count').html(count);
			$('#senden').attr("disabled",false);
			//$('#bar').animate( { "width": value+'%', }, 1 );
		} else {
			$('#senden').attr("disabled",true);
			$(this).val($(this).val().substring(0,max - 1));
		}
		return false;
	});

	
	//select all the a tag with name equal to modal
	$('a[id^=naduzycie_btn]').click(function(e) {		
		$('.error').remove();
		var id = $(this).attr('href');
		$('input[name^=komentuj_btn]').attr('value',$(this).attr('id'));		
		$(id).css('height', 350 );
		$(id).css('width', 450 );	
		//Cancel the link behavior
		e.preventDefault();				
		dodaj_maske();			             
		//Set the popup window to center
		var cw=document.body.clientHeight -100 - (e.pageY + 450);
		var top = ( cw < 0) ? e.pageY + cw : e.pageY;
		var left = e.pageX-540;
		$(id).css({'left':left,'top':top});
		$('#inform_dialog.window').css({'left':left,'top':top});		
		//transition effect
		$(id).fadeIn(1000);
	});

	$('#naduzycie-send').click(function () {
		$('#naduzycie_dialog.window').hide();	
		var dataString = $("#naduzycie_form").serialize()+'&'+$("#hidden_values").serialize();
		$.ajax({
		   type: "POST",
		   url: "ajax_naduzycie/dodaj",
		   data: dataString,
		   scriptCharset: "utf-8" ,		   				   
		   success: function(msg){			   
			   if (msg[0]=='<'){				
 				$('#naduzycie_dialog_content').html(msg);
 				$('#naduzycie_dialog.window').show();
 				$('#naduzycie_dialog').css('height', $('#naduzycie_dialog_content').height()+30 );
			   } else {
				   show_inform(msg);
			   }
		   }
		 });
	});
	
	//select all the a tag with name equal to modal
	$('a[id^=komentuj_btn]').click(function(e) {		
		$('.error').remove();
		var id = $(this).attr('href');
		$('input[name^=komentuj_btn]').attr('value',$(this).attr('id'));		
		$(id).css('height', 280 );				
		//Cancel the link behavior
		e.preventDefault();				
		dodaj_maske();			             
		//Set the popup window to center				
		var cw=document.body.clientHeight -100 - (e.pageY + 450);
		var top = ( cw < 0) ? e.pageY + cw : e.pageY;
		var left = ($(this).attr('id')=='komentuj_btn_0') ?  e.pageX : e.pageX-400;
		$(id).css({'left':left,'top':top});
		$('#inform_dialog.window').css({'left':left,'top':top});
		
		//transition effect
		$(id).fadeIn(1000);
	});

	$('#komentarz-send').click(function () {
		$('#komentarz_dialog.window').hide();	
		var dataString = $("#komentarz_form").serialize()+'&'+$("#hidden_values").serialize();;
		$.ajax({
		   type: "POST",
		   url: "ajax_komentarz/dodaj",
		   data: dataString,
		   scriptCharset: "utf-8" ,		   				   
		   success: function(msg){
			   // spowrotem formularz
			   if (msg[0]=='<'){			   
				   $('#komentarz_dialog_content').html(msg);
			 	   $('#komentarz_dialog.window').show();
				   $('#komentarz_dialog').css('height', $('#komentarz_dialog_content').height()+35 );
				}else{
					window.location.reload();
				}
		   }
		 });
	});
	
	
	//modalne okno informacyjne
	$('a[id^=inform_dialog_close]').click(function(e) {						
		$('#inform_dialog.window').fadeOut(500);
	});
	
	show_inform = function(msg){	
	    $('#inform_dialog_content').html(msg);
		$('#inform_dialog.window').show();		
	};
});
