/**
 * @file - users.js
 * @author - deepakb (Mindfire Solution)
 * @version - $Id$
 * @date - 11 Nov, 2010
 * JS functionality for users pages
 **/
$(document).ready(function(){
  $('.chooseType').click(function(){
    chooseType();
  });
});
 function editAccount() {
	$('#msgerror').hide();
	$('#account').hide();
	$('#edittogacc').hide();
	$('#editacc').show();
	$('#viewtogacc').show();
 }
 
 function viewAccount() {
	$('#editacc').hide();
	$('#viewtogacc').hide();
	$('#account').show();
	$('#edittogacc').show();
 }
 
 function updateAccount() {
	$('#accupd').hide();
	$('#loaderdiv').show();
	$.post(webroot+'users/account', $('#accountform').serialize(),function(data) {	
		if (data == 1) {
			getUpdateUserData();
			$('#viewtogacc').hide();
			$('#edittogacc').show();
			$('#loaderdiv').hide();
			$('#accupd').show();
		} else {
			$('#msgerror').show();
			$('#loaderdiv').hide();
			$('#accupd').show();
		}
	});
 }
 
 function getUpdateUserData() {
	$.post(webroot+'users/data', '' ,function(data) {
		$('#account').html(data);
		$('#editacc').hide();
		$('#account').show();
	});
 }
 
function download(id){
	$('#downloader_form'+id).submit();
	$('.download').css('display', 'none');
	$('#downloading').css('display', 'block');
  setTimeout("location.reload(true)", 10000);
}

function redownload(id){
	$('#redownloader_form'+id).submit();
	//$('.download').css('display', 'none');
	//$('#downloading').css('display', 'block');
  setTimeout("location.reload(true)", 10000);
}

function lstDownload(element) {
  $(element).parent().parent().parent().submit(); 
  var parentDiv = $(element).parent().parent().parent().parent().parent();
  $(parentDiv).children('.download').css('display', 'none');
  $(parentDiv).children('#downloading').css('display', 'block');
  setTimeout("location.reload(true)", 10000);
}

function freeDownload(element) {
  $(element).parent().submit(); 
  var parentDiv = $(element).parent().parent().parent();
  $(parentDiv).children('.download').css('display', 'none');
  $(parentDiv).children('#downloading').css('display', 'block');
  setTimeout("location.reload(true)", 10000);
}

function doNothing() {

}

  
function addLibPick() {
  if ($('#addLibraryPick').attr('checked')) {
    var check = 1;
  } else {
    var check = 0;
  }
  $.post(webroot+'ebooks/add_as_Library_Pick', { ebookId : $("input:hidden[name='ebookId']").val(), addLibraryPick : check, title : $('.image img').attr('alt'), bookFormat : $("input:hidden[name='bookFormat']").val() },function(data) {
    
  });
}

function approvePatron(ans) {
  $.post(webroot+'users/isAgree', { agree : ans },function(data) {
    location.reload();
  });
}

function skipPopUpForm(ans) {
 $.post(webroot+'users/skip_pop', { data : ans },function(data) {
      location.reload();
 });
}

function checkPatron(libid,patronid)
{	
	var data = "libid="+libid+"&patronid="+patronid.replace('+','_');
	jQuery.ajax({
		type: "post",  // Request method: post, get
		url: webroot+"libraries/checkPatron", // URL to request
		data: data,  // post data
		success: function(response)
		{
				var msg = response.substring(0,7);
				if(msg == 'success')
				{
					setTimeout(function(){ checkPatron(libid,patronid) }, 30000);
				}
				else if(response != '')
				{
				//	alert("You have been logged out from the system. Please login again.");
				//	location.reload();
				//	return false;
					setTimeout(function(){ checkPatron(libid,patronid) }, 30000);
				}
		}
		
	});
	return false; 
}

function chooseType() {
  var epub = $('#chooseEpub').attr('checked');
  var pdf = $('#choosePdf').attr('checked');
  
  if(epub && pdf) {
    $.post(webroot+'pages/choose_type', { choice : 'both' },function(data) {
      location.reload();
    });
  } else if(epub) {
    $.post(webroot+'pages/choose_type', { choice : 'epub' },function(data) {
      location.reload();
    });
  } else if(pdf) {
    $.post(webroot+'pages/choose_type', { choice : 'pdf' },function(data) {
      location.reload();
    });
  } else {
    $.post(webroot+'pages/choose_type', { choice : 'both' },function(data) {
      location.reload();
    });
  }
}
