﻿/* DWSWin 1.0
 * Copyright © 2008 Tiga Bilişim Hizmetleri Limited Şirketi - Her hakkı saklıdır
 * Adres: http://www.tiga.com.tr
 */ 
var winTypes = {None:0,Ok:1,YesNo:2,AutoExpire:3,Iframe:4};
var resultTypes = {Ok:0,Yes:1,No:2,Cancel:3,Close:4};
var savedTarget=null; 
var dragXoffset=0;  
var dragYoffset=0;  

function DWSWin(varName){

this.innerHTML='<br/>';
this.myName=varName==null?'DWSWinObject':varName;
this.id=this.myName+'_DWSWin_id';
this.contentId=this.myName+'_winBody_id';
this.loadingId=this.myName+'_loading_id';
this.title=culture.term('DefaultWinTitle');
this.winType=winTypes.None;
this.defaultWidth=325;
this.defaultHeight=75;
this.width=this.defaultWidth;
this.height=this.defaultHeight;
this.pageSize;
this.pageScroll;
this.padding='10px 10px 15px 10px';
this.x;
this.y;
this.backGroundOpacity=50;
this.opacity=100;
this.grayOut=false;
this.wrap=false;
this.closable=true;
this.movable=true;
this.titleEnable=true;
this.loadingImage=false;
this.expireTime=3000;
this.result='';
this.iframeUrl='';
this.iframeScroll='no';
this.darken=darken;
this.createWindow=createWindow;
this.close=close;
this.show=show;
this.disableScreen=disableScreen;
this.getCH=getCH;
this.setInnerHTML=setInnerHTML;
this.getPageSize=getPageSize;
this.getPageScroll=getPageScroll;
this.reArrange=reArrange;
this.reSizeWindow=reSizeWindow;
this.changeSelects=changeSelects;
this.visible=false;
this.contentVAlign='middle';
this.contentAlign='center';
this.onCloseParams='';
this.dragHandler=dragHandler;
function getCH(){
    if(this.titleEnable)
        return (this.height-22);
    else
        return this.height;
};  
function setInnerHTML(html){

if($(this.contentId))
   $(this.contentId).innerHTML=html;
 else
   this.innerHTML=html;
    
};
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}

	this.pageScroll = new Array('',yScroll); 
	return this.pageScroll;
};

function getPageSize(){
	
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	this.pageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
	return this.pageSize;
};

function reArrange(){
this.getPageSize();
this.getPageScroll();

var dark=document.getElementById('darkenScreenObject');
  if (dark && dark.style.display=='block')
      this.darken(true);
};

function changeSelects(state){
var selects = document.getElementsByTagName("select");
        for (var i = 0; i != selects.length; i++) {
                selects[i].style.visibility = state;
        }
};
        
function darken(vis) {

  var dark=document.getElementById('darkenScreenObject');
 
  if (!dark) {
   
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           
        tnode.style.position='absolute';                
        tnode.style.top='0px';                         
        tnode.style.left='0px';                     
        tnode.style.overflow='hidden';               
        tnode.style.display='none';                   
        tnode.id='darkenScreenObject';               
        tnode.innerHTML='<iframe class="hideSelectFrame" style="width:95%; height:95%;"></iframe>';       
        tbody.appendChild(tnode);                            
        dark=$('darkenScreenObject');

  }
  

  
 if(vis){
    setOpacity(dark,this.backGroundOpacity);
    dark.style.zIndex=50;        
    dark.style.backgroundColor='#000000';  
    dark.style.width= this.pageSize[0] + 'px';
    dark.style.height= this.pageSize[1] + 'px';
    dark.style.display='block';
    window.onresize=function(){if(DWSWinObject)DWSWinObject.reArrange();};
   }else{
    dark.style.display='none';
    
   }

};

