function getRelativePath() {	
	var path_name = document.location.pathname;
	//rootdir in index.php
	path_name = path_name.replace(rootdir,'');	
	var path_items = path_name.split("/");
	var rel_path = '';
	for (var i = 0; i < path_items.length-1; i++) {
		rel_path += '../';
	}
	return rel_path;
}

function setupHovers(hovers) {
	for (var id in hovers)
	{
		$("#"+id).hover(function(e){
			var img = hovers[$(this).attr('id')][1];
			$(this).attr('src', getRelativePath()+'images/'+img);
		},
		function() {
			var img = hovers[$(this).attr('id')][0];
			$(this).attr('src', getRelativePath()+'images/'+img);
		});
	}
}

function preloadImages(hovers, images) {
	//preload hovers
	for (var id in hovers)
	{
		loadedImage = new Image(); 
		loadedImage.src = hovers[id][1];
	}
	//preload other images
	for (var i = 0; i < images.length; i++) {
		loadedImage = new Image(); 
		loadedImage.src = getRelativePath()+'images/'+images[i];
	}
}

//form checking

function validateString(string, legal) {
	if (string == '') return false;
	string = string.toLowerCase();
	for (var i = 0; i < string.length; i++) {
		if (legal.indexOf(string.charAt(i)) == -1) {
			return false;
		}
	}
	return true;
}

function validateEmail(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(address);
}

//tell a friend form
function initiateTellAFriendForm(type)
{
	$("#tell-a-friend-btn").bind('click', function() {
		//show right state
		resetTellAFriendForm();
		//trigger popup
		$.fn.colorbox({width:$("#tell-a-friend-popup").width()+"px", height:$("#tell-a-friend-popup").height()+"px", inline:true, href:"#tell-a-friend-popup", transition: 'none', opacity: 0.8});		
	});
	$('.popup-header img').bind('click', function() {
		$.fn.colorbox.close();
	});
	//show/hide 'required' in fields
	$(".taf-content input, .taf-content textarea").each(function() {
		$(this).bind('focus', function() {
			var field_id = $(this).attr('id');
			var wrapper_id = field_id.split("field").join("wrapper");
			var newbg;
			if (field_id == 'taf-field-friends') { newbg = 'taf-friends-bg-active.png'; } 
			else if (field_id == 'taf-field-message') { newbg = 'popup-message-bg-active.png'; }
			else { newbg = 'popup-field-bg-active.png' }
			$("#"+wrapper_id).css('background-image', 'url('+getRelativePath()+'images/'+newbg+')');
			var feedback_id = field_id.split("field").join("feedback");
			$("#"+feedback_id).hide();
			
		});
		$(this).bind('blur', function() {
			if ($(this).val() == '') {
				var field_id = $(this).attr('id');
				var wrapper_id = field_id.split("field").join("wrapper");
				var newbg;
				if (field_id == 'taf-field-friends') { newbg = 'taf-friends-bg.png'; } 
				else if (field_id == 'taf-field-message') { newbg = 'popup-message-bg.png'; }
				else { newbg = 'popup-field-bg.png' }
				$("#"+wrapper_id).css('background-image', 'url('+getRelativePath()+'images/'+newbg+')');
			}
		});
	});
	//popup footer
	$('#send-copy').bind('click', function() {
		sendCopy = !sendCopy;
		if (sendCopy) {
			$("img",this).attr('src', getRelativePath()+'images/checkbox-checked.gif');
		} else {
			$("img",this).attr('src', getRelativePath()+'images/checkbox-unchecked.gif');
		}
	});
	$('#cancel-taf-btn').bind('click', function() {
		$.fn.colorbox.close();
	});
	
	$('#send-taf-btn').bind('click', function() {
		//check 'n submit form
		submitTellAFriendForm(sendCopy, type);
	});
}

