$(function() {
	
	
	/*** fancybox ***/
	$("a.zoom").fancybox({
		'padding':0,
		'zoomSpeedIn':500,
		'zoomSpeedOut':500
	});
	$("a.iframe").fancybox({
		'padding':0,
		'zoomSpeedIn':500,
		'zoomSpeedOut':500,
		'hideOnContentClick': false,
		'frameWidth' :960,
		'frameHeight' :400,
		'callbackOnClose': function() {
			$("#fancy_content").empty();
		}
	});
	
	/*** image hover effects ***/
	$("div.imageoverlay a").css("opacity","0");
	$("div.imageoverlay a").hover(function() {
		$(this).animate({opacity:0.55});
	},function() {
		$(this).animate({opacity:0});
	});
	
	
	var currentArtist = 0;
	
	$("#artistlist a").each(function(i) {
		$(this).data("artistId",i);
	});
	
	function showArtist(artistId) {
		currentArtist = artistId;
		var caption = $("#artisttitle");
		$(caption).hide();
		
		var overlay = $("#artistimagecontain div.imageoverlay");
		
		$(overlay)
			.hide()
			.children(".artist-image")
				.remove()
				.end()
			.children("a")
				.attr("href",artists[artistId].image.src);
		
		$('<img />').load(function() {
			$(overlay).prepend(this).fadeIn();
			$(caption)
				.html(artists[artistId].image.caption)
				.fadeIn();
		})
		.attr("src", artists[artistId].image.thumb)
		.attr("width",artists[artistId].image.width)
		.attr("height",artists[artistId].image.height)
		.addClass('artist-image');
		
		$("#artistbio").html(artists[artistId].bio);
	}
	
	$("#artistlist a").click(function() {
		var artistId = $(this).data("artistId");
		showArtist(artistId);
		$("#overlay").fadeIn();
		return false;
	});
	
	$("#artist-prev").click(function() {
		var artistId = currentArtist-1<0?artists.length-1:currentArtist-1;
		showArtist(artistId);
		return false;
	});
	$("#artist-next").click(function() {
		var artistId = currentArtist+1>artists.length-1?0:currentArtist+1;
		showArtist(artistId);
		return false;
	});
	
	$("#closeartist a").click(function() {
		$("#overlay").fadeOut();
		return false;
	});
	
	
	
	/*** artist overlay height ***/
	$("#overlay").height($("#overlay").parent().height());
	
	/*** country stylish select ***/
	$("#Country").sSelect();
	
	/*** form submit ***/
	$("#subForm a.submit").click(function() {	
		
		var subform = $('#subForm');
		
		// First, disable the form from submitting
		subform.submit(function() { return false; });
		
		// Grab form action
		formAction = subform.attr("action");
		
		// Hacking together id for email field
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		emailId = "ihwud";
		emailId = emailId.replace("/", "");
		emailId = emailId + "-" + emailId;
		
		// Validate email address with regex
		if (!checkEmail(emailId)) 
		{
			//alert("Please enter a valid email address");
			$("#error").fadeIn("slow");
			return;
		}
		
		// Serialize form values to be submitted with POST
		var str = subform.serialize();
		
		// Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "/wp-content/themes/alriwaqartspace/includes/proxy.php",
			type: "POST",
			data: final,
			success: function(html){
				$("#theForm").hide(); // If successfully submitted hides the form
				$("#confirmation").fadeIn("slow");  // Shows "Thanks for subscribing" div
			}
		});
		
		return false;
	});

	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
});