function createWindow(){
	
	if(isEmpty(this.x) || isEmpty($(this.id)))
		this.x=((this.pageSize[0] - this.width) / 2)+150;
    else
        this.x=$(this.id).style.left.replace('px','');
    
    if(isEmpty(this.y) || isEmpty($(this.id)))
        this.y=this.pageScroll[1] + ((this.pageSize[3] - 35 - this.height) / 2)+60;
    else
        this.y=$(this.id).style.top.replace('px','');

	if($(this.id))
        remove(this.id);	
 
    var myBody=document.getElementsByTagName("body")[0];
    
    /* Creating table and setting properties*/
    var tbl     = document.createElement("table");
    var tblBody = document.createElement("tbody");
    var row = document.createElement("tr");
    var cell = document.createElement("td");
    setClass(tbl,'dwswin');
    tbl.setAttribute('id',this.id);
    tbl.setAttribute('cellPadding',0);
    tbl.setAttribute('cellSpacing',0);
    tbl.setAttribute('border',0);
    tbl.style.left=this.x+'px';
    tbl.style.top=this.y+'px';
    
    
    /*Creating header and setting properties*/
    if(this.titleEnable){
        var hdrCss =this.movable?'winHdr movable':'winHdr';
        setClass(cell,hdrCss);
        
        if(!this.grayOut) 
            cell.innerHTML+='<iframe id="'+this.id+'_iframe" class="hideSelectFrame"></iframe>';
        
        if(this.closable)
           cell.innerHTML+='<a href="#" alt="'+culture.term('Close')+'" onclick="'+this.myName+'.close(resultTypes.Close); return false;"></a>';
        
        cell.innerHTML+='<div class="movable">'+this.title+'</div>';
        row.appendChild(cell);
        tblBody.appendChild(row);
    }
    
    /* Creation content body and setting properties*/
    cell = document.createElement("td");
    setClass(cell,'winBody');
    cell.setAttribute('id',this.contentId);
    cell.setAttribute('align',this.contentAlign);
    cell.setAttribute('vAlign',this.contentVAlign);
    cell.style.padding=this.padding;
    
    if(this.wrap)
       cell.style.whiteSpace='nowrap';
       
    if(this.width!=this.defaultWidth)
        cell.style.width=this.width+'px';
    
    if(this.height!=this.defaultHeight)
        cell.style.height=this.height+'px';
    
    
    var winBodyHtml='';
               
    if(this.loadingImage)
            winBodyHtml+='<div id="'+this.loadingId+'" class="loading"></div>';
         
    if(this.winType==winTypes.Iframe)
        winBodyHtml+='<iframe id="iframe" style="visibility:hidden; position:absolute" onload="hide($('+this.myName+'.loadingId)); show(this);" width="'+this.width+'" height="'+this.getCH()+'" frameborder="0" marginheight="0" marginwidth="0" scrolling="'+this.iframeScroll+'" src="'+this.iframeUrl+'"> </iframe>';
    else
        winBodyHtml+=this.innerHTML;
    
           
    if(this.winType==winTypes.Ok || this.winType==winTypes.YesNo){
        winBodyHtml+='<center class="msgBtns">';
        if(this.winType==winTypes.Ok){
        winBodyHtml+='<input class="msgBut" type="button" value=" '+culture.term('Ok')+' " onclick="'+this.myName+'.close(resultTypes.Ok);"/>';
        }else if(this.winType==winTypes.YesNo){
        winBodyHtml+='<input class="msgBut" type="button" value="  '+culture.term('Yes')+'  " onclick="'+this.myName+'.close(resultTypes.Yes);"/>&nbsp;&nbsp;';
        winBodyHtml+='<input class="msgBut" type="button" value="  '+culture.term('No')+'  " onclick="'+this.myName+'.close(resultTypes.No);"/>&nbsp;&nbsp;';
        winBodyHtml+='<input class="msgBut" type="button" value="  '+culture.term('Cancel')+'  " onclick="'+this.myName+'.close(resultTypes.Cancel);"/>';
        }
    winBodyHtml+='</center>';
    }else if(this.winType==winTypes.AutoExpire)
                setTimeout("if($('"+this.id+"')) "+this.myName+".close();",this.expireTime);
                
  
    cell.innerHTML=winBodyHtml;
    row = document.createElement("tr");
    row.appendChild(cell);
    tblBody.appendChild(row);
    cell = document.createElement("td");
    cell.setAttribute('id',this.id+'_footer');
    row = document.createElement("tr");
    row.appendChild(cell);
    tblBody.appendChild(row);
    tbl.appendChild(tblBody);
    myBody.appendChild(tbl);
 
 };

function show(){
   this.visible=true;
   this.getPageSize();
   this.getPageScroll(); 

   if(this.grayOut)
      this.darken(true);
    
   this.createWindow();
   if(this.movable)
      document.onmousedown=dragHandler;
    
  this.reSizeWindow();
   
};

function reSizeWindow(){
 var elm=$(this.id+'_iframe');
   if(elm)
    elm.style.height=(findPos($(this.id+'_footer'),0)-findPos($(this.id),0)+3)+'px';
};
function disableScreen(){
  this.visible=true;
  this.getPageSize();
  this.getPageScroll(); 
  this.darken(true);
};

function close(r){
    this.visible=false;
    document.onmousedown=null;
    this.result=r==null?this.result:r;
    if(this.onClose)
        this.onClose(this.result,this.onCloseParams);
     
    remove(this.id);

    if(this.grayOut)
        this.darken(false);
};
function moveHandler(e){
  if (e == null) e = window.event;
  if (e.button<=1){
  
     savedTarget.style.left=(e.clientX-dragXoffset)+'px';
     savedTarget.style.top=(e.clientY-dragYoffset)+'px';
    document.onmousemove=null; 
    document.onmousemove=moveHandler;
    return false;
  }
};

function cleanup(e) {
  document.onmousemove=null;
  document.onmouseup=null;

};

function dragHandler(e){
  var htype='-moz-grabbing';
  if (e == null) { e = window.event; htype='move';} 
  var target = e.target != null ? e.target : e.srcElement;
  orgCursor=target.style.cursor;
  if (target.className.indexOf("movable")!=-1) {
     savedTarget=getActiveWindow(target);
     var dwsWinBody= savedTarget;     
     target.style.cursor=htype;
     dragOK=true;
     dragXoffset=e.clientX-parseInt(dwsWinBody.style.left);
     dragYoffset=e.clientY-parseInt(dwsWinBody.style.top);
     document.onmousemove=moveHandler;
     document.onmouseup=cleanup;
     return false;
  }
};
function getActiveWindow(elm){
do{
    if(!isEmpty(elm.className) && elm.className.indexOf('dwswin')!=-1)
        return elm;
    else
        elm=elm.parentNode;
        
}while(!isEmpty(elm))
return null; 
};

function findPos(obj,x)
  { x=isEmpty(x)?1:x;
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(x && obj.x)
        curtop += obj.x;
            else if(!x && obj.y)
                    curtop += obj.y;
    return curtop;
  }
};
  
var DWSWinObject=new DWSWin();
