$(document).ready(function() {
	initButton();
});




function initButton(){
		$('#jPostButns div').hover(function(){addHover(this);}, function(){removeHover(this);});
}

var addHover = function(elm, isRollover) {
	$(elm).children('ul').show();
	$(elm).addClass('selected');
}

var removeHover = function(elm, isRollover) {
	$(elm).children('ul').hide();
	$(elm).removeClass('selected');
}



function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function checkEmail(emial){
	var str=emial;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		return true;
	else
		return false;
}

function checkRequired(){
	
	document.getElementById('commess').style.display = 'none';
	
	var status = document.getElementById('loggedin').value;
	var comment = trim(document.getElementById('comment').value);
	
	if( status=='no' ){
		var name = trim(document.getElementById('author1').value);
		var email = trim(document.getElementById('email').value);
		
		if( name=='Name' || email=='Email' ){
			//alert('Please complete the required fields (Name, Email).');
			document.getElementById('commess').innerHTML = 'Please complete the required fields (Name, Email).';
			document.getElementById('commess').style.display = 'block';
			return false;
		}
		else if( comment=='Comment' ){
			//alert('Please type a Comment.');
			document.getElementById('commess').innerHTML = 'Please type a Comment.';
			document.getElementById('commess').style.display = 'block';
			return false;
		}
		else if( !checkEmail(email) ){
			//alert('Please enter a valid email address.');
			document.getElementById('commess').innerHTML = 'Please enter a valid email address.';
			document.getElementById('commess').style.display = 'block';
			return false;
		}
	}
	else{
		if( comment=='Comment' ){
			//alert('Please type a Comment.');
			document.getElementById('commess').innerHTML = 'Please type a Comment.';
			document.getElementById('commess').style.display = 'block';
			return false;
		}
	}
	
	
	return true;
	
}



function submitComment(){
	if(checkRequired())	
		document.commentform.submit();
}


function clearSpace(){
	
	/*var archive=$('#archive li');
	var archiveWidth= archive.length*101-30;
	$('#archive').css('width',archiveWidth+'px');*/
	
	var posts=$('.jPost');
	if(posts.length >= 2){
		var leftWidth=posts[0].offsetHeight+20;
		var rightWidth=posts[1].offsetHeight+20;
		$(posts[1]).addClass('rgtPost');
		for(i=2;i<posts.length;i++){
			if(rightWidth > leftWidth){
				var gap=rightWidth-leftWidth-20;
				$(posts[i]).css("margin-top",-gap+"px").css("clear","left");
				leftWidth=leftWidth+posts[i].offsetHeight+20;
			}
			else{
				rightWidth=rightWidth+posts[i].offsetHeight+20;
				$(posts[i]).addClass('rgtPost');
			}	
		}
	}
}


function clearSpaceComment(){
	var ie6=false;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number
		if (ieversion>=6)
 			 ie6=true;
	}
	
	if(!ie6){
		var posts=$('.jPost');	
		var leftWidth=posts[0].offsetHeight+20;
		var rightWidth=0;
		
		var postImgs=$('.jPostImg');
		
		if(postImgs[0]){
			rightWidth=postImgs[0].offsetHeight+20;
	
			if(rightWidth > leftWidth){
				var gap=rightWidth-leftWidth-20;
				$('#comments').css("margin-top",-gap+"px");		
			}
		}
		
	}
	
}

/*function clearSpace() {
    var posts=$('.journalPosts');
	
	//element = $(element);
	
	for(i=0;i<posts.length;i++){
		element=posts[i];
		//alert(element.innerHTML);
		var node = element.firstChild;
		while (node) {
		  var nextNode = node.nextSibling;
		  if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
			element.removeChild(node);
		  node = nextNode;
		}
		//alert(element.innerHTML);
	}

  }*/
