// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);       
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
})();

//Standart Functions
function trace(val)
{
	console.log(val);	
}

function urlEncode(val)
{
	return encodeURIComponent(encodeURI(val));
}

function openPopup(url, name, width, height)
{
    var left           = parseInt((screen.availWidth/2) - (width/2));
    var top            = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    window.open(url, name, windowFeatures);
}

function socialIco(id)
{	
   	var s_title = urlEncode("Shareing JavaScript");
    var s_url = urlEncode(window.location);
    
    var w_bar;
    var w_url = "";
	
    if (id == 0)
    {
		
        w_url = "http://www.facebook.com/sharer.php?t=" + s_title + "&amp;u=" + s_url;
        w_bar = "height=400, width=600, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    else if (id == 1)
    {
        w_url = "http://twitter.com/home?status=" + s_title + "\n "+ s_url +" &amp;url=" + s_url;
        w_bar = "height=400, width=800, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    else if (id == 2)
    {
        w_url = "http://friendfeed.com/share/bookmarklet/frame#title=" + s_title + "&amp;url=" + s_url;
        w_bar = "height=400, width=800, titlebar=no, status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars=1"
    }
    window.open(w_url.replace(/amp;/gi, ""), "", w_bar, w_url);
}

//Class Stage
var Stage = Class.extend(
{
	stageHeigth	: $(window).height(),
	stageWidth	: $(window).width(),
	
	init		: function()
	{
		$(window).resize(this.defOnResize);
	},
	
	defOnResize : function()
	{
		Stage.stageHeigth	= $(window).height();
		Stage.stageWidth	= $(window).width();
	}
});

var stage = new Stage();

(function ($)
{
	$.fn.center = function()
	{
		return	this.each(function(i)
				{
					var w = $(this).width();
					var ow = $(this).outerWidth();
					var ml = (w + (ow - w)) / 2;
					var h = $(this).height();
					var oh = $(this).outerHeight();
					var mt = (h + (oh - h)) / 2;
					
					$(this).css("position", "absolute");
					$(this).css("margin-top", "-" + mt + "px");
					$(this).css("margin-left", "-" + ml + "px");
					$(this).css("top", "50%");
					$(this).css("left", "50%");
				});
	};
})(jQuery);

(function ($)
{
	$.fn.vCenter = function()
	{
		return	this.each(function(i)
				{
					var h = $(this).height();
					var oh = $(this).outerHeight();
					var mt = (h + (oh - h)) / 2;
					$(this).css("margin-top", "-" + mt + "px");
					$(this).css("top", "50%");
					$(this).css("position", "absolute");
				});
	};
})(jQuery);

(function ($)
{
	$.fn.hCenter = function()
	{
		return	this.each(function(i)
				{
					var w = $(this).width();
					var ow = $(this).outerWidth();
					var ml = (w + (ow - w)) / 2;
					$(this).css("margin-left", "-" + ml + "px");
					$(this).css("left", "50%");
					$(this).css("position", "absolute");
				});
	};
})(jQuery);
