(function($) {
//debug.setLevel(0);	
	window.APP = {
	
		init: function() {

			APP.legal.init();
			APP.print.init();
			APP.details.init();
			APP.tabs.init();
		}


		, modal : {
			

		
		}
		
		, tabs : {
			
			init: function() {
				debug.log('init tabs');
				$('#comparison_tabs').tabs();
			}
		
		}

		, details : {
		
			init: function() {
			
				$('details').each(APP.details._init_details);
			
			}

			, _init_details: function() {
				
				var ctxt = $(this);
				var link = ctxt.prev('a');
				link.qtip(
					{ 
					content: { text: ctxt.html() }
					, position: { 
						adjust: { screen: true } 
						, corner: {
							target: 'bottomLeft',
							tooltip: 'topLeft'
						}
					}
					, style: {
						classes: {
							tooltip: 'qtip-details'
						}
						, width: { min: 300, max: 600 }
					}
					, show: { solo: true }
					, hide: { fixed: true }
				}
				);
				link.click(function(e){
					e.preventDefault();
				});
				link.addClass('details');

				ctxt.hide();

			}
		
		}


		, prototypes: {
		
			get: function(name) {
			
				return $('#prototypes').find('.' + name).html();
			
			}
		
		}

		, print: {

			init: function() {
				$('a.printButton').live('click', APP.print.control.print);
			}

			, control: {

				print: function(e) {
					e.preventDefault();
					var rel = $(this).attr('rel');
					//$('body').addClass("print_" + rel);

					var print_me = $("#print_me");
					
					if (print_me.size() == 0) {
						var print_me = $('<div id="print_me">');
						$('body').append(print_me);
					}
					
					
					print_me.html($('#' + $(this).attr('rel')).clone());

					

					window.print();
					
					//$('body').removeClass("print_" + rel);
					//$('div#print_me').remove();
				}

			}

		}

		,legal: {
		
			init: function() {
				jQuery('#legalLink').append(
					jQuery('#prototypes .legalLink').html()
				)

				var ctxt = $('#legalLink a');				

				var text = $('#' + ctxt.attr('rel'));

				debug.log(text);

				ctxt.qtip(

					{
					content: {
						title: {
							text: text.find('h1').eq(0).text()
							,button: 'Close'
						}
						, text: text
					},
				position: {
					target: $(document.body) // Position it via the document body...
					, corner: {
							target: 'topMiddle',
							tooltip: 'topMiddle'
						}
					,adjust: {y: 50, resize: false, scroll: false } 
				},
				show: {
					when: 'click', // Show it on click
					solo: true // And hide all other tooltips
				},
				hide: { target: "#modal-blanket", when: 'click'},
				style: {
					width: { max: 750 },
					padding: '14px',
					border: {
						width: 3,
						radius: 0,
						color: '#666666'
					},
					name: 'light'
				},
				api: {
					beforeShow: function()
					{
						// Fade in the modal "blanket" using the defined show speed
						$('#modal-blanket').fadeIn('fast');
					}
					,onRender: function () {
						var api = ctxt.qtip('api');
						var title = api.elements.title;
						var content = api.elements.content;

						content.append(APP.prototypes.get('termsOfService'));
						title.append($('#prototypes .printButton').html());
						title.find('a').wrapInner('<span></span>').removeAttr('style');
					}
					, onShow: function() {
						$('#modal-blanket').one('click', function(){
							ctxt.qtip('hide');
						});

					}
					,beforeHide: function()
					{
						// Fade out the modal "blanket" using the defined hide speed
						$('#modal-blanket').fadeOut('fast');
					}
				}
				}
				);
			}




		}


	}

	$('document').ready(APP.init);

})(jQuery);

