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;
	}
}

function in_array(needle, haystack, strict) {  
	var found = false, key, strict = !!strict;  

	for (key in haystack) {  
	    if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {  
	         found = true;  
	        break;  
	    }  
	}  

	return found;
}

var _sections = ['homepage', 'classes', 'workshop', 'seminar', 'dotnet', 'js'];
var url = window.location.toString().split('/');
var section = 'homepage';
for (var i = 0, l = url.length; i < l; i++) {
	if (in_array(url[i], _sections)) {
		section = url[i];
	}
}
for (var i = 0, l = _sections.length; i < l; i++) {
	if (section == _sections[i]) {
		section = i;
		break;
	}
}


if ($.browser.msie && $.browser.version == '6.0') {
	jqPngFix();
}

/*
$('#navigation .content li:not(.last) a.mainnavlink')
	.mouseover(function () {
		var thiz = this;
		$('#navigation .content li:not(.last) a').removeClass('active');
		$(this).addClass('active');
		if ($.browser.msie) {
			$('#subnav .content')
				.html('<ul>' + $(thiz).siblings().html() + '</ul>');
			return false;
		}
		$('#subnav .content ul')
			.stop()
			.fadeTo('fast', 0, function () {
				$('#subnav .content ul')
					.html($(thiz).siblings().html())
					.fadeTo('slow', 1);
			});
		
	})
	.eq(section).mouseover();
*/
$('.kwicks')
	.kwicks({
		max : 360,
		spacing : 5,
		sticky: false,
		duration: 850
	});
	
$('#homepage-leftcontent .newsletter form')
	.submit(function () {
		var thiz = this;
		
		if (emailCheck($(this).find('.textbox').val())) {
			if ($.browser.msie && $.browser.version == '6.0') {
				return true;	
			}
			
			if ($(thiz).data('processing')) {
			} else {
				$(thiz).data('processing', true);
				var email = $(this).find('.textbox').val();
				$.ajax({
					type: 'post',
					url: $(this).attr('action'),
					data: 'email=' + email + '&form=newsletter&ajax=true',
					timeout: 4000,
					beforeSend: function () {
						$('#homepage-leftcontent .newsletter .loading')
							.css('height', $('#homepage-leftcontent .newsletter').height())
							.css('opacity', 0.8)
							.css('display', 'block');
					},
					success: function (html) {
						$('#homepage-leftcontent .newsletter .loading')
							.css('display', 'none');
						$('#homepage-leftcontent .newsletter form')
							.remove();
						$('#homepage-leftcontent .newsletter p')
							.css('opacity', 0)
							.html(html)
							.fadeTo('slow', 1);
							
						$(thiz).data('processing', false);
					},
					error: function () {
						$('#homepage-leftcontent .newsletter .loading')
							.css('display', 'none');
						alert('خطا! لطفا مجددا سعی کنید');
						$(thiz).data('processing', false);
					}
				});
			}
		}
		
		return false;
	});
	
$('#homepage-rightcontent .buttons .signup')
	.click(function () {
		if ($.browser.msie && $.browser.version == '6.0') {
			return true;	
		}
		
		$('#homepage-rightcontent .signupform')
			.slideToggle('slow');
		return false;
	});
	
$('#homepage-rightcontent #tws .signupform form')
	.submit(function () {
		if (
			   !emailCheck($(this).find('input[name="tws_email"]').val())
			|| $(this).find('input[name="tws_name"]').val() == ''
			|| $(this).find('input[name="tws_phone"]').val() == ''
		) {
			return false;
		}
		if ($.browser.msie && $.browser.version == '6.0') {
			return true;	
		}
		
		var thiz = this;
		
		if ($(thiz).data('processing')) {
		} else {
			$(thiz).data('processing', true);
			var name  = $(this).find('input[name="tws_name"]').val(),
			    email = $(this).find('input[name="tws_email"]').val(),
			    upcoming = $(this).find('input[name="tws_upcoming"]').val(),
			    phone = $(this).find('input[name="tws_phone"]').val();
	
			$.ajax({
				type: 'post',
				url: $(this).attr('action'),
				data: 'tws_name=' + name + '&tws_email=' + email + '&tws_upcoming=' + upcoming + '&tws_phone=' + phone + '&form=tws&ajax=true',
				timeout: 4000,
				beforeSend: function () {
					$('#homepage-rightcontent #tws .signupform .loading')
						.css('height', $('#homepage-rightcontent #tws .signupform').height())
						.css('opacity', 0.8)
						.css('display', 'block');
				},
				success: function (html) {
					$('#homepage-rightcontent #tws .signupform .loading')
						.css('display', 'none');
						
					if (html == 'ERROR') {
						$('#homepage-rightcontent #tws .signupform .content')
							.find('.tws-signupform-result-error')
								.remove()
								.end()
							.prepend('<p style="margin-bottom: 6px; background: #FF4E70; color: #fff; padding: 2px 15px 4px;" class="tws-signupform-result-error">لطفا اطلاعات را به صورت صحیح وارد نمایید</p>');
					} else {
						$('#homepage-rightcontent #tws .signupform form')
							.remove();
						$('#homepage-rightcontent #tws .signupform')
							.find('.tws-signupform-result-error')
								.remove()
								.end()
							.find('p')
								.css('opacity', 0)
								.html(html)
								.fadeTo('slow', 1);
					}
					
					$(thiz).data('processing', false);
				},
				error: function () {
					$('#homepage-rightcontent #tws .signupform .loading')
						.css('display', 'none');
					
					alert('خطا! لطفا مجددا سعی کنید');
					$(thiz).data('processing', false);
				}
			});
		}
		
		return false;
	});

$('.kwicks .link')
	.css('opacity', 0);
	
$('.kwickc')
	.css('display', 'block');

$('a[rel=external]')
	.attr('target', '_blank');