jQuery(document).ready(function($)
{
	$('a[rel~="external"], .press-item a.external').attr({target: '_blank'});
	$('#nav ul li:last-child, #sidebar .video ul li:last-child, #footer .footer-column:last, .tweets .tweet:last-child, #sidebar .posts ul li:last').addClass('last');
	$('.right-part img.b-space:last-child').removeClass('b-space');
	$('#main .tiny-box:not(:last-child, .smile_gallery, .twitter_box)').addClass('r-space');
	$('#sidebar .theme-widget-youtube-videos .video a').attr('target', '_blank');
	
	$('.press-item .press_thumbnail[href$="jpg"]').fancybox();
	
	$('.press-item').each(function() {
		if (!(($(this).index()-1)%3)) {
			$(this).css({ clear: 'left' });
		}
	});
	
	var welcome_slider = $('#welcome_slider');
	var slider = welcome_slider.find('> .slider');
	var slide_duration = 400;
	var slide_interval_pause = 5000;
	var slide_interval = false;
	
	if (slider.find('> li').length > 1) {
		function slide_to(index) {
			var is_interval_set = false;
			
			if (slide_interval) {
				var is_interval_set = true;
				clear_slider_timeout();
			}
			
			var slide_left = index * $(slider.find('> li')[0]).width();
			slider.animate({left: '-' + slide_left + 'px'}, slide_duration);
			slider.data('current_slide', index);
			welcome_slider.find('.pagination a').removeClass('active');
			welcome_slider.find('.pagination a:eq(' + index + ')').addClass('active');
			
			if (is_interval_set !== false) {
				set_slider_timeout();
			}
		}
		
		function slide_next() {
			var next_index = slider.data('current_slide') + 1;
			if (next_index >= slider.find('> li').length) {
				next_index = 0;
			}
			slide_to(next_index);
		}
		
		function slide_previous() {
			var prev_index = slider.data('current_slide') - 1;
			if (prev_index < 0) {
				prev_index = slider.find('> li').length - 1;
			}
			slide_to(prev_index);
		}
		
		function set_slider_timeout() {
			slide_interval = setInterval(function() {
				slide_next();
			}, slide_interval_pause);
		}
		
		function clear_slider_timeout() {
			clearInterval(slide_interval);
			slide_interval = null;
		}
		
		welcome_slider.find('.pagination a').click(function() {
			slide_to($(this).attr('rel'));
			return false;
		});
		
		welcome_slider.find('.navigation a.next').click(function() {
			slide_next();
			return false;
		});
		
		welcome_slider.find('.navigation a.prev').click(function() {
			slide_previous();
			return false;
		});
		
		welcome_slider.find('.navigation a.play').live('click', function() {
			set_slider_timeout();
			$(this).removeClass('play').addClass('pause');
			return false;
		});
		
		welcome_slider.find('.navigation a.pause').live('click', function() {
			clear_slider_timeout();
			$(this).removeClass('pause').addClass('play');
			return false;
		});
		
		set_slider_timeout();
	}
	
	$('.field').
		focus(function() {
			if(this.title==this.value) {
				this.value = '';
			}
		}).
		blur(function(){
			if(this.value=='') {
				this.value = this.title;
			}
		});
	
	$('marquee').marquee('pointer').mouseover(function () {
	    $(this).trigger('stop');
	}).mouseout(function () {
	    $(this).trigger('start');
	});
	
	$('form.contact, form.bookings').submit(function() {
		var errors = '';
		var valid = true;
		
		$(this).find('input.field').each(function() {
			if (($(this).val() == '') || $(this).val() == $(this).attr('title')) {
				$(this).addClass('error-field');
				if (valid) {
					$(this).focus();
				}
				errors = 'All fields are required\n';
				valid = false;
			} else {
				$(this).removeClass('error-field');
			}
		});
		
		$(this).find('input.email').each(function() {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			if (!pattern.test($(this).val())) {
				$(this).addClass('error-field');
				if (valid) {
					$(this).focus();
				}
				errors += 'Please enter a valid email address\n';
				valid = false;
			} else {
				$(this).removeClass('error-field');
			}
		});
		
		if (!valid) {
			alert(errors);
		};
		
		return valid;
	});
});

Cufon.replace('#nav li a', {hover:true});
Cufon.replace('.post h2 a', {hover:true});
Cufon.replace('.subscribe a.sub-btn', {hover:true});
Cufon.replace('h3');
Cufon.replace('h2');
Cufon.replace('.left-part h2');
Cufon.replace('strong.date');
