$(document).ready(function(){

	/* Converting the #contact-form div into a bounceBox: */
	$('#contact-form').bounceBox();
	$('a#page0').addClass('current');
	
/* 	setTimeout(function() {
		var newdiv = document.createElement('div');
		newdiv.setAttribute('id', 'free-widget');
		document.getElementsByTagName("body")[0].appendChild(newdiv);	
		//$('#free-widget').css('display','block');
		$('#free-widget').html( '<img src="img//button_buy_now.gif">' );
		$('#free-widget').animate({height: 'toggle'}, 800 )
		e.preventDefault();
	}, 1000); */
	
	
	function isValidEmail (email, strict) {
		if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
		return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
	}
	
	//  work with comment's pages 
	$('a[id*=page]').click(function(e){
		var p = this.id;
		$('div[class*=page]').addClass('hidden');
		$('a[id*=page]').removeClass('current');
		$('div[class*='+p+']').removeClass('hidden');
		$('a[id*='+p+']').addClass('current');
		e.preventDefault();
	});	
	//$('a[id*=page0]').click();
	
	/* Listening for the click event and toggling the contact-form: */
	$('a.cf-button').click(function(e){
		document.getElementById("cf-name").value = '';
		document.getElementById("cf-email").value = '';
		document.getElementById("cf-mess").value = '';
		$('#contact-form').bounceBoxToggle();
		e.preventDefault();
	});
	
	/* When the contact-form close is clicked, hide it: */
	$('#contact-form a.close').click(function(){
		$('#contact-form').bounceBoxHide();
	});
	
	/* close comment form */
	$('#comment-form a.close').click(function(){
		$('#comment-form').css('display','none');
		var elem = document.getElementById('blackout');
		(elem.parentNode).removeChild(elem);			
		document.body.style.overflow = 'visible';		
	});	
	
	/* Listening for the click event and toggling the comment-form: */
	$('a.comment-button').click(function(e){
		if  ( $('div').hasClass('blackout') ) {
			$('#comment-form').css('display','none');
			var elem = document.getElementById('blackout');
			(elem.parentNode).removeChild(elem);			
			document.body.style.overflow = 'visible';
		} else {
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id', 'blackout');
			newdiv.setAttribute('class', 'blackout');
			document.getElementsByTagName("body")[0].appendChild(newdiv);	
			document.body.style.overflow = 'hidden';
			document.getElementById("comment-name").value = '';
			document.getElementById("comment-email").value = '';
			document.getElementById("comment-mess").value = '';
			$('#comment-form').css('display','block');
		}	
		e.preventDefault();
	});	
	
	$('input#comment-submit').click(function(e) {
		process_forms( this );
	});
	
	$('input#submit').click(function(e) {
		process_forms( this );	
	});
	
	function process_forms( elem ) {
		//var spinner = $('span#spinner');
		//spinner.css('display','inline');
		//var formcorrect = true;
		var formstr = '';
		var do_action = '';
		var p = $(elem).parent().get(0).id;
		if ( "comment" == p ) {
			var cfname 	= document.getElementById("comment-name").value;
			var cfemail 	= document.getElementById("comment-email").value;
			var cfmess 	= document.getElementById("comment-mess").value;
			do_action = 'process_comment_form';
		
		} else if ( "form" == p ) { 
			var cfname 	= document.getElementById("cf-name").value;
			var cfemail 	= document.getElementById("cf-email").value;
			var cfmess 	= document.getElementById("cf-mess").value;
			do_action = 'process_contact_form';
			
		}	
		if ( !isValidEmail(cfemail, true) ) {
			formstr = "e-mail введен не корректно\n\n"; 
			//formcorrect = false;
		}
		if ( cfname.length == 0 ) {
			formstr = formstr+"имя не введено\n\n"; 
			//formcorrect = false;	
		}
		if ( cfmess.length == 0 ) {
			formstr = formstr+"нет текста сообщения"; 
			//formcorrect = false;	
		}	
		if ( formstr.length !== 0 ) {
			alert(formstr);
			return;
		}
		$.ajax({
			type: 'POST',
			url:'functions.php',
			data: {
				action : do_action,
				name: cfname,
				email: cfemail,
				mess: cfmess
			},
			success:function(data) {
				if ( do_action == 'process_comment_form' ) {
					$('a.comment-button').click();
					window.location = "http://macsale.info/comments.htm";
				} else if ( do_action == 'process_contact_form' ) {
					$('a.cf-button').click();

				}	
			} ,
			dataType:"html"
		});
	}
});

