// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string image_loader
 * //@var global string rp_cms
 *
 * //@function close_popup()
 *
*/

// You need an anonymous function to wrap around your function to avoid conflict
(function($){
 
    // Attach this new method to jQuery
    $.fn.extend({ 
         
        // This is where you write your plugin's name
        set_center: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				getWindowScrollTop: true,
				objectWidth:		600, // The width of the popup.
				absoluteLeft:		'50%'
            }
                 
            var options = $.extend(defaults, options);
			
			//var $cm = this.each(function(index,element){
			// "this" is already a jQuery object: 
			// When you create the click function you can assign that element to a variable and reference it within:
			/*
			var $cm = this.ready(function(e){
				
			});
			*/
			
			var o = options;
			var object = $(this); // now the object is the selected element such as #popup
			
			// Set the pop up variable.
			var object_width = object.width();
			var object_height = object.height();
			var window_height = $(window).height();
			var scroll_top = $(window).scrollTop();
			var scroll_left = $(window).scrollLeft();
			var scroll_height = $("body").attr("scrollHeight");
			
			// Do the calculation.
			var scroll_bottom = scroll_top + window_height;
			var object_top = (window_height/2)-(object_height/2) - 5;
			var margin_left = (scroll_left + object_width)/2;
			
			// Get the top index number from the plugin.
			var index_highest = $("div").get_topZindex();
			//alert(index_highest);
			
			// Check if the index has the number more than 0, add 1 to the highest number.
			// Else set the number to 1.
			if(index_highest > 0) index_superhigh = index_highest + 1;
			else index_superhigh = 1;
			
			// Keep the lines below for checking.
			//alert(object);
			//alert(object_width);
			//alert($cm.selector);
			//alert(objectWidth);
			//alert(object_top);
			
			//alert(object.find('form').attr('action'));
			//alert(object.find('form').serialize());
			//alert(object.get(0).nodeName); // returns DIV
			
			if(o.getWindowScrollTop) 
			{
				//alert('1');
				if(object_height > window_height) object_top_new = scroll_top;
				else object_top_new = scroll_top + object_top;
			}
			else
			{
				//alert('2');
				if(object_height > window_height) object_top_new = 0;
				else object_top_new = object_top;
			}
			
			object.css({
				top:object_top_new + "px", 
				left:o.absoluteLeft,
				marginLeft: "-" + margin_left + "px",
				width:object_width + "px",
				zIndex:index_superhigh,
				display:"none"
			});
			
			object.fadeIn('slow', function(){
				//closePopup_public(object);
				//make_boxshadow_ie(target);
			});
			
			
        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

// ]]>
