$(document).ready(function() {

	$('#payment_method').change(function() {
		if(this.value == 'invoice') {
			$('#cc-form').hide();
			$('#invoice-form').show();
		}
		else {
			$('#invoice-form').hide();
			$('#cc-form').show();
		}
	});

	$('#showBuy').click(function() {
		if( $(this).attr('checked') != undefined) 
			var attrs = {showBuy:'true'};
		else
			var attrs = {};
	
		$.get(WEBROOT+'books/embed/getcode/'+BOOK_ID, attrs, function(data) {
			$('#theCode').html(data);
		});
	});

	$("input[name=rememberme]").click(function() {
		if($(this).attr("checked"))
			$("#rememberMessage").show();
		else
			$("#rememberMessage").hide();
	});

	$("a.submit").click(function() {
		$(this).parents("form").submit();
		return false;
	});

	$("a.backLink").click(function() {
		history.go(-1);
		return false;
	});
	$("input.backLink").click(function() {
		history.go(-1);
		return false;
	});

	// MODALS
	$('body').append('<div id="modal-big" class="jqmWindow"></div>');   
	$('body').append('<div id="modal" class="jqmWindow small"></div>');   
	$('#modal').jqm({ajax:'@href', trigger:'a.modal', onLoad:function(h){modalSetup();}, ajaxText:'Laddar...'});
	$('#modal-big').jqm({ajax:'@href', trigger:'a.modal-big', onLoad:function(h){modalSetup();}, ajaxText:'Laddar...'});
	
});

function modalHide(h) {
    h.w.fadeOut('fast');if(h.o)h.o.fadeOut('fast', function(){$(this).remove()});/*remove();*/
    h.o.remove();
}

function modalSetup(responseText, responseCode) {
    if(responseText) {
        $('#modal').html(responseText);
    }   
  
    $('a.modal', $('#modal')).click(function(){
        $.jqm.open($('#modal')[0]._jqm, this);
                return false;
    }); 

//§    $('form',$('#modal')).ajaxForm({success:modalSetup});

    $('#modal').prepend($('<a href="#" style="float: right;" class="jqmClose"></a>').click(function() {
        $('#modal').jqmHide();
            return false;
    }));
    $('#modal-big').prepend($('<a href="#" style="float: right;" class="jqmClose"></a>').click(function() {
        $('#modal').jqmHide();
            return false;
    }));
    $('#snimodal').prepend($('<a href="#" style="float: right;" class="jqmClose"></a>').click(function() {
        $('#snimodal').jqmHide();
            return false;
    }));
    
    
    if($('#albumListCopy').attr('value') == 'new')
        $('#newAlbumName').show();

    $('#modal input.cancel').click(function() {
        $('#modal').jqmHide();
        return false;
    }); 
    
    $('#snimodal input.cancel').click(function() {
        $('#snimodal').jqmHide();
        return false;
    }); 
}

