/**
 * The PDFA setLimits
 *
 * This application handles the Contract between the father & child
 * it will send data to the server and print the contract
 *
 * @author Miguel Julio (mjulio@mcdpartners.com)
 */
(function () {

    /**
     * The dei package
     *
     * @namespace pdfa
     * @package pdfa.dei.setLimits
     */
	pdfa.dei.setLimits = {
		renderContractHTML : function() {
			//Get child and parent inputs
			var childSelected = mcd.dom.getElementsByAttribute('name', 'check-child', '', 'input', true);
			var parentSelected = mcd.dom.getElementsByAttribute('name', 'check-parent', '', 'input', true);

			//Render names
			mcd.dom.getElement('render-child-name').innerHTML = mcd.dom.getElement('child-name').value;
			mcd.dom.getElement('render-parent-name').innerHTML = mcd.dom.getElement('parent-name').value;

			//Render child list
			var childOutput = "<ul>";
			for(var i = 0; i < childSelected.length; i++) {
				if(childSelected[i].checked) {
					childOutput += "<li>" + childSelected[i].value + "</li>";
				}
			}
			childOutput += "</ul>";
			mcd.dom.getElement('render-child-agrees').innerHTML = childOutput;

			//Render parent list
			var parentOutput = "<ul>";
			for(var i = 0; i < parentSelected.length; i++) {
				if(parentSelected[i].checked) {
					parentOutput += "<li>" + parentSelected[i].value + "</li>";
				}
			}
			parentOutput += "</ul>";
			mcd.dom.getElement('render-parent-agrees').innerHTML = parentOutput;

			return true;
		},

		printContract : function() {
			//Get contract output
			var printContent = mcd.dom.getElement('parent-child-agreement-output').innerHTML;

			//Load output into iframe
			var ifrm = mcd.dom.getElement('print-output');
			ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
			ifrm.document.getElementById('parent-child-agreement').innerHTML = printContent;

			//Focus on iframe and print
			ifrm.focus();
			ifrm.print();

			return true;
		},

		hideContract : function() {
			mcd.dom.addClass(mcd.dom.getElement('overlay-contract-agreement'), 'hide');
			mcd.dom.removeClass(mcd.dom.getElement('overlay-contract-form'),'hide');
		}
	};
})();


/**
 * The Setup Routine (Called when the DOM is ready)
 */
mcd.dom.ready(function () {
	//	Flash Call on click
	mcd.event.add('video-trigger-rules','click', function(){
		// sets the overlay title
		mcd.dom.getElement('step-title').innerHTML = 'Set Limits';
		mcd.dom.getElement('step-section').innerHTML = 'Lay Down Rules';

		// sets the flash information
		pdfa.dei.movieLoader.swf 		= '/assets/PDFA_VideoPlayer_04.swf';
		pdfa.dei.movieLoader.flashVar 	= { xmlFileName: '/assets/VideoPlayer_t2s3_rules.xml' };

		// embeds the flash movie
		pdfa.dei.movieLoader.embedFlash();
	});

	mcd.event.add('video-trigger-consequences','click', function(){
		// sets the overlay title
		mcd.dom.getElement('step-title').innerHTML = 'Set Limits';
		mcd.dom.getElement('step-section').innerHTML = 'Set Firm Consequences';

		// sets the flash information
		pdfa.dei.movieLoader.swf 		= '/assets/PDFA_VideoPlayer_04.swf';
		pdfa.dei.movieLoader.flashVar 	= { xmlFileName: '/assets/VideoPlayer_t2s3_consequences.xml' };

		// embeds the flash movie
		pdfa.dei.movieLoader.embedFlash();
	});

	mcd.event.add('btn-print-contract','click', pdfa.dei.setLimits.printContract);


	// obtrusive overlay setup
	 mcd.ObtrusiveOverlay.manager.init({
		'video-tips': {
			'close-link' : function() {
                pdfa.dei.movieLoader.closeOverlay();
                return false;
            }
		}
	});

    pdfa.dei.movieLoader.prepareOverlay();

	// Prevent form from submitting
	mcd.dom.getElement('overlay-contract-form').onsubmit = function(){ return false; };

	// Set up back buttton
	mcd.dom.getElement('btn-go-back').onmouseup = function() { pdfa.dei.setLimits.hideContract(); }



	mcd.Overlay.manager.init({
			'overlay-contract-form' : {
				position: {
					'x' : 335,
					'y' : 150
				},
				terminators : {
					'close-link': function(){
						return true;
					},
					'btn-continue': function(){
						mcd.dom.addClass(mcd.dom.getElement('overlay-contract-form'), 'hide');
						mcd.dom.removeClass(mcd.dom.getElement('overlay-contract-agreement'),'hide');
						mcd.event.add(mcd.dom.getElement('close-link-agreement'),'click', function(event) {
							mcd.event.preventDefault(event);
							mcd.dom.addClass(mcd.dom.getElement('overlay-contract-agreement'),'hide');
						});
						pdfa.dei.setLimits.renderContractHTML();
						return true;
					}
				}

			}
		});

});