/*

Popupwindow plugin for jQuery.
  
Takes a link and will create a popupwindow based on the href of the link. You can
over ride the default setting by passing your own settings using the REL attribute
of the link. You can have different setting for each link if you'd like.
   
To use just include the plugin in the HEAD section of the page AFTER calling jQuery.
After that, use jQuery to find the links you want and pass any parameters you want
   
<html>
<head>
	<title>PopupWindow Example</title>
  	<script type="text/javascript" src="jquery-latest.pack.js"></script>
   	<script type="text/javascript" src="jquery.popupwindow.js"></script>
   	<script type="text/javascript">
   	$(function(){
   		$(".popupwindow").popupwindow();
   	});
   	</script>
</head>
<body>
  
<p>Opens a popupwindow with default settings<br/>
<a href="http://www.jquery.com" class="popupwindow">jQuery Homepage</a></p>
  
<p>Open a popupwindow with each link having it's own settings<br/>
<a href="http://www.jquery.com" class="popupwindow" rel="height:400,width:400">jQuery Homepage</a><br/>
<a href="http://www.jquery.com" class="popupwindow" rel="height:550,width:750,toolbar:1,scrollbars:1,status:1,resize:0,left:50,top:100">jQuery Homepage</a></p>

</body>
</html>
  
*/

jQuery.fn.popupwindow = function(){
	return this.each(function(index){
		var setting, parameters, mysettings, b;
		
		// for overrideing the default settings
		mysettings = (jQuery(this).attr("rel") || "").split(",");
		
		settings = {
            name:"PopUpWindow", // default name
            height:600, // sets the height in pixels of the window.
			width:600, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:1, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resize:1, // whether the window can be resized {1 (YES) or 0 (NO)}.
			left:0, // left position when the window appears.
			top:0 // top position when the window appears.
		};

		// overrides the settings with parameter passed in using the rel tag.
		for(var i=0; i < mysettings.length; i++)
		{
			b = mysettings[i].split(":");
			if(typeof settings[b[0]] != "undefined" && b.length == 2)
			{
				settings[b[0]] = b[1];
			}
		}

        parameters = "height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resize=" + settings.resize + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;

        jQuery(this).bind("click", function(){
            var win = window.open(this.href, settings.name, parameters).focus();
            return false;
		});
	});
};
