$(document).ready(function(){
	if($("#commentForm").length>0){
		$("#commentForm").ajaxForm({
			dataType:  "json", 
			success: processComment, 
			beforeSubmit: bSComment, 
			url: "/articles/insertcomment", 
			type: "post", 
			timeout: 30000
		});
	}
	
	
	$(".jCarouselMenu a").hover(function() {
		$(this).next("em").animate({opacity: "show", top: "-75"}, "slow");
	}, function() {
		$(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
	});

	
	$(".prev").attr('disabled','disabled');
	$(".prev").css({color:'lightgray'});
		

	
	$("#call_date").datepicker({"dateFormat":"yy-mm-dd"});
	$("#chapter_proposal_date").datepicker({"dateFormat":"yy-mm-dd"});
	$("#notification_acceptance_date").datepicker({"dateFormat":"yy-mm-dd"});
	$("#publishing_fee_date").datepicker({"dateFormat":"yy-mm-dd"});
	$("#final_manuscript_date").datepicker({"dateFormat":"yy-mm-dd"});
	$("#1st_proof_reading").datepicker({"dateFormat":"yy-mm-dd"});
	$("#2nd_proof_reading").datepicker({"dateFormat":"yy-mm-dd"});
	$("#book_schedule_date").datepicker({"dateFormat":"yy-mm-dd"});
	
	$("#referenceString").click(function(){
		$("#referenceString").select();
	});
	
	initMenu();
	
});
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function alinks(path, by, direction){
	if(by == ""){by='title';}
	if(direction == ""){direction='asc';}
	path += '/by/'+by+'/direction/'+direction;
	$.get(path, {}, function(data, textStatus) { 
		$("#middle_container").hide(0);
		$("#middle_container").html(data);
		$("#middle_container").show(0);
	}, 'html');
	if($('#relatedBooks').length > 0){
		$("#relatedBooks").hide(0);
	}
     $(this).next().slideToggle('normal');	
}

//function used in browse by subjects for sorting subitems
function sublinks(path, by, direction, bysub, subdirection){
	if(by == ""){by='title';}
	if(direction == ""){direction='asc';}
	if(bysub == ""){bysub='title';}
	if(subdirection == ""){subdirection='asc';}
	path += '/by/'+by+'/direction/'+direction;
	path += '/bysub/'+bysub+'/subdirection/'+subdirection;
	$.get(path, {}, function(data, textStatus) { 
		$("#sub_items").hide(0);
		$("#sub_items").html(data);
		$("#sub_items").show(0);
	}, 'html');
	if($('#relatedBooks').length > 0){
		$("#relatedBooks").hide(0);
	}
     $(this).next().slideToggle('normal');	
}

function blinks(path){
	$.get(path, {}, function(data, textStatus) { 
		$("#bmiddle_container").hide("slow");
		$("#bmiddle_container").html(data);
		$("#bmiddle_container").show('slow');
	}, 'html');
	return false;
}

function download(path){
//	$.get(path);
	window.location.href=path;
	$.get('http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55529');
	return false;
}

function initMenu() {
  $('#bookmenu ul').hide();
  $('#bookmenu li a').click(
    function() {
        $(this).next().slideToggle('fast');	
    }
  );
}

function doRate(x, y){
	var path = '/books/rateobject/objectid/'+x+'/objecttype/'+y;
	$("#rate_loader").show(0);
	$("#rateContainer").hide(0);
	$.get(path, {}, function(data, textStatus) { 
		if(data.Title === 'Success'){
			var counting;
			var object_type;
			if(data.ratedTimes>1){
				counting = 'persons';
			} else {
				counting = 'person';
			}
			if(y == 'a'){
				object_type = 'article';
			} else {
				object_type = 'book';
			}
			$("#rateContainer").html('<span id=""votedContainer"">' + data.ratedTimes + '</span>' + ' ' + counting + ' like this '+object_type);
		} 
		dialogMsg(data.msg, data.Title);
	}, 'json');
	$("#rateContainer").show(0);
	$("#rate_loader").hide(0);
}

function AddToFavorites(){
	var  title = document.title; 
	var  url = window.location.href;
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
		return false;
	} else if( window.external ) {
		window.external.AddFavorite( url, title);
		return false;
	} else if(window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
		return false;
	}
}

function processComment(data){
	if(data.Title === 'Success'){
		$("#CommentsContainer").html(data.commentList);
		$("#comment").val("");
	}
	dialogMsg(data.msg, data.Title);
	$("#rate_loader").hide(0);
	$("#commentForm").show(0);
	$("#comment").focus();
}

function bSComment(){
	$("#rate_loader").show(0);
	$("#commentForm").hide(0);
}

function commentAlert(){
	var msg;
	msg = 'To protect this place from missuse you have to be logged in.<br><br><a href="/account/login">Login</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/account/signup">Signup</a>';
	dialogMsg(msg, 'Error');
}

function AddComment(){
	$("#commentFormContainer").show(300);
	$("#comment").focus();
	$("#commentButton").hide(0);
	$("#replyComment").val("-5");
}
 function doReply(comment){
	 $("#replyComment").val(comment);
	 $("#commentFormContainer").show(300);
	 $("#comment").focus();
	 $("#commentButton").hide(0);
 }
