jQuery(function($)
{
	var cache = [];

	$.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);
		}
	}

	if ($('.validateForm').length)
	{
		$('.validateForm').submit(function(){
			var rAction = true;
			var form = $(this);
			var isEmail = false;
			var isEmpty = false;

			$('.formIsRequired', form).each(function(){
				if ($(this).val() == '')
				{
					rAction = false;
					if (!$(this).hasClass('formError'))
						$(this).addClass('formError');

					isEmpty = true;
				}
				else if ($(this).hasClass('formError'))
					$(this).removeClass('formError');
			});

			var formIsEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			$('.formIsEmail', form).each(function(){
				if ($(this).val() == '')
					return;

				if (formIsEmail.test($(this).val()) == false)
				{
					rAction = false;
					if (!$(this).hasClass('formError'))
						$(this).addClass('formError');

					isEmail = true;
				}
				else if ($(this).hasClass('formError'))
					$(this).removeClass('formError');
			});

			// say thank you!
			if (rAction)
			{
				if ($(this).attr('name') == 'suscribe')
					window.alert('Thank you! We have received your subscription request!');
				else if ($(this).attr('name') == 'request-info')
					window.alert('Thank you! We have received your message!');
				else if ($(this).attr('name') == 'contact')
					window.alert('Thank you! We have received your message!');
				else
					window.alert('Thank you! We have received your form!');
			}
			else
			{
				if (isEmpty)
					window.alert('Please, you must fill all the fields.');
				else if (isEmail)
					window.alert('Please, provide a valid e-mail.');
			}

			return rAction;
		});
	}
});
jQuery(document).ready(function($)
{
	$.preLoadImages(
		imagesUrl + '/artistWorks_closeHover.png',
		imagesUrl + '/artistWorks_rowLeftHover.png',
		imagesUrl + '/artistWorks_rowRightHover.png',
		imagesUrl + '/bookmarks-on.png',
		imagesUrl + '/homeUpdates_closeHover.png'
	);

	if ($('#homeSlider div').length)
	{
		$('#homeSlider li').each(function(){
			var updates = $("div.updates", this);
			var updatesContainer = $("div.updatesContainer", this);

			updatesContainer.height(updates.height());
			updatesContainer.width(updates.width());
			updatesContainer.css({opacity: .65});

			$('span.close', updates).click(function(){
				updates.animate({opacity: 0}, 500, function(){
					$(this).hide();
				});
				updatesContainer.animate({opacity: 0}, 500, function(){
					$(this).hide();
				});
			});
		});

		$('#homeSlider > div').pmSlider({
			// auto: true,
			numeric: true,
			speed: 1000,
			pause: 5000,
			homeSlider: true
		});
	}

	if ($('#artists-workSlider').length)
	{
		var maxHeight = 0;
		$('#artists-workSlider ul li').each(function(){
			var w = $('img.image', this).width();
			$('div.container', this).width(w);

			var h = $(this).height();
			if (h > maxHeight) maxHeight = h;
		});

		$('#artists-workSlider ul li').height(maxHeight);
		$('#artists-workSlider ul').height(maxHeight);

		$('#artists-workSlider > div').pmSlider({
			// auto: true,
			speed: 1000,
			pause: 5000,
			workSlider: true
		});
	}

	if ($('#contactMap').length)
	{
		map_lat = jQuery("#contact_map_latitud").text();
		map_lon = jQuery("#contact_map_longitud").text();
		map_zoom = parseInt(jQuery("#contact_map_zoom").text(), 10);
	
		var myLatlng = new google.maps.LatLng(map_lat, map_lon);
		var myOptions = {
			zoom: map_zoom,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}

		var map = new google.maps.Map(document.getElementById("contactMap"), myOptions);
		var marker = new google.maps.Marker({
			animation: google.maps.Animation.DROP,
			position: myLatlng, 
			map: map
		});
	}

	if (false && $('#news-rightList').length)
	{
		$('.news-listContent').each(function(){
			$(this).attr('rel', $(this).height());
			$(this).hide();
		});
		$('.news-listTitle:first').addClass('news-listActive').next().show();

		$('.news-listTitle').click(function() {
			if (!$(this).hasClass('news-listActive') && $(this).next())
			{
				$('.news-listTitle').each(function(){
					if (!$(this).hasClass('news-listActive')) return;

					$(this).removeClass('news-listActive');
					$(this).next().animate({height: 0}, 1000, function(){ $(this).hide(); });
				});

				var obj = $(this).next();
				$(this).addClass('news-listActive');

				obj.height(0);
				obj.show();
				obj.animate({height: parseInt(obj.attr('rel'), 10)}, 1000);
			}

			return false;
		});
	}
});
