Event.observe(window, 'load', function() {
	if ($('errors')) {
		$('errors').fade({duration: 4});
	}

	if ($('alerts')) {
		$('alerts').fade({duration: 4});
	}
	
	
	/* ---- for header menu in IE6 ---- */
	$('city').observe('mouseover', function(){
		$('city_sub').style.display = 'block';
	});
	
	$('city_sub').observe('mouseout', function(){
		$('city_sub').style.display = 'none';
	});
});

/*
 *  Club Rating AJAX
 */

function rateClub(rating, club_id) {
	var url = '/ajax/rate.php';
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: 'rating='+rating+'&type=club&type_id='+club_id, onComplete: updateRating});
}

function updateRating (originalRequest) {
	var newData = originalRequest.responseText;
	if (newData === 'Already Voted') {
		alert('We have already recieved a vote from you for this club.  Thanks for voting!');
	} else {
		$$('#rating_bg .rating').each(function(rating){
			rating.hide();
			rating.innerHTML = newData;
			rating.appear();			
		});
	}
}

/*
 *  Comments
 */

// RATE

function rateComment(rating, comment_id, user_id) {
	var url = '/ajax/rate.php';
	var myAjax = new Ajax.Request(url, {
		method: 'get', 
		parameters: 'comment_rating='+rating+'&comment_id='+comment_id+'&user_id='+user_id, 
		onComplete: updateCommentRating
	});
}

function updateCommentRating (originalRequest) {
	var newData = originalRequest.responseText;
	if (newData === 'Already Voted') {
		alert('We have already recieved a vote from you for this club.  Thanks for voting!');
	} else {
		var data = newData.evalJSON();
		var AffectedDivId = data.div;

		$(AffectedDivId).display = "none";

		$(AffectedDivId).innerHTML = '\
			<b>'+ data.rating +'</b> votes &nbsp&nbsp;\
			<img src="/images/icons/thumb_down_grey.png" alt="+" />\
			<img src="/images/icons/thumb_up_grey.png" alt="+" />\
		';

		$(AffectedDivId).appear();
	}
}

// REPLY

// create reply box

function openCommentReplyBox(replyCommentID, replyUserID, returnURI){
	$('reply_'+replyCommentID).innerHTML = '\
		<form method="post" action="/_post_comment_reply.php">\
			<input type="hidden" name="member_id" value="'+ replyUserID +'" />\
			<input type="hidden" name="comment_id" value="'+ replyCommentID +'" />\
			<input type="hidden" name="return_uri" value="'+ returnURI +'" />\
			<textarea name="comment" rows="5"></textarea><br />\
			<input type="submit" name="add_comment_reply" value="Save Reply" class="button" />\
		</form>';
}

/*
 * 	Guest List
 */

function addGuestToList() {
	
	// validate email
	var email 	= $('gl_email');
	var filter 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		email.focus();
		return false;
	}
	
	// make ajax call
	var url = '/ajax/guest_list.php';
	var pars = $('guest_list_form').serialize();
	
	var myAjax = new Ajax.Request(url, {
		method: 'get', 
		parameters: pars, 
		onComplete: updateList
	});	
}

function updateList(originalRequest){
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	
	var glButton 	= $('gl_add_button');
	var glUl 		= $('gl_responce_msg');
	var nameInput	= $('gl_name');
	var emailInput 	= $('gl_email');
	var glTitle	= $('gl_title');
	
	glButton.value = 'Add Another Person';
	nameInput.value = '';
	emailInput.value = '';
	
	if(glTitle.style.display === 'none'){
		glTitle.appear();
	}
	
	glLi = document.createElement('li');
	glLi.innerHTML = '<b>'+ data.name +'</b>'+ data.date;
	glUl.appendChild(glLi);
	glUl.show();
	glLi.hide();
	glLi.appear({to: 0.8});
	
	nameInput.focus();
	 
}


/*
 * 	Email Photo
 */

function emailPhoto() {
	
	// validate email
	var email 	= $('i_email');
	var filter 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		email.focus();
		return false;
	}
	
	// get form info and place in var
	var pars = $('email_photo_form').serialize();
	
	// give ajax progress indicator
	$('email_photo').innerHTML = '<img src="/images/icons/loading.gif" alt="" />';
	
	// make ajax call
	var url = '/ajax/email_photo.php';
	
	var myAjax = new Ajax.Request(url, {
		method: 'get', 
		parameters: pars, 
		onComplete: updatePhotoForm
	});	
}

function updatePhotoForm(originalRequest){
	$('email_photo').innerHTML = '<img src="/images/icons/email_sent.png" alt="" />';
}

/*
 *  Misc
 */

function signUp(obj, must){
	var signUpDiv = document.getElementById('sign_up_cont');
	var mustLogin = document.getElementById('must_login');
	if (obj) {
		signUpHTML = '\
			<div id="sign_up">\
				<a href="#" onclick="signUp(); return false">close x </a>&nbsp;\
				<form method="POST" action="" id="sign_up_form">\
					<h2>Sign Up</h2>\
					<b>Username</b> <input type="text" name="username" /><br />\
					<b>Email</b> 	<input type="text" name="email" /><br />\
					<b>Password</b>	<input type="text" name="password" /><br />\
					<input type="image" src="/images/icons/btn_sign_up_join.gif" name="sign_up" value="sign_up" style="margin: 7px 0 0 170px" /> \
				</form>\
				<form method="POST" action="" id="login_form">\
					<h2>Login</h2>\
					<b>Email</b> 	<input type="text" 		name="email"	style="width: 115px"  /><br />\
					<b>Password</b> <input type="password"	name="password" style="width: 115px"  /><br />\
					<input type="checkbox" 	name="remember" value="ON" /> Remember Me<br />\
					<input type="image" src="/images/icons/btn_sign_up_login.gif" name="login" value="login" style="margin: 7px 0 0 145px" /> \
				</form>\
			</div>';

		signUpDiv.innerHTML = signUpHTML;
		signUpDiv.style.display = 'block';
		if (must) {
			mustLogin.style.display = 'block';
		}
	} else {
		signUpDiv.style.display = 'none';	
		if (mustLogin.style.display == 'block') {
			mustLogin.style.display = 'none';
		}			
	}
}

function type_pass(oldPassId) {
	oldPass = document.getElementById(oldPassId);
	var c_frm = oldPass.parentNode;
	pass = document.createElement("<input name='password' />"); 
	pass.setAttribute('type', 'password');
	pass.setAttribute('id', 'new_pass');
	c_frm.replaceChild(pass, oldPass);	
	newPass = document.getElementById('new_pass');
	newPass.style.width='115px';
	newPass.setAttribute('id', 'password');
	newPass.focus();
}

function showHide(id) {
	var elem = document.getElementById(id);
	if (elem.style.display == 'none') {
		elem.style.display = 'block';
	} else {
		elem.style.display = 'none';
	}
}

function toggleWriteup(){
	var pt2 = $('read_all');
	var btn = $('readAllBtn');

	if (pt2.style.display == 'none'){
		pt2.appear();
		btn.innerHTML = 'Hide Text';
	} else {
		pt2.fade();
		btn.innerHTML = 'Read All';
	}
}

function toggleMult(id){
	var pt2 = $('read_all'+id);
	var btn = $('readAllBtn'+id);

	if (pt2.style.display == 'none'){
		pt2.blindDown();
		btn.innerHTML = 'Less';
	} else {
		pt2.blindUp();
		btn.innerHTML = 'More';
	}
}