function submitTellAFriendForm(sendcopy, type)
{
	var errors = new Array();
	//friends
	var friends = $('#taf-field-friends').val();
	if (friends == '') {
		errors.push(new Array('friends', '<strong>Please fill out all required fields</strong><br />At least one address is required'));
	} else {
		var emails = friends.split(",");
		var entered_emails = 0;
		for (var i = 0; i < emails.length; i++) {
			var email = emails[i].split(" ").join("");
			if (email != "") {
				if (!validateEmail(email)) {
					errors.push(new Array('friends', '<strong>Please use valid e-mail addresses</strong><br />One or more are invalid'));
					break;
				}
			}
		}
	}
	//name
	var name = $('#taf-field-name').val();
	if (name == '') {
		errors.push(new Array('name', '<strong>Please fill out all required fields</strong><br />Your name is required'));
	}
	//email
	var email = $('#taf-field-email').val();
	if (email == '') {
		errors.push(new Array('email', '<strong>Please fill out all required fields</strong><br />Your e-mail address is required'));
	} else if (!validateEmail(email)) {
		errors.push(new Array('email', '<strong>Please use a valid e-mail address</strong><br />This e-mail address is invalid'));
	}
	//subject
	var subject = $('#taf-field-subject').val();
	if (subject == '') {
		errors.push(new Array('subject', '<strong>Please fill out all required fields</strong><br />A subject is required'));
	}
	//message
	var message = $('#taf-field-message').val();
	if (message == '') {
		errors.push(new Array('message', '<strong>Please fill out all required fields</strong><br />A message is required'));
	}
	
	if (errors.length == 0) {
		//submit
		$('#send-copy').hide();
		$('#cancel-taf-btn').hide();
		$('#send-taf-btn').hide();
		$('#taf-form').hide();
		$('#taf-loading').show();
		var cid = (type == 'blog') ? 0 : $('#taf-cid').val();
		$.ajax({
			type: 'POST',
			data: {
				method: 'tell-a-friend',
				friends: friends,
				name: name,
				email: email,
				subject: subject,
				message: message,
				sendcopy: sendcopy,
				pid: $('#taf-pid').val(),
				cid: cid,
				type: $('#taf-type').val()
			},
			url: getRelativePath()+"ajax.php",
			success: function(response) {
				$('#taf-loading').hide();
				if (response == 'OK') {
					$('#taf-success').show();
				} else {
					$('#taf-fail').show();
				}
			}
		});
	} else {
		//show feedback
		for (var i = 0; i < errors.length; i++)
		{
			$('#taf-feedback-'+errors[i][0]).show();
			$('#taf-feedback-'+errors[i][0]).html(errors[i][1]);
		}
	}
}

function resetTellAFriendForm()
{
	$('#taf-form').show();
	$('#taf-loading').hide();
	$('#taf-success').hide();
	$('#taf-fail').hide();
	$('.form-feedback-point-left').hide();
	$('.form-feedback-point-right').hide();
	$('#send-copy').show();
	$('#cancel-taf-btn').show();
	$('#send-taf-btn').show();	
}

function initiateBuyPhotoForm()
{
	$("#buy-photo-btn").bind('click', function() {
		//show right state
		resetBuyPhotoForm();
		//trigger popup
		$.fn.colorbox({width:$("#buy-photo-popup").width()+"px", height:$("#buy-photo-popup").height()+"px", inline:true, href:"#buy-photo-popup", transition: 'none', opacity: 0.8});		
	});
	$('.popup-header img').bind('click', function() {
		$.fn.colorbox.close();
	});
	//show/hide 'required' in fields
	$(".bp-content .required").each(function() {
		$(this).bind('focus', function() {
			var field_id = $(this).attr('id');
			var wrapper_id = field_id.split("field").join("wrapper");
			var newbg = 'popup-field-bg-active.png';
			$("#"+wrapper_id).css('background-image', 'url('+getRelativePath()+'images/'+newbg+')');
		});
		$(this).bind('blur', function() {
			if ($(this).val() == '') {
				var field_id = $(this).attr('id');
				var wrapper_id = field_id.split("field").join("wrapper");
				var newbg = 'popup-field-bg.png';
				$("#"+wrapper_id).css('background-image', 'url('+getRelativePath()+'images/'+newbg+')');
			}
		});
	});
	//popup footer
	$('#info-fine-art').bind('click', function() {
		infoFineArt = !infoFineArt;
		if (infoFineArt) {
			$("img",this).attr('src', getRelativePath()+'images/checkbox-checked.gif');
		} else {
			$("img",this).attr('src', getRelativePath()+'images/checkbox-unchecked.gif');
			if (!infoLicense) {
				//one info type needs to be selected
				infoLicense	= true;
				$("img","#info-license").attr('src', getRelativePath()+'images/checkbox-checked.gif');
			}
		}
	});
	$('#info-license').bind('click', function() {
		infoLicense = !infoLicense;
		if (infoLicense) {
			$("img",this).attr('src', getRelativePath()+'images/checkbox-checked.gif');
		} else {
			$("img",this).attr('src', getRelativePath()+'images/checkbox-unchecked.gif');
			if (!infoFineArt) {
				//one info type needs to be selected
				infoFineArt	= true;
				$("img","#info-fine-art").attr('src', getRelativePath()+'images/checkbox-checked.gif');
			}
		}
	});
	$('#cancel-bp-btn').bind('click', function() {
		$.fn.colorbox.close();
	});
	
	$('#send-bp-btn').bind('click', function() {
		//check 'n submit form
		submitBuyPhotoForm(infoFineArt, infoLicense);
	});
}

