$(function () {

	$('#subnav .content').data('timeout', 0);
	
	$('#navigation .content li:not(.last) a')
		.mouseover(function () {
			var thiz = this;
			$('#navigation .content li:not(.last) a').removeClass('active');
			$(thiz).addClass('active');
			/*
			$('#subnav .content ul')
				.html($(thiz).siblings().html());
				
			$('#subnav .content')
				.animate(
					{opacity: 1},
					{duration: 300}
				);
			*/
			
			$('#subnav .content')
				.stop()
				.animate(
					{opacity: 0},
					{
						duration: 100,
						complete: function () {
							$('#subnav .content ul')
								.html($(thiz).siblings().html());
								
							$('#subnav .content')
								.animate(
									{opacity: 1},
									{duration: 200}
								);
						}
					}
				);
				
			clearTimeout( $('#subnav .content').data('timeout') );
		})
		.mouseout(function () {
			$('#navigation .content li:not(.last) a').removeClass('active');
			var x = setTimeout(function () {
				$('#subnav .content')
					.stop()	
					.animate(
						{
							opacity: 0
						},
						{
							duration: 300,
							complete: function () {
								$('#subnav .content ul').html('');
							}
						}
					);
			}, 500);
			$('#subnav .content').data('timeout', x);
		});
		
		$('#subnav .content')
			.mouseover(function () {
				clearTimeout( $('#subnav .content').data('timeout') );
			})
			.mouseout(function () {
				var x = setTimeout(function () {
					$('#subnav .content')
						.stop()
						.animate(
							{
								opacity: 0
							},
							{
								duration: 300,
								complete: function () {
									$('#subnav .content ul').html('');
								}
							}
						);
				}, 500);
				$('#subnav .content').data('timeout', x);
			});
		
	
		$('#games')
			.coinslider({
				links: true,
				width: 760,
				height: 350
			});
			
		$('#games')
			.css('display', 'block');
		
		$('.cs-buttons')
			.css({
				position: 'absolute',
				top: '0px',
				left: '0px',
				margin: 0
			})
			.find('a')
				.each(function (i) {
					$('<span></span>')
						.html( i+1 )
						.appendTo( $(this) );
				});
				
				
	function emailCheck(emailStr) {
              patt = /^[_a-zA-Z0-9-](\.{0,1}[_a-zA-Z0-9-])*@([a-zA-Z0-9-]{2,}\.){0,}[a-zA-Z0-9-]{1,}(\.[a-zA-Z]{2,4}){1,2}$/;
      		if (emailStr.match(patt)) {
      		    return true;
      		} else {
      		    return false;
      		}
          }

	$('#wrapper .content form :text, #wrapper .content form textarea')
		.data('valid', false)
		.bind('focus', function () {
			$(this)
				.css('border', '1px solid #6cf');
		})
		.bind('blur', function () {
			if ( $(this).val() == '' ) {
				$(this)
					.css('border', '1px solid #f60')
					.data('valid', false);
			} else {
				if ( $(this).attr('name') == 'email' ) {
					if ( emailCheck( $(this).val() ) ) {
						$(this)
							.css('border', '1px solid #9c0')
							.data('valid', true);
					} else {
						$(this)
							.css('border', '1px solid #f60')
							.data('valid', false);
					}
				} else {
					$(this)
						.css('border', '1px solid #9c0')
						.data('valid', true);
				}
			}
		});
		
	$('#wrapper .content form')
		.bind('submit', function ( e ) {
			$('#wrapper .content form :text, #wrapper .content form textarea')
				.trigger('focus')
				.trigger('blur');
				
			$('#wrapper .content form :text, #wrapper .content form textarea')
				.each(function () {
					if ( ! $(this).data('valid') ) {
						e.preventDefault();
						alert('لطفا تمامی فیلدها را به درستی پرنمایید.');
						return false;
					}
				});
		});
	
	
});
