﻿function Controls(object)
{
   this.Target = object;
   
   this.getElement = function(object)
   {
      return typeof(object) == 'string' ? document.getElementById(object) : object;
   };

   this.getLeft = function(object,fromfloatingparent)
   {
       if(fromfloatingparent){
           if(object.style){
                if(object.style.position){
                    if(object.style.position=="relative" || object.style.position=="absolute"){
                        return 0;
                    }
                }
           }
       }
       return object.offsetLeft + (object.offsetParent ? this.getLeft(object.offsetParent,fromfloatingparent) : object.x ? object.x : 0);
   };
   
   this.getTop = function(object,fromfloatingparent)
   {
       if(fromfloatingparent){
           if(object.style){
                if(object.style.position){
                    if(object.style.position=="relative" || object.style.position=="absolute"){
                        return 0;
                    }
                }
           }
       }
       return object.offsetTop + (object.offsetParent ? this.getTop(object.offsetParent,fromfloatingparent) : object.y ? object.y : 0);    
   };

   this.getRight = function(object,fromfloatingparent)
   {
	   var leftpos = this.getLeft(object,fromfloatingparent);
	   return this.getWidth(object) + leftpos;
   };

   this.getBottom = function (object,fromfloatingparent)
   {
	   var bottompos = this.getTop(object,fromfloatingparent);
	   return this.getHeight(object) + bottompos;
   };
   
   this.getWidth = function(object)
   {
	   return object.offsetWidth;
   };

   this.getHeight = function(object)
   {
	   return object.offsetHeight;
   };

   this.attachMouseScrollEvent = function(comp){
      if(comp!=null){
          if (comp.addEventListener)
              comp.addEventListener('DOMMouseScroll', __DFonMouseWheel, false);
          comp.onmousewheel = __DFonMouseWheel;
      }
   };
   
   return this;
} 

