$(document).ready(function() {
	function validateInfos() {
		errFlag = false;
		errMsg = email_text_error + "\n" +  "__________________________________\n\n";

		if(isEmpty(yourname) || !is_valid_name(yourname)) {
			errMsg = errMsg + email_text_name_invalid + "\n";
			errFlag = true;
		}
		
		if(isEmpty(youremail) || !is_valid_email(youremail)) {
			errMsg = errMsg + email_text_email_invalid + "\n";
			errFlag = true;
		}
		
		if(!isEmpty(yourremarks)) {
			if(!is_valid_remarks(yourremarks)) {
				errMsg = errMsg + email_text_remarks_invalid + "\n";
				errFlag = true;
			}
		}
		
		if(isEmpty(friendname) || !is_valid_name(friendname)) {
			errMsg = errMsg + email_text_friend_name_invalid + "\n";
			errFlag = true;
		}
		
		if(isEmpty(friendemail) || !is_valid_email(friendemail)) {
			errMsg = errMsg + email_text_friend_email_invalid + "\n";
			errFlag = true;
		}
		
		if(errFlag) {
			alert(errMsg);
			return false;
		} else {
			return true;
		}
	}
	
	$('#accordion').accordion({
		header : "h2",
		autoHeight: false
	});
	
	$('.tagcloud h2 a').toggle(function() {
		$('.tagcloud .list').slideUp();
		$(this).css('background-position', '8px 0');
		return false;
	}, function() {
		$('.tagcloud .list').slideDown();
		$(this).css('background-position', '8px -29px');
		return false;
	});
	
	$('.post').each(function() {
		// Share this tooltip
		offset = $(this).find('.sharethis a').offset();
		source = '<div class="sharett" id="tt-' + $(this).attr('id') + '"><div class="top"></div><div class="content"><ul>';
		title = $(this).find('h1 a').text();
		url = $(this).find('h1 a').attr('href');
		
		for(i = 0; i < bookmark.length; i++) {
			li = '<li><a href="' + bookmark[i]['url'] + '" style="background:url(' + bookmark[i]['icon'] + ') no-repeat top left">' + bookmark[i]['name'] + '</a></li>';
			li = li.replace('%TITLE%', title).replace('%URL', url);
			source = source + li;
		}
		
		source = source + '</ul></div><div class="bottom"></div></div>'
		
		$('body').append(source);
		$('.sharett li:last').css({
			'border' : 'none',
			'margin-bottom' : '0'
		});
		
		$('.sharett:last').css({
			'top' : Math.round(offset.top) + 18 +  'px',
			'left' : Math.round(offset.left) + 'px'
		});
		
		// Send to friend tooltip
		offset = $(this).find('.sendfriend a').offset();
		id = $(this).attr('id').substr(5, $(this).attr('id').length);
		form = '<form method="post" action="' + url + 'emailpopup"><div style="display:none"><input id="p" name="p" class="p" type="hidden" value="' + id + '" /><input id="popup" name="popup" class="popup" type="hidden" value="1" /></div><div class="left"><p><label for="yourname">From Name (Your Name):</label></p><p><input type="text" id="yourname" class="yourname" name="yourname" /></p><p><label for="friendname">To Name (Friends Name):</label></p><p><input type="text" id="friendname" name="friendname" class="friendname" /></p></div><div class="right"><p><label for="youremail">From Email (Your email address):</label></p><p><input type="text" id="youremail" name="youremail" class="youremail" /></p><p><label for="friendemail">To Email (Friends email address):</label></p><p><input type="text" id="friendemail" name="friendemail" class="friendemail" /></p></div><br class="clear" /><div class="message"><p><label for="yourremarks">Comment:</label></p><p><textarea id="yourremarks" name="yourremarks" class="yourremarks" row="8" cols="20"></textarea></p></div><div class="send"><input type="button" class="send_button" id="wp-email-submit" value="Send Post" /></div><br class="clear" /></form>';
		source = '<div class="sendtt" id="stt-' + $(this).attr('id') + '"><div class="top"></div><div class="content">' + form + '</div><div class="bottom"></div>';

		$('body').append(source);

		$('.sendtt:last').css({
			'top' : Math.round(offset.top) + 18 +  'px',
			'left' : Math.round(offset.left) + 'px'
		});
		
		$('.sendtt:last .send_button').click(function() {
			pa = $(this).parent().parent();
			yourname = pa.find('.yourname').val();
			youremail = pa.find('.youremail').val();
			friendname = pa.find('.friendname').val();
			friendemail = pa.find('.friendemail').val();
			yourremarks = pa.find('.yourremarks').val();
			p = pa.find('.p').val();
			popup = pa.find('.popup').val();
			
			$(pa.parent().css('height', pa.parent().height() + 'px'));
			
			if(validateInfos(yourname, youremail, yourremarks, friendname, friendemail)) {
				email.setVar('yourname', yourname);
				email.setVar('youremail', youremail);
				email.setVar('yourremarks', yourremarks);
				email.setVar('friendname', friendname);
				email.setVar('friendemail', friendemail);
				email.setVar('p', p);
				email.setVar('wp-email', '1');
				email.setVar('popup', popup);
				email.method = 'POST';
				email.element = 'wp-email';
				email.runAJAX();

				pa.fadeOut('slow', function() {
					pa.parent().animate({
						height : 10
					});
					pa.text('Your E-Mail has been sent.');
					pa.fadeIn('fast');
				});
			}
		});
	});
	
	$('.sharethis a').toggle(function() {
		$('#tt-' + $(this).parents('.post').attr('id')).fadeIn();
		return false;
	}, function() {
		$('#tt-' + $(this).parents('.post').attr('id')).fadeOut();
		return false;
	});
	
	$('.sendfriend a').toggle(function() {
		$('#stt-' + $(this).parents('.post').attr('id')).fadeIn();
		return false;
	}, function() {
		$('#stt-' + $(this).parents('.post').attr('id')).fadeOut();
		return false;
	});
	
	if($('#respond').html()) {
		function errorPulsate(element, newText) {
			$(element).animate({
				opacity : 0.1
			}, function() {
				$(this).text(newText).css('color', 'red').animate({ opacity : 1 }, function() {
					$(this).animate({ opacity : 0.1 }, function() {
						$(this).animate({ opacity : 1 });
					});
				});
			});
		}
	
		$('#submit').click(function() {
			errFlag = false;
			
			if($('#author').val() == '') {
				errorPulsate('#lblauthor', 'Name is required');
				errFlag = true;
			}

			if($('#email').val() == '') {
				errorPulsate('#lblemail', 'Email is required');				
				errFlag = true;
			} else if(!is_valid_email($('#email').val())) {
				errorPulsate('#lblemail', 'Email must be valid');
				errFlag = true;
			}
			
			if($('#comment').val() == '') {
				errorPulsate('#lblcomment', 'Comment is required');
				errFlag = true;
			}
			
			if(errFlag) {
				return false;
			}
		});
	}
});