﻿(function($) {
	$(document).ready(function() {
		
		$('a[rel="external"]').live('click',function() {
           var href = $(this).attr('href');
           window.open(href);
           return false;
		})
		
		src = $("#relayImg")[0].src;
		$("#relayD").html("");
		R = Raphael("relayD", 73, 65),
			img = R.image(src, 0, 0, 73, 65);
		R.safari();
		
		count = 0;
		setInterval(function() {
			count++;
			if(count%2 == 0) 
				$('#timeBox').css('background','none');
			else
				$('#timeBox').css('background','url(../icos/dots.gif) no-repeat center center');
		},1000);
		
		$('#home').bind('click',function() {
			obj = $(this);
			img.animate({rotation:0},1000,"<>",function() {
				animationCallback(obj);
			});
		})
		
		$('#contacts').bind('click',function() {
			obj = $(this);
			img.animate({rotation:45},1000,"<>",function() {
				animationCallback(obj);
				$('#contactInfo').fadeIn();
			});
		})
		
		$('#turnOff').bind('click',function() {
			obj = $(this);
			img.animate({rotation:90},1000,"<>",function() {
				animationCallback(obj);
				$('#childLock').fadeIn();
			});
		})
		
		$('#mail').bind('click',function() {
			obj = $(this);
			img.animate({rotation:130},1000,"<>",function() {
				animationCallback(obj);
				$('#contactForm').fadeIn();
			});
		})
		
		$('#order').bind('click',function() {
			obj = $(this);
			img.animate({rotation:180},1000,"<>",function() {
				animationCallback(obj);
			});
		})
		
		$('#portfolio').bind('click',function() {
			obj = $(this);
			img.animate({rotation:180},1000,"<>",function() {
				animationCallback(obj);
				$('#portfolioBlock').fadeIn();
			});
		})
		
		$('.close').live('click',function() {
			obj = $(this);
			img.animate({rotation:0},1000,"<>",function() {
				$('#relay a').removeClass('active');
				$('#home a').addClass('active');
				$(obj).parent().fadeOut();
			});
		})
		
		$('#sendMessage').live('click',function() {
			sendForm();
		});
		
		time();
		setInterval(function() {	
			time();
		},10000)
		
	})
	
	time = function() {
		var date = new Date();
		var hours = date.getHours().toString().split('');
		var minutes = date.getMinutes().toString().split('');
		$('#timeBox span').fadeIn();
		if(hours.length < 2) {
			hours = '<span id="digitGray0">&nbsp;</span><span id="digit'+hours[0]+'">&nbsp;</span>';
		} else {
			hours = '<span id="digit'+hours[0]+'">&nbsp;</span><span id="digit'+hours[1]+'">&nbsp;</span>';
		}
		if(minutes.length < 2) {
			minutes = '<span id="digit0">&nbsp;</span><span id="digit'+minutes[0]+'">&nbsp;</span>';
		} else {
			minutes = '<span id="digit'+minutes[0]+'">&nbsp;</span><span id="digit'+minutes[1]+'">&nbsp;</span>';
		}
		
		$('#timeBox').html(hours+' '+minutes);
	}
	
	animationCallback = function(object) {
		$(object).parent().find('a').removeClass('active');
		$(object).find('a').addClass('active');
		$('#crml div').fadeOut();
	}
	
	sendForm = function() {
		var regMail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var regName = /^([A-Za-zА-Яа-я ]){4,50}$/;
		var error = 0;
		if ($('#name').val() == 'your name' || !regName.test($('#name').val())) {
			$('#name').addClass('notValid');
			error++;
		} else 
			$('#name').removeClass('notValid');
			
		if ($('#email').val() == 'your e-mail' || !regMail.test($('#email').val())) {
			$('#email').addClass('notValid');
			error++;
		} else 
			$('#email').removeClass('notValid');
	
		if ($('#message').val() == 'your message') {
			$('#message').addClass('notValid');
			error++;
		} else 
			$('#message').removeClass('notValid');
			
		if(error == 0) {
			var data = $('#contactForm form').serialize();
			$.ajax({
				url:'sendMessage.php',
				type:'POST',
				data:data,
				success:function(response) {
					$('#contactForm h2').html(response);
				}
			});
		} else {
			$('#contactForm h2').html('check form for errors');
		}
	}

})(jQuery);