Controls.prototype = 
{
   Control:function()
   {
      var comp = this.getElement(this.Target);
      this.attachMouseScrollEvent(comp);
      return comp;
   }, 
   Left:function(fromfloatingparent)
   {
      return this.getLeft(this.Control(),fromfloatingparent);
   },
   Top:function(fromfloatingparent)
   {
      return this.getTop(this.Control(),fromfloatingparent);
   },
   Right:function(fromfloatingparent)
   {
      return this.getRight(this.Control(),fromfloatingparent);
   },
   Bottom:function(fromfloatingparent)
   {
      return this.getBottom(this.Control(),fromfloatingparent);
   },
   Width:function()
   {
      return this.getWidth(this.Control());
   },
   Height:function()
   {
      return this.getHeight(this.Control());
   },
   getVisible:function()
   {
      var visible = true;
      if(this.Control().style){
        if(this.Control().style.display){
            if(this.Control().style.display=="none"){
                visible = false;
            }
        }
      }
      return visible;
   },
   setVisible:function(visible)
   {
       visible = visible==true ? "" : "none";
       this.Control().style.display = visible;
   },
   setZIndex : function(index){
       this.Control().style.zIndex = index;
   },
   setPosition:function(x,y){
        var obj = this.Control();
        if(obj!=null){
            obj.style.position = "absolute";
            obj.style.left = parseInt(x) + "px";
            obj.style.top = parseInt(y) + "px";
        }
   },
   setDimension:function(w,h){
        var obj = this.Control();
        if(obj!=null){
            obj.style.width = parseInt(w) + "px";
            obj.style.height = parseInt(h) + "px";
        }
   },
   makeSameSize:function(obj){
        obj = new Controls(obj);
        if(obj.Control()!=null){
            this.setPosition(obj.Left(),obj.Top());
            this.setDimension(obj.Width(),obj.Height());
        }
   },
   setTransparency:function(percent){
        var obj = this.Control();
        if(obj!=null){
            obj.style.filter = "alpha(opacity=" + (parseFloat(percent)) + ")";
            obj.style.KhtmlOpacity = parseFloat(percent)/100;
            obj.style.MozOpacity = parseFloat(percent)/100;
            obj.style.opacity = parseFloat(percent)/100;
        }
   },
   startDrag : function(event,keepHoldPosition){
        keepHoldPosition = typeof(keepHoldPosition)=="undefined" ? true : keepHoldPosition;
        var offsetX = 0;
        var offsetY = 0;
        var e = event ? event : window.event;
        var tempX = 0;
        var tempY = 0;
        if(Browser.isIE()){
          tempX = e.clientX + document.body.scrollLeft;
          tempY = e.clientY + document.body.scrollTop;
        }else{
          tempX = e.pageX;
          tempY = e.pageY;
        }
        if (tempX < 0){tempX = 0};
        if (tempY < 0){tempY = 0};
        if(keepHoldPosition==true){
            this.Control().offsetX = this.Left() - tempX;
            this.Control().offsetY = this.Top() - tempY;
        }else{
            this.Control().offsetX = 0;
            this.Control().offsetY = 0;
        }
        document.body.dragcomp = this;
        document.body.onmousemove = function(e){
            var e = e ? e : window.event;
            var tempX = 0;
            var tempY = 0;
            if(Browser.isIE()){
              tempX = e.clientX + document.body.scrollLeft;
              tempY = e.clientY + document.body.scrollTop;
            }else{
              tempX = e.pageX;
              tempY = e.pageY;
            }
            var x = (tempX + this.dragcomp.Control().offsetX);
            var y = (tempY + this.dragcomp.Control().offsetY);
            var hei = Math.max(this.offsetHeight + this.offsetTop,Window.InnerHeight());
            var wid = Math.max(this.offsetWidth + this.offsetLeft, Window.InnerWidth());
            if(y < 0){
                y = 0;
            }
            if(y > (hei - this.dragcomp.Height())){
                y = hei - this.dragcomp.Height();
            }
            if(x < 0){
                x = 0;
            }
            if(x > (wid - this.dragcomp.Width())){
                x = (wid - this.dragcomp.Width());
            }
            this.dragcomp.setPosition(x,y);
            return false;
        };
        document.body.onmouseup = function(){
            this.dragcomp.stopDrag();
        };
   },
   stopDrag : function(ondrop){
        document.body.onmousemove = null;
        RunOrExcecute(ondrop);
   }
}

var BrowserObj = function(){
    return this;
}
BrowserObj.prototype = {
    isIE : function(){
        return ((navigator.appName.indexOf("Internet Explorer") > -1) ? true : false);
    },
    havePNGSupport : function(){
        return this.isIE ? (parseFloat(navigator.appVersion) < 6 ? false : true) : true;
    }
}



var Browser = new BrowserObj();

