jQuery(function($) {
	$('.custom-select').selectbox();
	
	//enable this if you want to scroll directly to content and skip seeing the slider
	//$(document).scrollTop($('.page-title').offset().top);
	
	$('.btn-purple, .btn-search').css({
		'text-shadow' : '0 -1px 0 #710733'
	});
	
	$(".portfolio .preview a:not([rel=project])").fancybox({
		padding: 5,
		hideOnContentClick: true
	});
	
	function header_slider() {
		if ($.browser.msie && $.browser.version == '8.0') {
			var slider = $('#slideshow .slide-shadow');
			var slides = slider.find('li');
			if (slides.length == 1) {
				return;
			}
			slides.css('position', 'absolute');
			slides.hide();
			var slides_num = slides.length;
			
			slideControlls = '<div class="slidecontrolls">';
			for (var i=0; i<slides_num; i++) {
				slideControlls += '<a class="ie6fix" href="#">&nbsp;</a>';
			}
			slideControlls += '</div>';
			
			slider.parent().append(slideControlls);
			
			slideControlls = $(slider.parent().find('.slidecontrolls'));
			var links = slideControlls.find('> a');
			
			slideControlls.css('float', 'none');
			slideControlls.width(links.length * (links.width()+1));
			
			var current_slider_position = 0;
			
			function move_slider_to(index) {
				if (current_slider_position == index) {
					slider.find('li').eq(index).show();
					return;
				} else if (index >= slides_num-1) {
					index = -1;
				}
				
				clearTimeout(slide_change_timeout);
				
				var last_slide = slider.find('li').eq(current_slider_position);
				current_slider_position = index;
				var current_slide = slider.find('li').eq(current_slider_position);
				
				current_slide.css('z-index', '1000');
				last_slide.css('z-index', '1');
				
				current_slide.fadeIn(duration, function() {
					last_slide.hide();
				});
				
				slide_change_timeout = setTimeout(function() {
					links.eq(index+1).click();
				}, pause);
			}
			
			links.click(function() {
				links.removeClass('active_item');
				move_slider_to($(this).index());
				$(this).addClass('active_item');
				return false;
			});
			
			var duration = 500;
			var pause = 3500;
			
			var slide_change_timeout = setTimeout(function() {
				links.eq(current_slider_position+1).click();
			}, pause);
			
			links.eq(0).click();
			
		} else {
			$('#slideshow .slide-shadow').aviaSlider({ blockSize: { height: 100, width: 100 },
				transition: 'slide',
				animationSpeed: 1500,
				betweenBlockDelay:25
			});
		}
	}
	header_slider();
	
	function featured_slider() {
		if ($.browser.msie && $.browser.version == '8.0') {
			var slider = $('.item-slideshow .holder');
			var slides = slider.find('li');
			if (slides.length == 1) {
				return;
			}
			slides.css('position', 'absolute');
			slides.hide();
			var slides_num = slides.length;
			
			slideControlls = '<div class="slidecontrolls">';
			for (var i=0; i<slides_num; i++) {
				slideControlls += '<a class="ie6fix" href="#">&nbsp;</a>';
			}
			slideControlls += '</div>';
			
			slider.parent().append(slideControlls);
			
			slideControlls = $(slider.parent().find('.slidecontrolls'));
			var links = slideControlls.find('> a');
			
			slideControlls.css('float', 'none');
			slideControlls.width(links.length * (links.width()+1));
			
			var current_slider_position = 0;
			
			function move_slider_to(index) {
				if (current_slider_position == index) {
					slider.find('li').eq(index).show();
					return;
				} else if (index >= slides_num-1) {
					index = -1;
				}
				
				clearTimeout(slide_change_timeout);
				
				var last_slide = slider.find('li').eq(current_slider_position);
				current_slider_position = index;
				var current_slide = slider.find('li').eq(current_slider_position);
				
				current_slide.css('z-index', '1000');
				last_slide.css('z-index', '1');
				
				current_slide.fadeIn(duration, function() {
					last_slide.hide();
				});
				
				slide_change_timeout = setTimeout(function() {
					links.eq(index+1).click();
				}, pause);
			}
			
			links.click(function() {
				links.removeClass('active_item');
				move_slider_to($(this).index());
				$(this).addClass('active_item');
				return false;
			});
			
			var duration = 500;
			var pause = 3500;
			
			var slide_change_timeout = setTimeout(function() {
				links.eq(current_slider_position+1).click();
			}, pause);
			
			links.eq(0).click();
			
		} else {
			$('.item-slideshow .holder').aviaSlider({ blockSize: { height: 100, width: 100 }, 
				transition: 'slide',
				animationSpeed: 1500,
				betweenBlockDelay:25

			});
		}
	}
	
	featured_slider();
	
	$('#navigation li').hover(function() {
		$(this).find('.dd').stop(true, true).hide().slideDown();
		$(this).find('a').eq(0).addClass('hover');
	}, function() {
		$(this).find('.dd').stop(true, true).slideUp();
		$(this).find('a').removeClass('hover');
	});
	
	$('#navigation .dd li:first-child').addClass('first-child');
	
	$('.preview a').each(function() {
		var backgroundImage = $(this).find('span').css('background-image');
		$(this).css('background-image', backgroundImage );
	});
	$('.preview a').hover(function() {
		$(this).find('span').stop(true, true).fadeOut(500);
	},
	function() {
		$(this).find('span').stop(true, true).fadeIn(500);
	});
		
	$('.btn-search').hover(function() {
		$(this).toggleClass('hovered');
	});
	
	$('.blink')
	.focus(function() {
		$(this).parents('.field').addClass('focus');
	})
	.blur(function() {
		$(this).parents('.field').removeClass('focus');
	});
	$('.field')
	.focus(function() {
		$(this).addClass('focus');
	})
	.blur(function() {
		$(this).removeClass('focus');
	});
	
	var coun = $('.testimonial').length;
	var idx = 0;
	window.setInterval(function() {
		
		$('#testimonials').scrollTo($('.testimonial').eq(idx++), 500, {offset:-18});
		if( idx == $('.testimonial').length - 1 ) {
			idx = 0;
		}
	}, 8000);
	
	// if( $.browser.msie ) {
	// 	$('.preview a').hover(function() {
	// 		$(this).find('img').css({
	// 			visibility : 'hidden'
	// 		});
	// 	}, function() {
	// 		$(this).find('img').css({
	// 			visibility: 'visible'
	// 		});
	// 	});
	// }
	
	$('form').submit(function() {
		var valid = true;
		var errors = '';
		
		$(this).find('.required').each(function() {
			if ($(this).val() == '') {
				$(this).addClass('field-error');
				errors += 'The "' + $(this).attr('title') + '" field is required\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		var email_field = $(this).find('.email');
		
		if (email_field.length != 0) {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			if (!pattern.test(email_field.val())) {
				email_field.addClass('field-error');
				errors += 'The email address you entered is not valid\n';
				if (valid) {
					email_field.focus();
				}
				valid = false;
			} else {
				email_field.removeClass('field-error');
			}
		}
		
		if (!valid) {
			alert(errors);
		}
		
		return valid;
	});
	
	$(document).ready(function() {
		$('#footer .latest-posts .post-preview:last-child').addClass('noborder');
		$('#content .wp-pagenavi').append('<div class="cl" />');
	});
});
