﻿/**
 * Copyright (c) 2009 Michael Piera (idianet.net)
 * Inner windows plugin
 *
**/
(function($) {
	jQuery.fn.innerWindow = function(params) {

		// single 	= boolean (can be open more than one)
		// ident 	= if not single, ident is the inner window ID else zindex is ID
		var params = $.extend({
			zindex 	: 0,
			single 	: 'single',
			ident 	: 'default',			
			title 	: 'sans titre',
			height	: Math.round($(window).height()/2),
			width 	: Math.round($(window).width()/2),
			load	: 'index.php'
		}, params);

		var id		= ((params.single=='single') ? params.ident : params.zindex);
		var iw_id	= '#iw_'+id;
		var iw 		= iw_id;
		var iw_r 	= iw+' .iw_resizer';
		var	iw_c 	= iw+' .iw_content';

		this.each(function(){
			create_iw();
		});
		
		function create_iw() {
			$("#body").append('<div class="innerWindow" id="iw_'+id+'">'+
				'<div class="iw_resizer">'+
					'<div class="iw_header">'+
						'<div class="iw_title">'+params.title+'</div>'+
						'<div class="iw_opt">'+
							'<div class="iw_opt_show-hide"><span class="show" title="Réduire"></span></div>'+
							'<div class="iw_opt_max-restor"><span class="max" title="Agrandir"></span></div>'+
							'<div class="iw_opt_close"><span class="close" title="Fermer"></span></div>'+
						'</div>'+
					'</div>'+
					'<div class="iw_content"></div>'+
				'</div>'+
			'</div>');	
			// center inner window
			var ww = params.width;
			var wh = params.height;
			var wt = $('body').height()/2-wh/2; 
			var wl = $('body').width()/2-ww/2;
			var wtop = $(document).scrollTop()+(wh/2);			
			$(iw).css('top', wtop);
			$(iw).css('left', wl);
			$(iw_r).css('width', ww);
			$(iw_r).css('height', wh);
			$(iw_c).css('width', ww-2);
			$(iw_c).css('height', wh-32);
			$(iw_c).addClass('loading');
			$(iw_c).load(params.load,{}, function() {
				$(iw_c).removeClass('loading');								  
			}); 
			// focus an z-index	
			$(iw).switchIndex({
				group 	: '.innerWindow',
				zindex 	: 'iw',
				focus	: true,
				msDown	: false
			});			
		};
		
	// ----------------------------------------------
	// IW Maximize/Restor
	// ----------------------------------------------	
		$(iw_id+' .iw_opt_max-restor').toggle(
			function(){	
				// Save dimension var 
				this.iw_position 			= $(iw).position();
				this.iw_resizer_height 		= $(iw_r).height();
				this.iw_content_height 		= $(iw_c).height();	
				this.iw_resizer_width 		= $(iw_r).width();
				this.iw_content_width 		= $(iw_c).width();
				this.iw_content_display 	= $(iw_c).css('display');
				// switch button			
				var newClass = $(this).children('span').attr('class').replace(/max/, 'restor');
				$(this).children('span').attr("class", newClass);
				$(this).children('span').attr("title", "Restaurer");
				// new dimension
				$(iw_r).css('height', $(window).height()-6);
				$(iw_r).css('width', $(window).width()-6);				
				$(iw_c).css('height', $(iw_r).height()-32);				
				$(iw_c).css('width', $(iw_r).width()-2);
				$(iw_c).css('display','');
				$(iw).css('top', $(document).scrollTop());
				$(iw).css('left', 0);
				// disable resize / draggable
				$(iw).draggable('disable');					
				$(iw).resizable('disable');			

			},function(){				
				// switch button
				var newClass = $(this).children('span').attr('class').replace(/restor/, 'max');
				$(this).children('span').attr("class", newClass);
				$(this).children('span').attr("title", "Agrandir");				
				// restor dimension
				$(iw_r).css('height', this.iw_resizer_height);			
				$(iw_r).css('width', this.iw_resizer_width);
				$(iw_c).css('height',this.iw_content_height );
				$(iw_c).css('width', this.iw_content_width );
				$(iw_c).css('display', this.iw_content_display);
				$(iw).css('top', this.iw_position.top);
				$(iw).css('left', this.iw_position.left);
				// enable resize / draggable
				$(iw).draggable('enable');	
				$(iw).resizable('enable');
		});	

	// ----------------------------------------------
	// IW Show all / hide content
	// ----------------------------------------------
		$(iw_id+' .iw_opt_show-hide').toggle(
			function(){
				// Save dimension var
				this.iw_resizer_height = $(iw_r).height();
				// switch button
				var newClass = $(this).children('span').attr('class').replace(/show/, 'hide');
				$(this).children('span').attr("class", newClass);
				$(this).children('span').attr("title", "Restaurer");		
				// new dimension
				$(iw_r).css('height','auto');
				$(iw_c).css('display','none');							
			},function(){
				// switch button
				var newClass = $(this).children('span').attr('class').replace(/hide/, 'show');
				$(this).children('span').attr("class", newClass);
				$(this).children('span').attr("title", "Réduire");	
				// restor dimension
				$(iw_r).css('height',this.iw_resizer_height);
				$(iw_c).css('display','');					
		});	
	
	// ----------------------------------------------
	// IW close
	// ----------------------------------------------		
		$(iw_id+' .iw_opt_close').click(function(){
			iw_close();
		});	
		function iw_close() {
			$(iw).fadeOut(
                "fast",
                function(){
					$(iw).remove();
			});
		}
		
	// ----------------------------------------------
	// IW resizable
	// ----------------------------------------------		
		$(iw_id+' .iw_resizer').resizable({
			minHeight: 50,
			minWidth: 300,
			containment: 'document',
			start : function() {
				$(iw_c).css('opacity','0');
			},
			stop : function() {
				this.iw_ch = $(this).height()-32;
				this.iw_cw = $(this).width()-2;	
				$(iw_c).css('display','');				
				$(iw_c).css({'height':this.iw_ch,'width':this.iw_cw});
				$(iw_c).css('opacity','');
			}				
		});

	// ----------------------------------------------
	// IW draggable
	// ----------------------------------------------	
		$(iw).draggable({
			handle: '.iw_header',
			containment : 'body',
			start : function() {
				$(iw_c).css('opacity','0');
			},
			stop : function() {
				$(iw_c).css('opacity','');						
			}			
		});	
		
	// ----------------------------------------------
	// IW toggle INDEX
	// ----------------------------------------------
		$(iw).mousedown(function(){
			$(iw).switchIndex({
				group 	: '.innerWindow',
				zindex 	: 'iw',
				focus	: true,
				msDown	: false
			});
		});
		
	};
})(jQuery);