//---------------------------------------------------------------------
// itobox.js - LiteBox rake iframe viewer
//   version 0.01 (Oct. 20, 2008)
//
// License:      MIT License
// Copyright:    Copyright (c) 2007-2008 ItoKuro inc. Hirokazu Ito
//---------------------------------------------------------------------

try{
    anime = new iAnime();
}catch(e){}
var itoBox = {
    init: function(){
        var links = document.links || document.getElementsByTagName('a');
        $each(links, function(e, i){
            if (e.rel && e.rel.test(/^lightbox/i)){
		if (  e.addEventListener ) {
                     e.addEventListener("click", function(){itoBox.openIbox(this);}.bind(e), false );
                }else{
                     e.attachEvent("onclick", function(){itoBox.openIbox(this);}.bind(e))
                }
            }
        }, this);
	
    },

    openhref: function(href, obj){
        //direct open window
	this.current_a_y = document.documentElement.scrollTop || document.body.scrollTop;
        scrollTo(0,0); 
        this.mode = "openhref";
        return this.open_window(href);
    },

    openIbox: function(obj){
      /*
        if(this.mode != undefined){
            obj.href = "/";
            return false;
        }
        */
        this.mode = "openbox";
        this.current_a   = obj;
        this.current_a_y = document.documentElement.scrollTop || document.body.scrollTop;
        this.current_href = obj.href;
        obj.href = "#";
        return this.open_window(this.current_href);
    },

    closebox: function(){
        obj = $("iboxMain");
        if($("iboxMain").getStyle('display') && $("iboxMain").getStyle('display') != 'none'){
     	    //kyo if iboxMain is being displayed , let this box disapear. avoid shifting
	    $("iboxMain").setStyle('display','none');
	}
	if(this.mode == "openbox"){
            this.current_a.href = this.current_href;
        }
        return this.close_window(obj);
    },

    fit_window: function(obj){
        var psize = this.document_size();
        obj.style.height = psize[0] + "px";

        //obj.style.width  = psize[1] + "px";
        obj.style.width  = "100%";

        return obj;
    },

    toggle_select: function(){
       var elems = document.getElementsByTagName("select");
       for (i = 0; i < elems.length; i++) {
           if(elems[i].style.visibility != "hidden"){
               elems[i].style.visibility = "hidden";
           }else{
               elems[i].style.visibility = "visible";
           }
       }
    },

    open_window: function(src){
        this.toggle_select();
        this.iframeId = "iboxFrame_" + new Date().getTime();
        var iboxBackground = new Element('div').setProperty('id', 'iboxBackground').inject(document.body);
        this.fit_window(iboxBackground);

        var iboxMain    = new Element('div').setProperty('id', 'iboxMain').inject(document.body);
        var iboxCambas  = new Element('div').setProperty('id', 'iboxCambas').inject(iboxMain);

	//kyo, fix twinkling before opening window in IE
	
	if (document.all && !window.opera)
	    var iboxLoading = new Element('img').setProperties({
                id:'iboxLoading',
                src:'/scripts/slimbox_ex/images/sb_loading.gif',
                style:'vertical-align:middle;'
            }).injectInside(iboxMain);
	else
	var iboxLoading = new Element('img').setProperties({
                id:'iboxLoading',
                src:'/scripts/slimbox_ex/images/sb_loading.gif',
                style:'vertical-align:middle;'
            }).injectInside(iboxCambas);

	
	this.iboxFrame = new Element('iframe').setProperties({
                    src:src,
                    width:'1',
                    height:'1',
                    id: this.iframeId,
                    onload: 'itoBox.resize_frame(this)',
                    frameborder:"0",
                    style: "border: none; padding: 0px; frameBorder: 0; scrolling: no; display: hidden; opacity: 0;"
                }).injectInside(iboxCambas);
	
	if(document.all && !window.opera){
	    iboxCambas.setStyle('display','none');
	    iboxLoading.setStyle('position','absolute');
	    iboxLoading.setStyle('top','100px');
	}
	
	this.iboxFrame.onreadystatechange = function(){
            if(this.readyState == "complete"){
		itoBox.resize_frame(itoBox.iboxFrame);
	    }
        }
    },

    close_window: function(obj){
/*
    var myFx = new Fx.Elements($("iboxMain"), {
        onComplete: function(){
            this.remove(this.iboxFrame);
            this.remove($("iboxMain"));
            this.remove($("iboxBackground"));
        }
    }).start({
        '0': { 'opacity': [1,0] },
        '1': { 'opacity': [0,0] }
    });
*/
      this.iframe_width  = undefined;
      this.iframe_height  = undefined;
      anime.addSequence([{id:this.iframeId, effect:'fadeout', duration:300, onComplete:function(){itoBox.remove_all()}}]);
//        itoBox.remove_all();
    },

    remove_all: function(){
//        this.remove(this.iboxFrame);
	this.remove($("iboxMain"));
        this.remove($("iboxBackground"));
        this.toggle_select();
        return scrollTo(0,this.current_a_y); 
    },

    resize_frame: function(obj){
        if (document.all && !window.opera) {
            //for IE
	    //alert('a');
	    if($("iboxCambas").getStyle('display') && $("iboxCambas").getStyle('display') == 'none'){
            	//kyo if iboxMain is being displayed , let this box disapear. avoid shifting
            	$("iboxCambas").setStyle('display','inline');
            }
	    //$("iboxLoading").setStyle('display','none');
	    //alert($("iboxLoading").getStyle('display')); 
            var w  = obj.contentWindow.document.body.scrollWidth;
            var h  = obj.contentWindow.document.body.scrollHeight;
        } else {
            //for Mozilla, Opera, Safari
            var w = obj.contentWindow.document.documentElement.scrollWidth;
            var h = obj.contentWindow.document.documentElement.scrollHeight;
        }
	//alert(this.iframe_width);
        if(this.iframe_width != undefined){
		//kyo avoid minus
            if((w - this.iframe_width) < 30 && w>=this.iframe_width){
                w = this.iframe_width;
            }else{
		//IE avoid becoming bigger
		if(w+25 > this.iframe_width)
		    w = this.iframe_width;
		else
                   w = w + 25;
            }
        } else {
            w = w + 25;
            //for IE7 fix cambas'size of CandidateProfileRecordEdit
            if (isIE7()) {
                pattern1 = /^.*(CandidateProfileRecordEdit.*|CandidateProfileResumeEdit.*|CandidateProfileHistoryEdit.*|CandidateProfileFreeEdit.*|CandidateProfilePortfolioEdit.*)$/i;
                pattern2 = /^.*(CandidateProfilePublic.*)$/i;
                if(pattern1.test(obj.src) == true && pattern2.test(obj.src) == true){
                    w = w - 25;
                }

            }
        }
        if(w == 25){
            w = 900;
        }
        this.iframe_width  = w;
        if (document.all && !window.opera) {
            //for IE
	    	try{
			//alert(itoBox.iboxFrame.contentWindow.document.body.scrollWidth + '-'+w+'client:'+itoBox.iboxFrame.contentWindow.document.body.clientWidth);
  	    	    	if(w < 300 || w == 345)
			    obj.width=itoBox.iboxFrame.contentWindow.document.body.scrollWidth;
			else
			    obj.width = w;
			
	    	}catch(e){alert(e.name + e.message);}
        } else {
            //for Mozilla, Opera, Safari
	    	if(itoBox.iboxFrame.contentDocument.width < w && (w - itoBox.iboxFrame.contentDocument.width) < 30){
			obj.width = itoBox.iboxFrame.contentDocument.width;
		}
	    	else{
		    if(w < 300 || w == 345)
			//login box  
			obj.width = w - 25;
		    else{
	    	        obj.width = w; 
		    }
		}
        }

        if(this.iframe_height != undefined){
		//kyo avoid minus
            if((h - this.iframe_height) < 30 && h >= this.iframe_height){
                h = this.iframe_height;
            }else{
                h = h + 25;
            }
        } else {
            h = h + 25;
        }
        if(h == 25){
            h = 900;
        }
        this.iframe_height  = h;
        if (document.all && !window.opera) {
            //for IE
	 	    try{
	    		if(obj.contentWindow.document.body.scrollHeight < h && obj.contentWindow.document.body.scrollHeight != 1 && obj.contentWindow.document.body.scrollHeight != 0){
	    		    if(obj.contentWindow.document.body.scrollHeight < 500){
				    	//if this is a login box , do not add 25px
			        	obj.height=obj.contentWindow.document.body.scrollHeight;
			        	//fix cambas'size of CandidateProfileRecordEdit
                        if (isIE7()) {
                        	pattern1 = /^.*(CandidateProfileRecordEdit.*|CandidateProfileResumeEdit.*|CandidateProfileHistoryEdit.*|CandidateProfileFreeEdit.*|CandidateProfilePortfolioEdit.*)$/i;
                            pattern3 = /^.*(CandidateProfileOpenConf.*)$/i;
                            if(pattern1.test(obj.src) == true && pattern3.test(obj.src) == true){
                                obj.height = parseInt(obj.height) + 35;
                                obj.width = obj.width - 25;
                            }
                        }
			        }
		    	    else
		    			obj.height=obj.contentWindow.document.body.scrollHeight+25;
	    		}else
			    obj.height=h+25;
			//alert(obj.height +'-'+h);
	    	}catch(e){obj.height=h-25;alert(obj.height)}
        }else{
            //for Mozilla, Opera, Safari
	     	if(itoBox.iboxFrame.contentDocument.documentElement.offsetHeight < h && ( h - itoBox.iboxFrame.contentDocument.documentElement.offsetHeight) < 30){
		    if( h < 500 )
			//login box
			obj.height= h-25 ;
		    else		    
		        obj.height= h ;
		}else if(itoBox.iboxFrame.contentDocument.documentElement.offsetHeight != 0){
		    //h - itoBox.iboxFrame.contentDocument.documentElement.offsetHeight > 30
		    //kyo use a smaller height to avoid the white edge. +25 to avoid showing scrollbar
		    obj.height = itoBox.iboxFrame.contentDocument.documentElement.offsetHeight + 25;
		}else
		    obj.height = h;
 	 }
	//alert(h + '-' + itoBox.iboxFrame.contentDocument.documentElement.offsetHeight);
        //login after
	pattern = /^.*(CandidateProfileDelete.*|Login|LogDelete.*|MailFormatDelete.*|BookMarkDelete.*|CandidateProfileOpenConf.*|CandidateProfilePublic.*|CandidateImageDelete.*)$/i;
        if(pattern.test(obj.src) == false){
            $("iboxCambas").style.border = "1px solid #AAA";
        }
        //loding img remove
        if($("iboxLoading")){
            anime.addSequence([{id:'iboxLoading', effect:'fadeout', duration:100}]);
            this.remove($("iboxLoading"));
        }
        $("iboxCambas").style.width  = (obj.width) + "px";
    	$("iboxCambas").style.height = (obj.height) + "px";
        return anime.addSequence([{id:this.iframeId, effect:'fadein', duration:300} ]);
    },

    resize: function(h, w, s){
        //call from frame
        if (this.current_frame_h == undefined && this.current_frame_w == undefined){
            try{
                this.current_frame_h = this.iboxFrame.height;
                this.current_frame_w = this.iboxFrame.width;
            }catch(e){}
        }
        var h = (h - 2 == this.current_frame_h) ? this.current_frame_h : h + 2;
        var w = (w - 2 == this.current_frame_w) ? this.current_frame_w : w + 2;
        var s = s ? s : "resize";
        if(s == "resize"){
            this.iboxFrame.height = h;
            this.iboxFrame.width  = w;
            $("iboxCambas").style.height = h + "px";
            $("iboxCambas").style.width  = w + "px";
        }else{
            return false;
        }
        return scrollTo(0,0);
    },

// util
    document_size: function(){
       var xScroll, yScroll;
       if (window.innerHeight && window.scrollMaxY) {
           xScroll = window.innerWidth + window.scrollMaxX;
           yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
           xScroll = document.body.scrollWidth;
           yScroll = document.body.scrollHeight;
       } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
           xScroll = document.body.offsetWidth;
           yScroll = document.body.offsetHeight;
           // support Explorer 6 Strict mode
           if (document.documentElement && document.documentElement.scrollWidth > xScroll) {
               xScroll = document.documentElement.scrollWidth;
               yScroll = document.documentElement.scrollHeight;
           }
       }
       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
           if(document.documentElement.clientWidth){
               windowWidth = document.documentElement.clientWidth; 
           } else {
               windowWidth = self.innerWidth;
           }
           windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
           windowWidth = document.documentElement.clientWidth;
           windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
           windowWidth = document.body.clientWidth;
           windowHeight = document.body.clientHeight;
       }   
       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
           pageHeight = windowHeight;
       } else { 
           pageHeight = yScroll;
       }
       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){  
           pageWidth = xScroll;        
       } else {
           pageWidth = windowWidth;
       }
       arrayPageSize = new Array(pageHeight, pageWidth, windowHeight, windowWidth);
       return arrayPageSize;
    },

	remove: function(dom_obj){
        if(dom_obj == null) return false
		var dom_obj_parent=dom_obj.parentNode;
		dom_obj_parent.removeChild(dom_obj);
	}
};

//init
window.addEvent('domready', itoBox.init);
//window.addEvent('onresize', function(){alert();});

window.onresize = winEvent;

function winEvent(theEvent){
    if(itoBox.current_href){
        itoBox.fit_window($('iboxBackground'));
    }
}

//legacy
function lbOpen(href, obj){
    itoBox.openhref(href, obj);
}

function lbOnloadOpenWithCenterName(href,title,rev,centerName){
    itoBox.openhref(href);
}

function lbOnloadOpen(href,title,rev){
    itoBox.openhref(href);
}

function lbClose(){ //aliases
    itoBox.closebox();
}

function isIE7(){
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=b_version.split(";");
	var trim_Version=version[1].replace(/[ ]/g,"");
	if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE7.0")
		return true;
	else
		return false;
}

