/**
 * @author abhaga
 */
var wait_message = 'My Message';
var help_message = 'Little help';
var pbook_buy_panel_setup = false;

$(document).ready(function(){

	setupLoginDialog();
	setup_wait_dialog();
	setup_help_dialog();

	$('#navtop-login').click(function(){
		login();
		return false;
	});
	
	$('#login_button').live("click", function(){
		login_request();		
		return false;
	});

	$('#open_dashboard').click(function(){
		auth_GET('/dashboard');
		return false;
	});

	$('#pbook-buy').live('click',function(){
		setup_pbook_buy_dialog($('#blogbook_hash').val(), $('#blogbook_price').val(),$('#blogbook_pages').val());
		return false;
	});
	$('#ebook-download').click(function(){
		$('#download-op').val($(this).attr('name'));
	});
	$('#pbook-download').click(function(){
		$('#download-op').val($(this).attr('name'));
	});
	
	$('.savedbook_delete').live('click',function(){
		window.tobedeleted = $(this)		
		confirm_and_delete_saved_book();
		return false;
	});

	// Setup faq hover
	$('.sidebar-faq li').hover(
		function (){
			$(this).addClass('sidebar-faq-clickme');
		},
		function (){
			$(this).removeClass('sidebar-faq-clickme');
		});
	
	$('input[type=text]').focus(function(){
		this.select();
	});
});

function confirm_and_delete_saved_book(){
	var content = '<div id="confirm-dialog-content">';
	content += '<div id="confirm-message">Do you really to delete the saved book?</div>';
	content += '</div>';
	$(content).addClass('confirm-dialog rounded-corners').dialog({
		modal: true,
		autoOpen: true,
		bgiframe: true,
		closeOnEscape: true,
		overlay: { 'background-color': 'gray', 'opacity': 0.5 },
		resizable: false,
		buttons:	{
			cancel: function(){
				$(this).dialog('close');
			},
			Delete: function(){
				$(this).dialog('close');
				save_data = "id="+window.tobedeleted.siblings('.savedbook_id').val();
				window.tobedeleted.append('<img src="/site_media/images/spinner.gif">')
				auth_ajax({
					type: 'post',
					dataType: 'json',
					url: '/delete_book',
					data: save_data,
					success: function(data, textStatus){
						alert('Successfully deleted.');
						window.tobedeleted.parent().parent().remove();
						window.tobedeleted = null;
					},
					error: function(XMLHttpRequest, textError, exception){
						alert('Could not delete the book. Please try again or report the issue.')
					}
				});
			}
		}
	}).siblings('.ui-dialog-titlebar').hide();		
}

function setup_wait_dialog(){
	var content = '<div id="wait-dialog-content">';
	content += '<div id="wait-message"></div>';
	content += '<div id="spinner-div"><img src="/site_media/images/loader.gif"></div>';
	content += '</div>';

	$(content).addClass('wait-dialog rounded-corners').dialog({
		modal: true,
		autoOpen: false,
		bgiframe: true,
		closeOnEscape: false,
		height: 120,
		overlay: { 'background-color': 'gray', 'opacity': 0.5 },
		resizable: false,
		open: function(){
			$('#wait-message').text(wait_message);
		}
	}).siblings('.ui-dialog-titlebar').hide();
}

function setup_help_dialog(){
	var content = '<div id="help-dialog-content">';
	content += '<div id="help-message"></div>';
	content += '</div>';

	$(content).addClass('help-dialog rounded-corners').dialog({
		autoOpen: false,
		bgiframe: true,
		width: 300,
		closeOnEscape: true,
		resizable: true,
		position: 'right',
		open: function(){
			$('#help-message').html(help_message);
		},
		buttons: {
			close: function(){
				$(this).dialog('close');
			}
		}
	}).siblings('.ui-dialog-titlebar').css('font-size','30%');
}

function setup_pbook_buy_dialog(hashstr, price, pages){
	if(window.pbook_buy_panel_setup == false){
		var content = '<div id="pbook-buy-dialog-content">';
		content += '<div id="pbook-buy-message"><span style="font-size: 1em;"><b>Price</b>: Rs.<span id="pbook-buy-price">'+price+'</span>/- per copy (Free Shipping within India)</span>';
		content += '<br/>';
		content += '<form id="pbook-qty-form" action="buy_book"><input type="text" id="pbook-buy-qty" name="qty" value="1">';
		content += '<input type="hidden" name="bookcode" id="pbook-buy-hashstr" value="'+hashstr+'">';
		content += '<input type="hidden" name="pages" id="pbook-buy-pages" value="'+pages+'">';
		content += '<input type="submit" value="Update Quantity"></form>';
		content += '<div style="font-size: 1em; margin-bottom: 20px;"><b>Total</b>: Rs.<span id="pbook-buy-total"></span></div>';
		content += '<a target="_new" id="pothi-link" href=""><img src="/site_media/images/BuyNow.png"></a>';
		content += '<div style="margin-top: 20px; font-size: 12px; font-style: italic;">*Please note that non-English content in blogs may not render correctly. Please check the preview before ordering.</div>';
		content += '<div style="font-size: 12px; font-style: italic; margin-bottom: 10px;">*Any images in the book will be printed in grayscale.</div>';
		content += '</div></div>';
	
		$(content).addClass('pbook-buy-dialog rounded-corners').dialog({
			modal: true,
			autoOpen: false,
			bgiframe: true,
			closeOnEscape: true,
			title: 'Buy a printed copy',
			width: 550,
			overlay: { 'background-color': 'gray', 'opacity': 0.5 },
			resizable: true
		}).siblings('.ui-dialog-titlebar').css('font-size','60%');
		window.pbook_buy_panel_setup = true;
		$('#pothi-link').click(function(){
			$('.pbook-buy-dialog').dialog('close');
		});
		
		$('#pbook-qty-form').submit(function(){
//			alert('here');
			if($('#pbook-buy-qty').val().match(/^\d+$/) == null || $('#pbook-buy-qty').val() < 1){
				$('#pbook-buy-qty').val('1');
			}
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: '/buy_book',
				data: $("#pbook-qty-form").serialize(),
				success: function(data, textStatus){
					$('#pothi-link').attr('href',data.url);
					$('#pbook-buy-total').text($('#pbook-buy-price').text()*$('#pbook-buy-qty').val());
				},
				error: function(XMLHttpRequest, textError, exception){
					alert('Error')
				}
			});
			return false;
		}).submit();
		$('.pbook-buy-dialog').dialog('open');
	}
	else{
		$('#pbook-buy-qty').val('1');
		$('#pbook-buy-price').text(price);
		$('#pbook-buy-total').text(price);
		$('#pbook-buy-hashstr').val(hashstr);
		$('.pbook-buy-dialog').dialog('open').show();
	}
}

