$(document).ready(function() {
	//global vars
	var TEMPLATE_PATH = '/wp-content/themes/silvia';
	
	Cufon.replace('#menu-main li a, .date, .title, #bottom .content, ul.tabs li a, .page-template-contact-tpl-php .col label, .page-template-contact-tpl-php .col p, #sidebar-menu ul li a, .pic-meta .description', {hover: true});
	
	$('#menu-main').show();
	$('#sidebar-menu ul').show();
	
	if($('ul.tabs').length > 0)
		$('ul.tabs').show();
	
	//add carousel action
	$('#slider').nivoSlider({
				effect: 'fade',
				directionNavHide: false
				});
				
	//add pics carousel action				
	var pics_carousel = $('#pics-slider').jcarousel({
					   scroll:1,
					   //auto: 20,
					   animation: 500
					});

	//newsletter
	$('#newsletter-input').focus(function()
	{
		$(this).val('');
	});
	
	//tabs
	if($('.tabs').length > 0)
	{
	
		$('.tabs a').click(function(){
			switch_tabs($(this));
			
			return false;
		});
	 
		switch_tabs($('.defaulttab'));
	}
	
	//gallery details
	$('#gallery-details a').click(function()
	{
		$('.gallery-text').animate({height: 'toggle'}, 300);
		
		return false;
	});
	
	//gallery
	$('#pics-slider ul li a').click(function()
	{
		var infos 	 = $(this).attr('rel').split(';');
		var img_path = infos[0];
		var img_desc = infos[1];

		$('.big-pic a.show').attr('href', '/' + img_path);		
		$('.big-pic img').attr('src', '/wp-content/themes/silvia/image.php?width=740&height=414&cropratio=1.8:1&image=/' + img_path);

		//center big image
		//center_big_image();
		
		$('.pic-meta .title').text($(this).attr('title'));
		$('.pic-meta .description em').text(img_desc);
		
		Cufon.replace('.pic-meta .description, .title');
		
		//move selected
		$('#pics-slider ul li').removeClass('selected');
		$(this).parent('li').addClass('selected');
		
		return false;
	});
	
	//add lightbox
	//$('.big-pic a').lightBox(); // Select all links that contains lightbox in the attribute rel
	
	//add fancybox
	$('.big-pic a, #home-content .left-content a.image').fancybox(
	{
		'overlayColor' : '#000'
	});
	
	//preload images
	$.preLoadImages(TEMPLATE_PATH + '/images/slider-next-over.png', TEMPLATE_PATH + '/images/slider-next-over.png');
		
	
	//Contact form
	$('#contact-me').submit(function(){
		var error = false;
		
		$("#contact-me input[type=text]").removeClass("error");
		$("#contact-me textarea").removeClass("error");
		
		//form validation
		if(!$("#contact-me #name").val()){
			error = true;
			$("#contact-me #name").addClass("error");
		}
		if(!validateEmail($("#contact-me #email").val())){
			error = true;
			$("#contact-me #email").addClass("error");
		}
		if(!$("#contact-me #message").val()){
			error = true;
			$("#contact-me #message").addClass("error");
		}
		
		if(!error){			
			param = 'name=' + $("#contact-me #name").val() +
					'&email=' + $("#contact-me #email").val() +
					'&subject=' + $("#contact-me #subject").val() +
					'&message=' + $("#contact-me #message").val();

			$.ajax({
				type: "POST",
				url: '/requests/contact.php',
				cache: false,
				data: param,
				success: function(html){
					$("#contact-me").fadeOut('', function()
					{
		    			$("#thank-you").fadeIn();
		    		});
				},
				error: function(){
					alert('Please retry');
				}
			});
		}
		
		return false;
	});

});

function switch_tabs(obj)
{
	$('.tab-content').hide();
	$('.tabs li').removeClass("selected");
	var id = obj.attr("rel");
 
	$('#'+id).show();
	obj.parent('li').addClass("selected");
}

function center_big_image()
{
	//center big image
	var image_left_pos = 395 - parseInt($('.big-pic img').width()/2);
	var image_top_pos = 220 - parseInt($('.big-pic img').height()/2);
	
	$('.big-pic img').css('left', image_left_pos);
	$('.big-pic img').css('top', image_top_pos);
}

var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
  
function validateEmail(email) {
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
}
