var test;
var gallery_image = 0;
$('head').bind("ajaxSend",function(){
	if($('#wait').length == 0) $('head').append('<style type="text/css" id="wait">*{cursor:wait;}</style>');
});
$('head').bind("ajaxStop",function(){
	$('#wait').remove();
});
$.ajax({'async':false});
$(document).ready(function () {
	var dialog = $('#dialog');
	$('a[rel="gallery"]').parent().parent().each(function(i,e){
		$('a[rel="gallery"]',e).lightBox();
	});
	$('.artist .paintings').each(function(i,e){
		if($.browser['version'] <= 6.0 && $.browser['msie'] == true) return false;
		var width = $(e).find('img').length*150;
		$(e).css('overflow-y','hidden');
		$(e).css('overflow-x','scroll');
		$('ul',e).css('width',width+'px');
	});
	$('.tr_clickable tr').live('click',function(e){ //Makes table rows clickable
		var link = $(this).find('a:eq(0)').attr('href');
		if(!$(e.target).is('a') && !$(e.target).is('th') && link) window.location = link;
	});
	form_toggle_values('#submit_comment');
	form_toggle_values('#contact_form');
	$('#contact_form').submit(function(e){
		e.preventDefault();
		if(check_form('#contact_form') == true){
			var data = $('#contact_form').serialize();
			$.post('index.php?p=contact',data,function(data){
				$('#contact_form').prepend(data);
				if($(test)[0].className == 'success') reset_form('#contact_form');
				$('.error,.success','#contact_form').fadeOut(10000);
			});
		}
	});
	$('#submit_comment').submit(function(e){
		e.preventDefault();
		if(check_form('#submit_comment') == true){
			var data = $('#submit_comment').serialize();
			$.post('index.php?p='+window.location.search.match(/p=([a-z]+)/)[1]+'&id='+document.location.search.match(/id=([0-9]+)/)[1],data,function(data){
				$('#comments').prepend(data);
				if($(test)[0].className == 'success') reset_form('#submit_comment');
			});
		}
	});
	$('#choose_featured_painting').click(function(e){
		e.preventDefault();
		$(dialog).load('ajax.php?f=featured_painting',function(data){
			$(dialog).dialog({
				modal:true,
				width:600,
				position:['middle',50]
			}).dialog('open');
			$('#choose_artist',dialog).click(function(e){
				e.preventDefault();
				var id = $(dialog).find('select').val();
				$(dialog).load('ajax.php?f=featured_painting&id='+id,function(data){
					$(dialog).find('img').click(function(e){
						e.preventDefault();
						$('#painting').val($(this).attr('id').split('_')[1]);
						$(dialog).dialog('close');
					});
				});
			});
		})
	
	});
});
function form_toggle_values(form){
	$('input',form).bind('focus blur',function(e){
		if($(this).attr('type') != 'submit'){
			if(e.type == 'focus'){
				if($(this).val() == this.defaultValue) $(this).val('');
			}
			else if(e.type == 'blur'){
				if($(this).val() == '') $(this).val(this.defaultValue);
			}
		}
	});
	$('textarea',form).bind('focus blur',function(e){
		if(e.type == 'focus'){
			if($(this).val() == $(this).text()) $(this).val('');
		}
		else if(e.type == 'blur'){
			if($(this).val() == '') $(this).val($(this).text());
		}
	});
}
function check_form(form){
	var errors = 0;
	$(form).find('input:text.required').each(function(i,e){
		if($(e).val() == e.defaultValue){ 
			$(e).addClass('required_empty');
			errors = errors+1;
		}
		else if($(e).val() == ''){
			$(e).addClass('required_empty');
			errors = errors+1;
		}
		else{
			$(e).removeClass('required_empty');
		}
	});
	$(form).find('textarea.required').each(function(i,e){
		if($(e).val() == $(e).text()){
			$(e).addClass('required_empty');
			errors = errors+1;
		}
		else if($(e).val() == ''){
			$(e).addClass('required_empty');
			errors = errors+1;
		}
		else{
			$(e).removeClass('required_empty');
		}
	});
	if(errors == 0) return true;
	else return false;
}
function reset_form(form){
	$(form).find('input').each(function(i,e){
		$(e).val(e.defaultValue);
	});
	$(form).find('textarea').each(function(i,e){
		$(e).val($(e).text());
	});
}