﻿/*Kodlama:Ahmet AKÇAY*/

var myMBox=null;
var myMBoxUsageStatu=0;
function mBox(handlerObject,bigTitle){
//myMBox = new myModalBox(handlerObject, bigTitle);
	if( myMBox == null) myMBox = new myModalBox(handlerObject, bigTitle);
	myMBox.refreshHadlerObject(handlerObject);
	//if (typeof(nodeMap)=="undefined") nodeMap = document
	return myMBox.init("myMBox");
}

function myModalBox(handlerObject,bigTitle){
	this.handlerObject = handlerObject;
	this.bgDiv = null; 
	this.contentArea = null;
	this.content = null;
	this.loader  = null;
	this.header  = null;
	this.footer  = null;
	this.Width = 400;
	this.Height = 400;
	this.originalWidth = 400;
	this.originalHeight = 400;
	this.Height = 400;
	this.linkType = null;
	this.targetType=null;
	this.title_ = bigTitle;	
	this.windowWidth = this.getWindowWidth();
	this.windowHeight = this.getWindowHeight();
	this.contentHeight=null;	
	this.root = "http://" + window.location.hostname + myApplicationName;
}
myModalBox.prototype = {
    refreshHadlerObject : function(handlerObject){
        this.handlerObject = handlerObject;
    },
	init: function(_myObjectName){
		// stupid iezy
		if(myMBoxUsageStatu==0){
		    str = "<div id=\"mBOXBG\" style=\"display:none;\"></div><div style=\"display:none;\" id=\"mBOXBG_Content\"><div id=\"mBOXBG_Content_Header\"></div><div id=\"mBOXBG_Content_Footer\"><a href='#' onclick='myMBox.hide();return false;'><img src='"+this.root +"/_i/closemBox.gif' border='0' alt='Kapat' /></a></div><div id=\"mBOXBG_Content_Area\"></div></div><div id=\"mBOXBG_Content_Loader\"><img src='"+this.root +"/_i/loadermBox.gif"+"' width=\"208\" height=\"13\" /></div>"
		    document.write(str)
		    myMBoxUsageStatu = 1;
		}

		this.bgDiv = mE("#mBOXBG");
		this.contentArea = mE("#mBOXBG_Content");
		this.content = mE("#mBOXBG_Content_Area");
		this.loader = mE("#mBOXBG_Content_Loader");
		this.header = mE("#mBOXBG_Content_Header");
		this.footer = mE("#mBOXBG_Content_Footer");
		
		mE("#mBOXBG_Content_Header").innerHTML = "<h1>" + this.title_ + "</h1>";
		
		this.bgDiv.onclick=function(){
			myMBox.hide();
		}	
			
		window.onscroll = function(){
		    myMBox.setPosition("content");
		    myMBox.windowWidth = myMBox.getWindowWidth();
	        myMBox.windowHeight = myMBox.getWindowHeight();
		}
		window.onresize = function(){
		    myMBox.windowWidth = myMBox.getWindowWidth();
	        myMBox.windowHeight = myMBox.getWindowHeight();
		    myMBox.setPosition("content");		    
		}
		
		for(var i=0,k=this.handlerObject.length;i<k;i++){
		//alert(this.handlerObject[i].innerHTML)
			this.handlerObject[i].onclick=function(){	
				myMBox.showContainer();
				myMBox.showContent(this);
				return false;
			}
		}
		return this;	
	},
	outOpen : function(link){
	    myMBox.showContainer();
	    myMBox.showContent(link);
	},
	hide: function(){
		this.bgDiv.style.display="none"
		this.contentArea.style.display="none"
		this.loader.style.display="none";
		this.content.innerHTML ="";
		this.ie6SelectBugFix("show")
		this.Width = 400;
	    this.Height = 400;
	    this.content.style.height = "100%";
	    // scroll açıldığında kapanmıyordu düzeltmek için
	    this.content.style.overflow="hidden";
	    document.body.onkeypress  = null;
	},
	showContainer: function(){	
    	document.body.onkeypress=function(){return false;}    
		windowHeight = this.windowHeight;		
		contentHeight = mE("body")[0].offsetHeight;	
		this.contentHeight = contentHeight;
		this.bgDiv.style.height = ((windowHeight<contentHeight)? (contentHeight) : windowHeight) + "px";
		this.bgDiv.style.display="block";
		if(this.contentArea.style.display=="none"){
		    this.loader.style.display="block";
		    this.setPosition("loader");
		}			
	},
	showContent: function(_link){
    	if(BrowserDetect.browser =="Internet Explorer" && BrowserDetect.version<7){
	        this.ie6SelectBugFix("hide");
	    }
	    // scroll açıldığında kapanmıyordu düzeltmek için
	    this.content.style.overflow="auto";
		this.getLinkAttribute(_link)
		   
	},
	getLinkAttribute: function(_link){
	    myHref = _link.href || _link;
	    if(myHref.indexOf("#")>0){this.linkType = "div"}
	    else if(myHref.indexOf(".html")>0 || myHref.indexOf(".htm")>0 || myHref.indexOf(".asp")>0 || myHref.indexOf(".aspx")>0 || myHref.indexOf(".txt")>0 || myHref.indexOf(".php")>0){
	        this.linkType = "ajax"
	    }	    else if(myHref.indexOf(".jpg")>0 || myHref.indexOf(".jpeg")>0 || myHref.indexOf(".gif")>0 || myHref.indexOf(".png")>0){this.linkType = "image"}	    	    
	    if(myHref.indexOf("w=")>0){
	        wStart = myHref.indexOf("w=") + 2	        
	        wFinish = (myHref.substr(wStart,myHref.length-wStart).indexOf("&")) + wStart
	        this.Width = myHref.substr(wStart,wFinish-wStart)	        	        
	        hStart = myHref.indexOf("h=") + 2
	        hFinish = myHref.length
	        this.Height = myHref.substr(hStart,hFinish-hStart)
	    }
	    if(myHref.indexOf("type=")>0){        	        
	        tStart = myHref.indexOf("type=") + 5
	        tFinish = myHref.length
	        this.targetType = myHref.substr(tStart,tFinish-tStart)
	    }
	    
	    this.loadContent(_link);  
	    //this.setPosition("content");    
	},
	
	setPosition: function(_type){
	if(this.bgDiv.style.display=="none") return false;		   	    
	   if(_type == "content"){
	        this.showContainer();	        
	        mW = parseInt(this.Width)	        
	        mH = parseInt(this.Height)
	       if( this.linkType == "image") {mW = parseInt(this.originalWidth); mH = parseInt(this.originalHeight);}
	        
	        //100 fazla fazla verilmiş normalde yukseklikten 60px olması lazım ama olsun :)
	        if ((mW>(this.windowWidth-(this.windowWidth*0.10)) || mH>(this.windowHeight-(this.windowHeight*0.20))) && this.linkType == "image"){
	           _mH = mH;
	           mH = this.windowHeight-(this.windowHeight*0.20);
	           mW= parseInt(mH*mW/_mH);
	           if(mW>(this.windowWidth-(this.windowWidth*0.10))){
	               _mW = mW;
	               mW = this.windowWidth-(this.windowWidth*0.10);
	               mH= parseInt(mH*mW/_mW);
	           }
	           if(mE("#mBOXBG_Image")!=null){  
	               mE("#mBOXBG_Image").style.width = mW+"px";
	               mE("#mBOXBG_Image").style.height = mH+"px";	
	               this.Width=mW;
	               this.Height=mH;	            
	           }
	        }
	        
	        /*if ((mW>(this.windowWidth-300) || mH>(this.windowHeight-250)) && this.linkType == "image"){
	           _mH = mH;
	           mH = this.windowHeight-250;
	           mW= parseInt(mH*mW/_mH);
	           if(mW>(this.windowWidth-300)){
	               _mW = mW;
	               mW = this.windowWidth-300;
	               mH= parseInt(mH*mW/_mW);
	           }
	           if(mE("#mBOXBG_Image")!=null){  
	               mE("#mBOXBG_Image").style.width = mW+"px";
	               mE("#mBOXBG_Image").style.height = mH+"px";	
	               this.Width=mW;
	               this.Height=mH;	            
	           }
	        }*/
	        
	        //ie 6 ve 7 de yuksekliklerde sacmaliyabiliyo header yuksekligi eklenip cikartiliyor 60px
	        if(this.linkType == "image"){ mH += 60;}	        
	        if(BrowserDetect.browser =="Internet Explorer" && BrowserDetect.version<7){mH -= 60}	
            
	        mT = ((this.windowHeight - mH) / 2) + this.getScrollXY()[1] 	        
	        mL = parseInt((this.windowWidth - this.Width) / 2) + this.getScrollXY()[0] 	       
	        //yukardayken anlasin asagiya yaklastigini
	        if(mT+mH > this.contentHeight - 60)  {mT = this.getScrollXY()[1] - 60}
	        this.contentArea.style.top = mT + "px";
	        //ie 6 da sacmaliyodu
	        if(BrowserDetect.browser =="Internet Explorer" && BrowserDetect.version<7){this.contentArea.style.top = mT-20 + "px";}
	        this.contentArea.style.left = mL + "px";
	        this.contentArea.style.height = mH + "px";
	        this.contentArea.style.width = mW + "px";
	        //this.content.style.width = mW + "px";
	        this.content.style.height = mH + "px";
	        if (this.linkType != "image" && this.targetType != "iframe") this.content.style.height = mH - 100 + "px";
	        if (this.linkType != "image" && this.targetType == "iframe") this.content.style.height = mH - 60 + "px";
	    }
	    else if(_type == "loader"){
	        mT = parseInt((this.windowHeight - this.loader.offsetHeight) / 2) + this.getScrollXY()[1]
	        mL = parseInt((this.windowWidth - this.loader.offsetWidth) / 2) + this.getScrollXY()[0] 
	        mW = this.loader.offsetWidth
	        mH = this.loader.offsetHeight    	    
	        //if(mT*2 + this.getScrollXY()[1] > windowHeight) return false;	
	        this.loader.style.top = mT + "px";
	        this.loader.style.left = mL + "px";
	    }
	   if(BrowserDetect.browser =="Internet Explorer" && BrowserDetect.version<7){ this.bgDiv.style.width = this.windowWidth + "px"  }
	    
	},
	loadContent: function(_link){
	    if(this.linkType == "ajax"){
	        this.setPosition("content"); 
	        myLink = _link.href || _link;
	        myFile = myApplicationName + myLink.replace(this.root,"");
	        
	        if(this.targetType != "iframe"){
	            var myAjx = new Acaks(
		            {
			            url: myFile,
			            successRun : function(html){
				            myMBox.content.innerHTML = html;
				            myMBox.contentArea.style.display="block";
				            myMBox.contentArea.className="normal"
				            myMBox.loader.style.display="none";
			            }
		            }
	            );
	        }
	        else{
	            str="<iframe id='mBOXBG_IFRAME' name='mBOXBG_IFRAME' src='"+myFile+"' scrolling='no' width='100%' height='100%' frameborder='0'></iframe>"
	            myMBox.content.innerHTML = str;
				myMBox.contentArea.style.display="block";
				myMBox.contentArea.className="forIFrame"
				myMBox.loader.style.display="none";
	        }
	        
	    }
	    else if(this.linkType == "image"){      
	            var myImg = new Image();
	            myImg.id = "mBOXBG_Image";
	            myImg.src = _link;
	            if(myImg.width==0){
	                myImg.onload = function(){
	                   myMBox.Width = this.width;
	                   myMBox.Height = this.height;	
	                   myMBox.originalWidth = this.width;
	                   myMBox.originalHeight = this.height;
	                   myMBox.Height = this.height;	        
                       myMBox.content.appendChild(this);
	                   myMBox.setPosition("content")
	                   myMBox.contentArea.className="forImage"
	                   myMBox.contentArea.style.display="block"; 
	                   myMBox.loader.style.display="none"; 
	                }
	            }
	            else{
	               myMBox.Width = myImg.width;
	               myMBox.Height = myImg.height;
	               myMBox.originalWidth = myImg.width;
	               myMBox.originalHeight = myImg.height;
	               myMBox.content.appendChild(myImg);
	               myMBox.setPosition("content")	
	               myMBox.contentArea.className="forImage"
	               myMBox.contentArea.style.display="block"; 
	               myMBox.loader.style.display="none";                               
	            }
	            
	            
	    }
	    else if(this.linkType == "div"){
	        this.setPosition("content");
	        _link = _link +"";
	        
	        if(_link.indexOf("?")>0){
	
	            myDiv = _link.substring(_link.indexOf("#"),_link.indexOf("?"))
	            //alert(myDiv)
	        }else{
	            myDiv = _link.substring(_link.indexOf("#"),_link.length)
	             //alert(myDiv)
	        }
  
	        myMBox.content.innerHTML = mE(myDiv).innerHTML;
	        myMBox.contentArea.style.display="block";
		    myMBox.contentArea.className="normal"
			myMBox.loader.style.display="none";
	        
	    }
	},
    ie6SelectBugFix : function(_type){
        var sel = mE("select");
        for (var i=0, k = sel.length;i<k;i++){
            if(_type == "hide"){
                sel[i].setAttribute("exType", sel[i].style.display);
                sel[i].style.visibility="hidden";
            }
            
            if(_type == "show"){
                if(sel[i].getAttribute("exType")!="none"){sel[i].style.visibility="visible";}
                
            }
        }        
    },
	getWindowHeight : function(){
		var windowHeight = 0;
		if(typeof(window.innerHeight) == 'number'){
			windowHeight = window.innerHeight;
		}else{
			if(document.documentElement && document.documentElement.clientHeight){
				windowHeight = document.documentElement.clientHeight;
			}else{
				if(document.body && document.body.clientHeight){
					windowHeight = document.body.clientHeight;
				}
			}
		}
		return windowHeight;
	},	
	getWindowWidth : function(){
		var windowWidth = 0;
		if(typeof(window.innerWidth) == 'number'){
			windowWidth = window.innerWidth;
		}else{
			if(document.documentElement && document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			}else{
				if(document.body && document.body.clientWidth){
					windowWidth = document.body.clientWidth;
				}
			}
		}
		return windowWidth;
	},
	getScrollXY: function (){
		var x = 0, y = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			// Netscape
			x = window.pageXOffset;
			y = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			// DOM
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			// IE6 standards compliant mode
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		return [x, y];
	}
	
}

