$(function() {
	$('#browseTitle li.browsetitle').click(function() {
		$(".browseCat").css("display", "none");
		$("#browseCat-" + $(this).text()).css("display", "block");
		$('#browseTitle li.browsetitle').removeClass("selected");
		$(this).addClass("selected");			
	});
	
	$("form#emailPostForm").submit(function() {
		return requestAjax();
	});
	
	$("input#submitBtn").click(function() {
		return requestAjax();
	});
});


function open_window(url, title, width, height) {
	var newwin;
	newwin = window.open(url, title, "width=" + width + ",height=" + height + ",status=0,toolbar=0,resizable=0,scrollbars=1");
}

function print_post(el) {
	open_window(el.href, "print", 500, 600);
	return false;
}

function email_post(el, id) {

	open_modal('emailPost')
	$('input#emailID').val(id);
	return false;
}

function getOverlayContentPosition(overlay) {

	var winHeight, winWidth;
	var ypos, xpos;
	
	if (typeof( window.innerHeight ) == 'number') { //except ie
		ypos = window.pageYOffset;
		xpos = window.pageXOffset;
		winHeight = window.innerHeight;
		winWidth = window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { //ie
		ypos = document.documentElement.scrollTop;
		xpos = document.documentElement.scrollLeft;
		winHeight = document.documentElement.clientHeight;
		winWidth = document.documentElement.clientWidth + 18;
	}
	else if (document.body) {
		ypos = document.body.scrollTop;
		xpos = document.body.scrollLeft;
		winHeight = documnet.body.clientHeight;
		winWidth = documnet.body.clientWidth;
	}

	ypos = ypos + (winHeight - $('#' + overlay).height()) / 2;
	xpos = xpos + Math.ceil((winWidth - $('#' + overlay).height()) / 2) - 39;
	
	return ypos;
	//$('#emailPostCont').css('top',(ypos) + 'px');
	//$('#emailPostCont').css('left', (xpos) + 'px');
}


function open_modal(overlay) {
	$('#modalScreen').addClass('modal-visible');
	$('#modalScreen').css("height", parseInt($('#page').height()));
	$('#emailPostCont').addClass('modal-visible');
	$('#emailPostCont').css('top', (getOverlayContentPosition(overlay)) + 'px');
	$('.modalContent').addClass('modalHidden');
	$('#' + overlay).removeClass('modalHidden');
}
var msgTimer;

function close_modal() {
	clearTimeout(msgTimer);
	$('.modal').removeClass('modal-visible');
}

function requestAjax() {

	if ( !checkCommentForm() )  {
		open_modal('emailPostLoading');
		var timestamp = Math.random();
		 $.post($("form#emailPostForm").attr('action'), {
			   id: $("#emailID").val(),
			   yourEmail: $("#yourEmail").val(),
			   friendEmail : $("#friendEmail").val(),
			   emailMessage : $("#emailMessage").val(),
			   submit : 'submit',
			   time: timestamp
			 }, function(txt) {
			 	txt = txt.replace(new RegExp( "\\n", "g" ), '');
			 	if (txt != 'Success') {
			 		txt = 'Error';
			 	}
			 	displayMessage(txt);
		 });
	}
	return false; 	
}

function displayMessage(txt) {
	clearTimeout(msgTimer);
	open_modal('emailPost' + txt);
	$("#emailMessage").val('');
	$('#yourEmail').val('');
	$('#friendEmail').val('');
	msgTimer = setTimeout('close_modal()', 3000);
}


function checkCommentForm() {
	var seeErr = false;
	var regExEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if ($("#emailMessage").val().length < 150 ) {
		$("#emailMessage").removeClass('error');
	}
	else {
		seeErr = true;
		$("#emailMessage").addClass('error');	
	}
	
	if (($('#yourEmail').val() != '') && regExEmail.test($('#yourEmail').val())) {
		$("#yourEmail").removeClass('error');
	}
	else {
		seeErr = true;
		$("#yourEmail").addClass('error');
	}
	
	if (($('#friendEmail').val() != '') && regExEmail.test($('#friendEmail').val())) {
		$("#friendEmail").removeClass('error');
	}
	else {
		seeErr = true;
		$("#friendEmail").addClass('error');
	}
	
	if (seeErr) {
		$('#emailError').css('display', 'block');
	}
	else {
		$('#emailError').css('display', 'none');	
	}

	return seeErr;
}