var WindowObj = function(){
    return this;
}
WindowObj.prototype = {
    InnerWidth : function(){
        var WinInnerWid = 630;
        var WinInnerWid1 = 0;
        if( typeof( window.innerWidth ) == 'number' ) {    
        WinInnerWid1 = window.innerWidth;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
        WinInnerWid1 = document.documentElement.clientWidth;    
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
        WinInnerWid1 = document.body.clientWidth;    
        }
        WinInnerWid = WinInnerWid < WinInnerWid1 ? WinInnerWid1 : WinInnerWid;
        return WinInnerWid;
    },
    InnerHeight : function(){
        var WinInnerHei = 460;
        var WinInnerHei1 = 0;
        if( typeof( window.innerHeight ) == 'number' ) {    
        WinInnerHei1 = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
        WinInnerHei1 = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
        WinInnerHei1 = document.body.clientHeight;
        }
        WinInnerHei = WinInnerHei < WinInnerHei1 ? WinInnerHei1 : WinInnerHei;
        return WinInnerHei;
    },
    ScrollX : function(){
        return document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
    },
    ScrollY : function(){
        return document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
    }
}
var Window = new WindowObj();
function RunOrExcecute(fnstr){
    if(typeof(fnstr)=="function"){
        fnstr();
    }else if(typeof(fnstr)=="string"){
        if(fnstr.trim()!="null"){
            eval(fnstr);
        }
    }
}
function __DFonMouseWheel(event){
    var delta = 0;
    if(!event) event = window.event;
    if(event.wheelDelta){
        delta = event.wheelDelta/120;
    }else if(event.detail){ delta = -event.detail/3;}    
    if(delta){
        if (delta < 0){
            if(this.onMouseScrollDown!=null){this.onMouseScrollDown();}
        }else{
            if(this.onMouseScrollUp!=null){this.onMouseScrollUp();}
        }
    }
    if(event.preventDefault) event.preventDefault();
    event.returnValue = false;
}
function _ltrim(){
    return this.toString().replace(/^\s+/,"");
}
function _rtrim(){
    return this.toString().replace(/\s+$/,"");
}
function _trim(){
    return this.ltrim().rtrim();
}
function _isIn(){
    for(i=0;i<arguments.length;i++){
        if(this.trim().toUpperCase() == arguments[i].trim().toUpperCase()){
            return true
        }
    }
    return false
}
function _convertToDouble(){
    if(this.trim()!="" && !isNaN(this)){return this;}
    return null;
}
function _addTrialZeros(pre){
    var incvalue = this.cDouble();
    incvalue = incvalue ? incvalue : 0;
    if(incvalue < 10){incvalue = "0" + incvalue;}
    return incvalue;
}
String.prototype.ltrim = _ltrim;
String.prototype.rtrim = _rtrim;
String.prototype.trim = _trim;
String.prototype.isIn = _isIn;
String.prototype.cDouble = _convertToDouble;
String.prototype.addTrialZero = _addTrialZeros;
Number.prototype.ltrim = _ltrim;
Number.prototype.rtrim = _rtrim;
Number.prototype.trim = _trim;
Number.prototype.isIn = _isIn;
Number.prototype.cDouble = _convertToDouble;
Number.prototype.addTrialZero = _addTrialZeros;

Array.prototype.add = function(object){
    this[this.length] = object;
}
Array.prototype.remove = function(index){
    this.splice(index,1);
}
Array.prototype.push = function(item){
    this[this.length] = item;
}
Array.prototype.pop = function(){
    this.remove(this.length-1);
}
Array.prototype.count = function(){
    return this.length;
}

var onLoadFunctions = new Array();
var onAfterLoadFunctions = new Array();
var onResizeFunctions = new Array();
var DFControlsWindowOnResize = function(){
    var dori = 0;
    for(dori=0;dori<onLoadFunctions.length;dori++){
        RunOrExcecute(onResizeFunctions[dori]);
    }
}
if(window.addEventListener){
    window.addEventListener("onresize",DFControlsWindowOnResize,false);
}else{
    window.onresize = DFControlsWindowOnResize;
}

function getDocumentStatus(){
    return document.readyState;
}
var checkInterval = 100;
var checkTimoutId = null;
function checkUntilComplete(){
    clearTimeout(checkTimoutId);
    var currStatus = getDocumentStatus();
    if(currStatus!=null){
        if(currStatus.toLowerCase()=="complete"){
            setTimeout(onPageLoadComplted,100);
        }else{
            checkTimoutId = setTimeout(checkUntilComplete,checkInterval);
        }
    }else{
        onload = onPageLoadComplted;
    }
}
checkUntilComplete();
function onPageLoadComplted(){
    var doli = 0;
    for(doli=0;doli<onLoadFunctions.length;doli++){
        RunOrExcecute(onLoadFunctions[doli]);
    }
    
    doli = 0;
    for(doli=0;doli<onAfterLoadFunctions.length;doli++){
        RunOrExcecute(onAfterLoadFunctions[doli]);
    }
}
