/*
//*******
//
//	filename: pdf_modal.js
//	author: Zack Brown
//	date: 7th January 2011
//
//*******
*/

//declare PDFModal class
var PDFModal = Class.create();

//declare the PDFModal prototype
PDFModal.prototype = {

	//modal body element
	modal_body: null,
	
	//model overlay and wrapper
	modal_overlay: null,
	modal_wrapper: null,
	
	//init self
	initialize:function()
	{
		//gather body element
		this.modal_body = $$('body')[0];
		
		//declare elements
		this.modal_overlay = new Element("div", {id: "modal_overlay"});
		this.modal_wrapper = new Element("div", {id: "modal_wrapper"});
		this.modal_header = new Element("div", {id: "modal_header"});
		this.modal_title = new Element("h1", {id: "modal_title"});
		this.modal_close = new Element("img", {id: "modal_close", width: "16px", height: "16px", alt: "Modal Window", src: "images/smcms/cross.png"});
		this.modal_content = new Element("div", {id: "modal_content"});
		
		//declare form elements
		this.modal_form = new Element("form", {action: "site/media/", method: "post"});
		this.modal_form_fieldset = new Element("fieldset", {});
		this.model_form_first_name_input = new Element("input", {type: "text", name: "input_first_name"});
		this.model_form_last_name_input = new Element("input", {type: "text", name: "input_last_name"});
		this.modal_form_email_address_input = new Element("input", {type: "text", name: "input_email_address"});
		this.modal_form_submit_input = new Element("input", {type: "submit", value: "Request Media Pack", name: "input_submit"});
		this.modal_form_first_name_label = new Element("label", {});
		this.modal_form_last_name_label = new Element("label", {});
		this.modal_form_email_address_label = new Element("label", {});
		this.modal_form_submit_label = new Element("label", {});
		this.modal_form_first_name_paragraph = new Element("p", {});
		this.modal_form_last_name_paragraph = new Element("p", {});
		this.modal_form_email_address_paragraph = new Element("p", {});
		
		//set modal form label content
		this.modal_form_first_name_paragraph.innerHTML = "* First Name:";
		this.modal_form_last_name_paragraph.innerHTML = "* Last Name:";
		this.modal_form_email_address_paragraph.innerHTML = "* Email Address:";
		
		//append label content to labels
		this.modal_form_first_name_label.appendChild(this.modal_form_first_name_paragraph);
		this.modal_form_last_name_label.appendChild(this.modal_form_last_name_paragraph);
		this.modal_form_email_address_label.appendChild(this.modal_form_email_address_paragraph);
		
		//append input fields to labels
		this.modal_form_first_name_label.appendChild(this.model_form_first_name_input);
		this.modal_form_last_name_label.appendChild(this.model_form_last_name_input);
		this.modal_form_email_address_label.appendChild(this.modal_form_email_address_input);
		this.modal_form_submit_label.appendChild(this.modal_form_submit_input);
		
		//append labels to fieldset
		this.modal_form_fieldset.appendChild(this.modal_form_first_name_label);
		this.modal_form_fieldset.appendChild(this.modal_form_last_name_label);
		this.modal_form_fieldset.appendChild(this.modal_form_email_address_label);
		this.modal_form_fieldset.appendChild(this.modal_form_submit_label);
		
		//append fieldset to form
		this.modal_form.appendChild(this.modal_form_fieldset);
		
		//bind event listener for keyboard events
		this.modal_keyboard_listener = this.modal_keyboard_listener.bindAsEventListener(this);
		
		//bind event listener for mouse events
		this.modal_mouse_listener = this.modal_mouse_listener.bindAsEventListener(this);
		
		//observe click events on modal close image
		Event.observe(this.modal_close, "click", this.modal_hide.bind(this));
		
		//set default modal title
		this.modal_title.innerHTML = "Our Media Pack";
		
		//append notes to header
		this.modal_header.appendChild(this.modal_title);
		this.modal_header.appendChild(this.modal_close);
		
		//append nodes to wrapper
		this.modal_wrapper.appendChild(this.modal_header);
		this.modal_wrapper.appendChild(this.modal_content);
		
		//append nodes to body
		this.modal_body.appendChild(this.modal_overlay);
		this.modal_body.appendChild(this.modal_wrapper);
		
		//append form to modal
		this.modal_content.appendChild(this.modal_form);
		
		//hide overlay and wrapper
		this.modal_overlay.hide();
		this.modal_wrapper.hide();
		
		//gather media pack link elements
		var media_pack_link = $("media_pack_link");
		var media_pack_handle = $("media_pack_handle");
		var media_pack_additional = $("media_pack_additional");
		
		//check media pack link element is valid
		if(media_pack_link != undefined)
		{
			//observe click events
			Event.observe(media_pack_link, "click", this.modal_display.bind(this));
		}
		
		//check media pack handle element is valid
		if(media_pack_handle != undefined)
		{
			//observe click events
			Event.observe(media_pack_handle, "click", this.modal_display.bind(this));
		}
		
		//check media pack addtional element is valid
		if(media_pack_additional != undefined)
		{
			//observe click events
			Event.observe(media_pack_additional, "click", this.modal_display.bind(this));
		}
	},
	
	//wrapper function for handling keyboard events
	modal_keyboard_listener: function(event)
	{
		//gather key event
        var key_code = event.keyCode;
		
		//define escape key code
        var escape_key_code;
		
		//determine
        if (event.DOM_VK_ESCAPE)
		{
			//FF
            escape_key_code = event.DOM_VK_ESCAPE;
        }
		else
		{
			//IE
            escape_key_code = 27;
        }
        
		//check key code
        if(key_code == escape_key_code)
		{
			//hide modal
			this.modal_hide();
		}
	},
	
	//wrapper function for handling mouse events
	modal_mouse_listener: function(event)
	{
		//gather scroll dimensions
        var page_scroll_dimensions = document.viewport.getScrollOffsets();
		
		//set overlay scroll offset
		this.modal_overlay.setStyle({top: (page_scroll_dimensions[1] - 25) + "px"});
		
		//set wrapper scroll offset
		this.modal_wrapper.setStyle({top: (page_scroll_dimensions[1] + 35) + "px"});
	},
	
	//wrapper function for submitting the modal form
	modal_submit: function()
	{
		//create new ajax request
		new Ajax.Request("site/media/", {	method: "post",
											parameters: $("quote_sidebar").serialize(),
											onComplete: function()
											{
												//
											},
											onSuccess: function(result)
											{
												//
													
											}.bind(this)});
	},
	
	//wrapper function for displaying modal
	modal_display: function()
	{
		//observe keyboard events
		Event.observe(document, 'keydown', this.modal_keyboard_listener);
		
		//observe keyboard events
		Event.observe(window, 'scroll', this.modal_mouse_listener);
		
		//stretch overlay to fill screen
		this.modal_overlay.setStyle({width: document.viewport.getWidth() + "px", height: (document.viewport.getHeight() + 25) + "px"});
		
		//gather scroll dimensions
        var page_scroll_dimensions = document.viewport.getScrollOffsets();
		
		//set scroll offset for overlay
		this.modal_overlay.setStyle({top: (page_scroll_dimensions[1] - 25) + "px"});
		
		//set position for modal
		this.modal_wrapper.setStyle({top: (page_scroll_dimensions[1] + 35) + "px", left: ((document.viewport.getWidth() / 2) - (parseInt(this.modal_wrapper.getStyle("width")) / 2)) + "px"}).show();
		
		//fade overlay in
		new Effect.Appear(this.modal_overlay, {duration: 0.5, from: 0.0, to: 0.7, afterFinish: function() { this.modal_overlay.show(); }.bind(this) });
	},
	
	//wrapper function for hiding modal
	modal_hide: function()
	{
		//stop observing keyboard events
		Event.stopObserving(document, 'keydown', this.modal_keyboard_listener); 
		
		//stop observing mouse events
		Event.stopObserving(window, "scroll", this.modal_mouse_listener);
		
		//hide wrapper
		this.modal_wrapper.hide();
		
		//fade wrapper out
		new Effect.Fade(this.modal_overlay, {duration: 0.5, afterFinish: function() { this.modal_overlay.hide(); }.bind(this) });
	}
};

//observe window load event
Event.observe(window, 'load', function()
{
	//create a new form modal
	var pdf_modal = new PDFModal();
});
