var poppets = (function() {
	var poppets = function() {
		return new poppets.fn.init();
	}
    poppets.fn = poppets.prototype = {
        count: 0,
        config:   { },
        htmlObjs: { },
		init: function() {  
			if(arguments.length>0)
				for(id in arguments[0]) {
					this.config[id] = arguments[0][id]
					var target_obj = $('#'+arguments[0][id].target);
					
					if(typeof arguments[0][id].content != 'undefined') {
						target_obj = $(arguments[0][id].content);
					}
					
					var	cssClass     = target_obj.attr('class'),
						cssStyle     = target_obj.attr('style'),
                        orgHtmlOuter = "<div id=\""+id+"\" class=\""+cssClass+"\" style=\""+cssStyle+"\">";
						
					this.htmlObjs[id] = orgHtmlOuter + target_obj.html() + "</div>";
					
					var width = target_obj.css('width'),
						height = target_obj.css('height');
					
					this.config[id].height = height.substring(0, height.length-2);
					this.config[id].width  = width.substring(0, width.length-2);
					
					// log('#'+arguments[0][id].target, this.htmlObjs[id].height, this.htmlObjs[id].width);
					
                    target_obj.remove();
                }
		},
		create: function() {
			if(arguments.length>0) {
				html = this.htmlObjs[arguments[0]];
				var width = this.config[arguments[0]].width,
					height = this.config[arguments[0]].height,
					paddingLeft =  $(html).css('padding-left') || 'auto',
					paddingRight =  $(html).css('padding-right') || 'auto',
					css = '';
				
				var containerWidth = parseInt(width) + parseInt(paddingLeft.substring(0, width.length-2)) + parseInt(paddingRight.substring(0, paddingRight.length-2)),
					controlsHtml     = "",
                    controlItemsHtml = "";
				
				if(jQuery.browser.msie == true && containerWidth!='auto') {
					containerWidth = containerWidth + 20;
				}
				
				var h = parseInt(width.substring(0, width.length-2)) / 2;
				
				var top  = ((($(window).height() - parseInt(height)) / 2) - h) + "px",
					left = (($(window).width()  - parseInt(width)) / 2) + "px" 
					
				if(typeof this.config[arguments[0]].cssClass != 'undefined') 
					css = this.config[arguments[0]].cssClass;
				
				if(containerWidth!='auto') containerWidth = containerWidth + 'px';
				
				var outerHtml = "<div id=\"_"+arguments[0]+"\" class=\"poppets "+css+"\" style=\"width:"+containerWidth+"; display:none; \">",
					underLay  = "<div id=\"overLay\"></div>";
					
				if(typeof this.config[arguments[0]].close != 'undefined')
				    controlItemsHtml +="<span class=\"close\">Close</span>";
				
				if(typeof this.config[arguments[0]].max != 'undefined')
                    controlItemsHtml +="<span class=\"max\">Max</span>";

                if(typeof this.config[arguments[0]].min != 'undefined')
                    controlItemsHtml +="<span class=\"min\">Min</span>";

                if(controlItemsHtml>'' && this.config[arguments[0]].header != false)
                    controlsHtml = "<div class=\"poppetHeader\">"+controlsHtml+controlItemsHtml+"</div>";
                
				$('body').append(underLay+outerHtml+controlsHtml+html+'</div>');
				
				$("#_"+arguments[0]).css('display', 'block').css('top',top).css('left',left);
				
				if(typeof this.config[arguments[0]].fade != 'undefined')
					window.setTimeout("$('#"+arguments[0]+"').fadeIn(900)", 0);
				else
					$("#"+arguments[0]).css('display', 'block');
					
				$('#_'+arguments[0]+' .poppetHeader .close').click([arguments], this.hide);
                // $('#'+arguments[0]).click([arguments], this.primary); 
			}
		},
		show: function() {
            if(arguments.length>0) {
                this.create(arguments[0]);
			    // $('#'+arguments[0]).draggable();
				
				if(typeof this.config[arguments[0]].callBack != 'undefined')
					this.config[arguments[0]].callBack();
					
			}
		},
		hide: function() {
            var id = '';

            if(typeof arguments == 'object')
                id = arguments[0];

            if(arguments.length>0 && typeof arguments[0].data != 'undefined')
                id = arguments[0].data[0][0];
			
			if(id.length>0) {
                $("#_"+id).remove();
                $("#overLay").remove();
			}
		},
        primary: function() {
            if(arguments.length>0)
                $('#'+arguments[0].data[0][0]).css('z-index', 99999);
        },
		from: function(ref, from, to) {
			var xml = $.ajax({ url:from , 
							   async:false,
							   dataType:'html' });
							  
			this.init({ 'mypoppet' : { 'target':to, 'content':xml.responseText, 'close':true }});
			this.create(ref);
		},
		replaceContent: function() {
			
		}
	};
	// poppets.fn.init(); -- uncomment to run init() on js load or use as construtor
	return poppets.fn;
}());

