/*
 * Poppy Windows (poppyWindows.js)
 * Author: Jason Crider
 * Email: xylude@gmail.com
 * 
 * Use it however you wish, but please leave this bit of text at the top.
 */

jQuery(document).ready(function(){
	jQuery('#closeBtn').live('click',function(){
		jQuery('#poppyWindow').fadeOut('slow',function(){
			jQuery('#poppyWindow').empty();
		});
		jQuery('#dimmer').fadeOut('slow');
	});
	jQuery('#okBtn').live('click',function(){
		jQuery('#poppyWindow').fadeOut('slow',function(){
			jQuery('#poppyWindow').empty();
		});
		jQuery('#dimmer').fadeOut('slow');
	});
	jQuery('body').append('<div id="poppyWindow"></div><div id="dimmer"></div>');
	jQuery("#dimmer").live('click',function(){
		jQuery('#poppyWindow').fadeOut('slow',function(){
			jQuery('#poppyWindow').empty();
		});
		jQuery('#dimmer').fadeOut('slow');
	});
});

function launchPoppyWindow(width,height,content,loadFromUrl)
	{
		var window_width = window.innerWidth;
		var lPos = ((window_width/2)-(width/2))-scrollbarWidth();
		var window_height = window.innerHeight;
		
		jQuery('#poppyWindow').css('left',lPos);
		if(height=='auto'){jQuery('#poppyWindow').css('height',height);}else{jQuery('#poppyWindow').css('height',height+'px');}
		jQuery('#poppyWindow').css('width',width+'px');
		jQuery('#poppyWindow').css('top','50px');
		if(loadFromUrl) {
			var window_content = jQuery.ajax({
				url:content,
				async:false
			}).responseText;
			jQuery('#poppyWindow').html('<div id="poppyWindowContent"><div class="closeBtn_holder"><div id="closeBtn">close</div></div>'+window_content+"</div>");
		} else {
			jQuery('#poppyWindow').html('<div id="poppyWindowContent"><div class="closeBtn_holder"><div id="closeBtn">close</div></div>'+content+"</div>");
		}
		jQuery('#dimmer').css('height',window_height);
		jQuery('#dimmer').fadeIn('slow');
		jQuery('#poppyWindow').fadeIn('slow');
	}
function swapPoppyWindowUrl(window_content,loadFromUrl)
	{
		jQuery('#poppyWindowContent').fadeOut('fast');
		jQuery('#poppyWindowContent').html('');
		if(loadFromUrl) {
			var content = jQuery.ajax({
				url:window_content,
				async:false
			}).responseText;
		} else {
			var content = window_content;
		}
		jQuery('#poppyWindowContent').html('<div id="poppyWindowContent"><div class="closeBtn_holder"><div id="closeBtn">close</div></div>'+content+"</div>").hide().fadeIn('fast');
	}
function closePoppyWindow()
	{
		jQuery('#poppyWindow').fadeOut('slow',function(){
			jQuery('#poppyWindow').empty();
		});
		jQuery('#dimmer').fadeOut('slow');
	}
function scrollbarWidth() {
    var div = jQuery('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it
    jQuery('body').append(div);
    var w1 = jQuery('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = jQuery('div', div).innerWidth();
    jQuery(div).remove();
    return (w1 - w2);
}
