// -- some helpful functions -> has to be change to jQuerys -- //

//check browser
var ope = false;
var ie = false; 
if(navigator.userAgent.indexOf("MSIE") >=0) {
   ie = true;
} 
else if(navigator.userAgent.indexOf("Opera") >=0) {
   op = true;
} 

//return the width of window
function getWindowWidth() {
  if (window.innerWidth) {
    return window.innerWidth; //other Browser
  } 
  else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth; //IE
  } 
  else {
    return 0;
  }
}

//return the width of window
function getWindowHeight() {
   if (ie == true) {
      //return document.body.offsetHeight;     
      return document.documentElement.clientHeight;
   } 
   else {
      return window.innerHeight;
   }
}

//return the ScrollOffest
function getScrollOffsetY() {
   if (self.pageYOffset)  {
      return self.pageYOffset;  // all except Explorer
   }
   else if (document.documentElement && document.documentElement.scrollTop)  {
      return document.documentElement.scrollTop;   // Explorer 6 Strict
   }
   else if (document.body)  {
      return document.body.scrollTop; // all other Explorers
   }
}

// -- the jQuery function -- //
(function($){
   $.fn.ojCenter = function() {  	
      //window size
      var winWidth = getWindowWidth();
      var winHeight = getWindowHeight();
      //iterate selection      
      return this.each(function(index) {
         if (index == 0) {          
            var $this = $(this);
            //element size
            var elHeight = $this.height();
            var elWidth  = $this.width();
            //calculate top left corner
            var posX = Math.round((winWidth  - elWidth) / 2);
            var posY = Math.round((winHeight - elHeight) / 2);
                posY += getScrollOffsetY();
            if (posX < 0) {
            	posX = 0;
            }
            if (posY < 10) {
            	posY = 10;
            }
            //set new position   
            $this.css({
               position: 'absolute',
               margin: '0',
               left: posX,
               top: posY
            });
         }
      });
   }  
})(jQuery);