function submitBuyPhotoForm(infofineart, infolicense)
{
	var errors = new Array();
	
	var name = $('#bp-field-name').val();
	
	//email
	var email = $('#bp-field-email').val();
	if (email == '') {
		errors.push(new Array('email', '<strong>Please fill out all required fields</strong><br />Your e-mail address is required'));
	} else if (!validateEmail(email)) {
		errors.push(new Array('email', '<strong>Please use a valid e-mail address</strong><br />This e-mail address is invalid'));
	}
	
	var comments = $('#bp-field-comments').val();
	
	if (errors.length == 0) {
		//submit
		$('#cancel-bp-btn').hide();
		$('#send-bp-btn').hide();
		$('#bp-form').hide();
		$('#bp-loading').show();
		$.ajax({
			type: 'POST',
			data: {
				method: 'buy-photo',
				pid: $('#bp-pid').val(),
				name: name,
				email: email,
				comments: comments,
				infofineart: infofineart,
				infolicense: infolicense
			},
			url: getRelativePath()+"ajax.php",
			success: function(response) {
				$('#bp-loading').hide();
				if (response == 'OK') {
					$('#bp-success').show();
				} else {
					$('#bp-fail').show();
				}
			}
		});
	} else {
		//show feedback
		for (var i = 0; i < errors.length; i++)
		{
			$('#bp-feedback-'+errors[i][0]).show();
			$('#bp-feedback-'+errors[i][0]).html(errors[i][1]);
		}
	}
}

function resetBuyPhotoForm()
{
	$('#bp-form').show();
	$('#bp-loading').hide();
	$('#bp-success').hide();
	$('#bp-fail').hide();
	$('.form-feedback-point-left').hide();
	$('.form-feedback-point-right').hide();
	$('#cancel-bp-btn').show();
	$('#send-bp-btn').show();
}

function initiateContactForm()
{
	//show/hide 'required' in fields
	$(".form-content .required").each(function() {
		$(this).bind('focus', function() {
			var field_id = $(this).attr('id');
			var wrapper_id = field_id.split("field").join("wrapper");
			var newbg;
			if (field_id == 'cf-field-message') { newbg = 'contact-form-message-active.png'; } 
			else if (field_id == 'cf-field-subject') { newbg = 'contact-form-field-long-active.png'; }
			else { newbg = 'contact-form-field-short-active.png' }
			$("#"+wrapper_id).css('background-image', 'url('+getRelativePath()+'images/'+newbg+')');
			var feedback_id = field_id.split("field").join("feedback");
			$("#"+feedback_id).hide();
		});
		$(this).bind('blur', function() {
			if ($(this).val() == '') {
				var field_id = $(this).attr('id');
				var wrapper_id = field_id.split("field").join("wrapper");
				var newbg;
				if (field_id == 'cf-field-message') { newbg = 'contact-form-message.png'; } 
				else if (field_id == 'cf-field-subject') { newbg = 'contact-form-field-long.png'; }
				else { newbg = 'contact-form-field-short.png' }
				$("#"+wrapper_id).css('background-image', 'url('+getRelativePath()+'images/'+newbg+')');
			}
		});
	});

	$('#send-btn').bind('click', function() {
		//check 'n submit form
		submitContactForm();
	});
}

function submitContactForm()
{
	var errors = new Array();
	var name = $('#cf-field-name').val();
	//email
	var email = $('#cf-field-email').val();
	if (email == '') {
		errors.push(new Array('email', '<strong>Please fill out all required fields</strong><br />Your e-mail address is required'));
	} else if (!validateEmail(email)) {
		errors.push(new Array('email', '<strong>Please use a valid e-mail address</strong><br />This e-mail address is invalid'));
	}
	//subject
	var subject = $('#cf-field-subject').val();
	if (subject == '') {
		errors.push(new Array('subject', '<strong>Please fill out all required fields</strong><br />A subject is required'));
	}
	//message
	var message = $('#cf-field-message').val();
	if (message == '') {
		errors.push(new Array('message', '<strong>Please fill out all required fields</strong><br />A message is required'));
	}
	
	if (errors.length == 0) {
		//submit
		$('#send-btn').hide();
		$('#cf-form').hide();
		$('#cf-loading').show();
		$.ajax({
			type: 'POST',
			data: {
				method: 'contact',
				name: name,
				email: email,
				subject: subject,
				message: message
			},
			url: getRelativePath()+"ajax.php",
			success: function(response) {
				$('#cf-loading').hide();
				if (response == 'OK') {
					$('#cf-success').show();
				} else {
					$('#cf-fail').show();
				}
			}
		});
	} else {
		//show feedback
		for (var i = 0; i < errors.length; i++)
		{
			$('#cf-feedback-'+errors[i][0]).show();
			$('#cf-feedback-'+errors[i][0]).html(errors[i][1]);
		}
	}
}

function resetContactForm()
{
	$('#cf-form').show();
	$('#cf-loading').hide();
	$('#cf-success').hide();
	$('#cf-fail').hide();
	$('.form-feedback-point-left').hide();
	$('.form-feedback-point-right').hide();
	$('#send-btn').show();	
}
