/*
 This prototype handles the AJAX capabilities of the download manager app
 It will open and close a 'lightbox' style pop-up with a registration form
 
*/


PDF = function(doc){ 
    this.init(doc)
}

PDF.prototype = { 

    cache:'',
    opened:false,
    animating:false,
    doc: '',
    
    init:function(doc){
	
	var me = this;
		
	// get document filename
	path_array = doc.split("/")
	// -2 as last array element is empty due to removal of trailing /
	this.doc = path_array[path_array.length-2]
	PDF.prototype.doc = doc

	me.check_for_session()

    },
	
    open:function(){

	var me = this;

        $.ajax({ type: "POST", url: "/downloads/pdfbox/"+this.doc+'/',
	       
                success:function(response){
		    // cache form
                    PDF.prototype.cache = response;
		    // scroll to the top
		    $('html').animate({scrollTop: 0}, 200, function(){
			// place div over everything
			$("body").append("<div id='mask'></div>")
			$('#mask').css({'height' :$('html').height(), 'display' : 'block'})
			$('#mask').animate({opacity: 0.6}, 1000, function() {
			   // show form
			   $("#mask").after("<div id='download_form_container'>CONTAINER</div>")
			       $('#download_form_container').css({'display' : 'block'})
			       $('#download_form_container').html(response)		       
			       $('#download_form_container').animate({opacity: 1}, 500)
			       $("input#id_name").focus()
			       // add callback to submit action
			       me.add_callback()  
			});
		    });
                }
        })
        
    },
    add_callback:function(){
	var me = this
	$("#download_form_container form input[type='image']").click(function(){
	    var s = me.submit()
	    return false
	})
    },
    submit:function(){
	
	var me = this;
	
	// validate
	var name = $("#id_name").val()
	var company = $("input#id_company").val()
	var email = $("input#id_email").val()

	// checkbox



	if (name == "") {
	  $("fieldset#mm_name span.error").text('Please tell us your full name')
	  $("input#id_name").focus()
	  return false;  
	}

	if (email == "") {
	  $("fieldset#mm_email span.error").text('Please enter a valid email address')
	  $("input#id_email").focus()
	  return false;  
	}
	
	if (company == "") {   
	  $("fieldset#mm_company span.error").text('What company to you represent?')
	  $("input#id_company").focus()
	  return false;  
	}



	// grab all form elements
	var datastring = ''
	var kv_pair = ''
	
	$("#download_form_container form input[type=checkbox]").each( 
	    function() { 
		var checked = $(this).is(':checked')
		kv_pair = $(this).attr('name') +'='+ checked + '&' 
		datastring += kv_pair
	    } 
	);
	
	$('#download_form_container form :input[type=text]').each(function(){
	    val = $(this).val()
	    if(val.length > 0)
	    {
		key = $(this).attr('name')
		val = $(this).val()
		if(key.length>0)
		{
		    kv_pair = key +'='+ val + '&' 
		    datastring += kv_pair
		}		    
	    } 
	})
	
	$('#download_form_container form :input[type=hidden]').each(function(){
	    val = $(this).val()
	    if(val.length > 0)
	    {
		key = $(this).attr('name')
		val = $(this).val()
		if(key.length>0)
		{
		    kv_pair = key +'='+ val + '&' 
		    datastring += kv_pair
		}		    
	    } 
	})
	
	// ajax submit request	
	$.ajax({ type: "POST", data: datastring,  url: "/downloads/register_download/",
	    success:function(response){
			// add user to cache
			PDF.prototype.cache = email
			// update form
			$('#download_form_container').html(response)
			// add callback to form
			me.add_callback()
	    },
	    error:function(response){
			// this should NEVER be called
			$('#download_form_container').html('<p>unanticipated error</p>')
	    }
	
	});

	return false;
    },
    check_for_session:function(){
	
	var me = this

	$.getJSON("/downloads/cache/", {}, function(cache){
	    if(!cache['cache'])
	    {
		me.open()
	    }
	    else
	    {
		window.location = PDF.prototype.doc
	    }			
    	});
    },
	
    post_open:function(){
        // pass
    },
	
    close:function() {
	$('#download_form_container').animate({opacity: 0}, 200, function() {
	    $(this).remove()
	    $('#mask').animate({opacity: 0}, 200, function() {
		$(this).remove()
	    });
	});
    }
	
}