$(function () {
	
	$('#header .title a, #header .logo a')
		.bind('mouseover', function (e) {
			$(this)
				.stop()
				.animate(
					{opacity: 1},
					{duration: 300}
				);
		})
		.bind('mouseout', function (e) {
			$(this)
				.stop()
				.animate(
					{opacity: 0},
					{duration: 200}
				);
		});
		
	$('#header .rss-mobile .rss')
		.bind('mouseover', function () {
			$('#header .rss-mobile .disable')
				.stop()
				.css('opacity', 0.4)
				.animate({opacity: 0});
			$('#header .rss-mobile').css('background-position', '-10px 0');
		})
		.bind('mouseout', function () {
			$('#header .rss-mobile .disable')
				.stop()
				.css('opacity', 0.5)
				.animate({opacity: 0});
			$('#header .rss-mobile').css('background-position', '0 0');
		});
		
	$('#header .rss-mobile .mobile')
		.bind('mouseover', function () {
			$('#header .rss-mobile .disable')
				.stop()
				.css('opacity', 0.4)
				.animate({opacity: 0});
			$('#header .rss-mobile').css('background-position', '-20px 0');
		})
		.bind('mouseout', function () {
			$('#header .rss-mobile .disable')
				.stop()
				.css('opacity', 0.5)
				.animate({opacity: 0});
			$('#header .rss-mobile').css('background-position', '0 0');
		});
		
	$('#header ul li')
		.each(function (i) {
			var thiz = this;
			
			$(thiz)
				.bind('mouseenter.lq', function (e) {
					$(thiz)
						.addClass('selected');
				})
				.bind('mouseleave.lq', function (e) {
					$(thiz)
						.removeClass('selected');
				})
				.data('mouseenter', function () {
					if ($.browser.msie && $.browser.version == 7.0) {
					}
					else {
						$(thiz).css('z-index', 2000);
					}
					
					$(thiz).find('span')
						.stop()
						.animate(
							{opacity: 1},
							{duration: 300}
						);
					$(thiz).find('a')
						.stop()
						.animate(
							{
								top: 0,
								height: 56
							},
							{duration: 500}
						);
				})
				.data('mouseleave', function () {
					if ($.browser.msie && $.browser.version == 7.0) {
					}
					else {
						$(thiz).parents('ul').eq(0).find('li').css('z-index', 1000);
					}
					
					$(thiz).find('span')
						.stop()
						.animate(
							{opacity: 0},
							{duration: 300}
						);
					$(thiz).find('a')
						.stop()
						.animate(
							{
								top: 56,
								height: 0
							},
							{
								duration: 500,
								complete: function () {
									$(this).css('top', -10);
								}
							}
						);
				});
		});

	$(window)
		.bind('load', function () {
		    if (top === self) { 
		        
		    }
		    else {
		        return false;
		    }
			$('#header ul li')
				.unbind('mouseenter.lq')
				.unbind('mouseleave.lq')
				.bind('mouseenter.hq', function () {
					$(this).data('mouseenter')();
				})
				.bind('mouseleave.hq', function () {
					$(this).data('mouseleave')();
				});
			
			$('#header ul li')
				.each(function (i) {
					$(this).addClass(
						i == 0 ? 'h' :
						i == 1 ? 'i' :
						i == 2 ? 'w' :
						i == 3 ? 'p' :
						i == 4 ? 'd' :
						i == 5 ? 't' : 'c'
					)
					.find('a').html('');
				});
			
		});
		
	$('.content .body ol.reasons').each(function () {
		var diss = this;
		
		
		$(diss).find('li').each(function (i) {
			var thiz = this;
			
			$(thiz)
				.find('.t span')
					.html(i+1);
			
			$(thiz)
				.bind('mouseenter', function (e) {
					clearTimeout( $(diss).data('timeout') );
					$(diss).find('li')
						.each(function (j) {
							if ( i != j ) {
								$(this).find('*')
									// .css('opacity', 0.3);
									
									.stop()
									.animate(
										{opacity: 0.3},
										{duration: 400}
									);
									
							}
							else {
								$(this).find('*').stop().css('opacity', 1);
								// $(this).find('.t').css('color', '#ea3513');
							}
						});
				})
				.bind('mouseleave', function () {
					// $(this).find('.t').css('color', '#000');
					var t = setTimeout(function () {
						$(diss).find(' li *')
							.stop()
							.animate(
								{opacity: 1},
								{duration: 300}
							);
					}, 500);
					$(diss).data('timeout', t);
				});
		});
	});
		
	$("#footer .contactus .comment").fancybox(
		{
		'titlePosition'		: 'inside',
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'centerOnScroll'	: true
		}
	);
	

	$('#slideshow')
		.each(function () {
			var diss = this;
			
			$(diss)
				.find('h3')
					.html('فهرست ' + $(diss).find('ul li').size() + ' پروژه اجرا شده آخر');
			
			$(diss)
				.bind('schedule', function () {
					var t = setTimeout(function () {
						$(diss).trigger('nextitem');
					}, 10000);
					
					$(diss).data('timeout', t);
				})
				.bind('pauseslide', function () {
					$(diss).data('pause', true);
					clearTimeout($(diss).data('timeout'));
				})
				.bind('resumeslide', function () {
					$(diss).data('pause', false);
					$(diss).trigger('schedule');
				})
				.bind('showitem', function (e, n) {
					clearTimeout($(diss).data('timeout'));
					
					$('#slideshow .date, #slideshow .owner, #slideshow .url, #slideshow .picture div').css('opacity', 0);
					
					$(diss).find('ul li a.active')
						.removeClass('active');
					
					var link = $(diss).find('ul li a').eq(n);
					
					link
						.addClass('active');
					
					$('#slideshow .date')
						.find('span')
							.html('تاریخ اجرای پروژه: ')
							.end()
						.find('strong')
							.html(link.attr('data-date'));
							
					$('#slideshow .owner')
						.find('span')
							.html('نام کارفرما: ')
							.end()
						.find('strong')
							.html(link.attr('data-owner'));
							
					$('#slideshow .url a')
						.html(link.attr('data-url'));
					
					$('<img>')
						.attr('src', link.attr('data-img'))
						.appendTo( $('#slideshow .picture div a').html('') );
						
					$('#slideshow .url a, #slideshow .picture div a')
						.attr('href', link.attr('data-href'));
						
					$('#slideshow .date, #slideshow .owner, #slideshow .url, #slideshow .picture div').animate({opacity: 1});
					
					if (! $(diss).data('pause')) {
						$(diss).trigger('schedule');
					}
				})
				.bind('nextitem', function () {
					clearTimeout($(diss).data('timeout'));
					
					var a = $(diss).find('ul li a.active');
					var n = a.parents('li').eq(0).next('li').find('a');
					if (n.size() == 1) {
						n.trigger('click');
					}
					else {
						$(diss).find('ul li a:first')
							.trigger('click');
					}
				})
				.bind('previtem', function () {
					clearTimeout($(diss).data('timeout'));
					
					var a = $(diss).find('ul li a.active');
					var n = a.parents('li').eq(0).prev('li').find('a');
					if (n.size() == 1) {
						n.trigger('click');
					}
					else {
						$(diss).find('ul li a:last')
							.trigger('click');
					}
				});
				
			$(diss)
				.find('ul li a')
					.each(function (i) {
						$(this)
							.html('')
							.bind('click', function (e) {
								e.preventDefault();
								$(diss).trigger('showitem', i);
							})
							.bind('mouseover', function () {
								if ($(this).hasClass('active')) {
									return false;
								}
							})
							.bind('mouseout', function () {
								if ($(this).hasClass('active')) {
									return false;
								}
							});
					});
			
			$(diss)
				.find('.handle')
					.find('div')
						.bind('mouseenter', function () {
							$(this).find('span')
								.stop()
								.animate({opacity: 1});
						})
						.bind('mouseleave', function () {
							$(this).find('span')
								.stop()
								.animate({opacity: 0});
						})
						.bind('click', function () {
							if ($(this).is('.next')) {
								$(diss).trigger('nextitem');
							}
							else {
								$(diss).trigger('previtem');
							}
						});
						
			$(diss)
				.bind('mouseenter', function () {
					$(diss).trigger('pauseslide');
				})
				.bind('mouseleave', function () {
					$(diss).trigger('resumeslide');
				});
		})
		.trigger('showitem', 0);
		
	$('form')
		.bind('submit', function (e) {
			e.preventDefault();
			
			var thiz = this;
			
			$(thiz)
				.find('input, textarea')
					.each(function () {
						$(this)
							.bind('blur', function () {
								if ( $(this).val() == '' ) {
									$(this).addClass('error');
								}
								else {
									$(this).removeClass('error');
								}
							});
							
						if ( $(this).val() == '' ) {
							$(this).addClass('error');
						}
					});
		});
		
	$('.projects-nav a')
		.bind('mouseover', function (e) {
			if (! $(this).hasClass('selected')) {
				$(this)
					.stop()
					.animate(
						{opacity: 1},
						{duration: 300}
					);
			}
		})
		.bind('mouseout', function (e) {
			if (! $(this).hasClass('selected')) {
				$(this)
					.stop()
					.animate(
						{opacity: 0},
						{duration: 200}
					);
			}
		})
		.bind('click', function (e) {
			e.preventDefault();
			
			var thiz = this;
			var _ = $(thiz).attr('href').split('#');
			var id = _[_.length-1];
			
			$('.projects-nav a')
				.removeClass('selected')
				.css('opacity', 0);
				
			$(thiz)
				.css('opacity', 1)
				.addClass('selected');
			
			$('.projects')
				.find('ul')
					.css('display', 'none')
					.end()
				.find('#' + id)
					.css('display', 'block');
		});
		
	$('.products')
		.each(function () {
			var diss = this;
			
			$(diss)
				.find('li:not(.s)')
					.each(function (index) {
						var thiz = this;
						var _timeout = [];
						
						$(thiz)
							.bind('mouseenter', function () {
								clearTimeout( $('.products').data('timeout') );
								
								$(diss).find('li:not(.s)')
									.each(function (j) {
										if (index != j) {
											$(this)
												.stop()
												.animate(
													{opacity: 0.2},
													{duration: 400}
												);
										}
										else {
											$(this).stop().css('opacity', 1);
										}
									});
								
								$(thiz).css('background-position', '0 -212px');
								

							})
							.bind('mouseleave', function () {
								var _t = setTimeout(function () {
									$(diss).find('li:not(.s)')
										.stop()
										.animate(
											{opacity: 1},
											{duration: 300}
										);
								}, 500);
								
								$('.products').data('timeout', _t);
								
								$(thiz).css('background-position', '0 0');

							});
						
						$(thiz).find('h2 span').text(index+1);
					});
					
			$(diss).find('li:last')
				.each(function () {
					if ( $(this).hasClass('s') ) {
						$(this).remove();
					}
				});
		});
		
		$('#gallery a[rel="gallery"]').fancybox({
			'centerOnScroll': true
		});
			
});
