
/**
 * WBoxJS class file.
 *
 * @author Wanderson C. Bragança <wanderson.wbc@gmail.com>
 * @link http://wanderson.wordpress.com
 * @copyright Copyright &copy; 2010, Wanderson C. Bragança
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
 * @version 0.6
 */
 
var wbox = Class.create();	
wbox.prototype = {
    windowWidth : null,
    windowHeight : null,
    ClientFunction : null,
    pageDimensions : {
        width : null,
        height : null
    },
    fileTypes : {
        page : ['asp', 'aspx', 'cgi', 'cfm', 'htm', 'html', 'pl', 'php4', 'php3', 'php', 'php5', 'phtml', 'rhtml', 'shtml', 'txt', 'vbs', 'rb'],
        media : ['aif', 'aiff', 'asf', 'avi', 'divx', 'm1v', 'm2a', 'm2v', 'm3u', 'mid', 'midi', 'mov', 'moov', 'movie', 'mp2', 'mp3', 'mpa', 'mpa', 'mpe', 'mpeg', 'mpg', 'mpg', 'mpga', 'pps', 'qt', 'rm', 'ram', 'swf', 'viv', 'vivo', 'wav'],
        image : ['bmp', 'gif', 'jpg', 'png', 'tiff']
    },
    pagePosition : {
        x : 0,
        y : 0
    },
    myBody:null,
    paramRev:null,
    url:null,
    initialize : function(options) {
        this.paramRev = options.paramRev;
        this.url      = options.url;
        this.myBody = document.getElementsByTagName('body')[0];
        this.browserDimensions();
        this.createWindow();
        this.createEffect();
		
    },
    _getDomain : function(url) {
        var leadSlashes = url.indexOf('//');
        var domainStart = leadSlashes+2;
        var withoutResource = url.substring(domainStart, url.length);
        var nextSlash = withoutResource.indexOf('/');
        var domain = withoutResource.substring(0, nextSlash);
        if (domain.indexOf(':') > -1){
            var portColon = domain.indexOf(':');
            domain = domain.substring(0, portColon);
        }
        return domain;
    },
    fileType : function(url) {
        var image = new RegExp("[^\.]\.("+this.fileTypes.image.join('|')+")\s*$", "i");
        if (image.test(url)) return 'image';
        if (url.indexOf('#') > -1 && (document.domain == this._getDomain(url))) return 'inline';
        if (url.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));
        var type = 'unknown';
        var page = new RegExp("[^\.]\.("+this.fileTypes.page.join('|')+")\s*$", "i");
        var media = new RegExp("[^\.]\.("+this.fileTypes.media.join('|')+")\s*$", "i");
        if (document.domain != this._getDomain(url)) type = 'external';
        if (media.test(url)) type = 'media';
        if (type == 'external' || type == 'media') return type;
        if (page.test(url) || url.substr((url.length-1), url.length) == '/') type = 'page';
        return type;
    },
    getScroll : function(){
        if(typeof(window.pageYOffset) == 'number') {
            this.pagePosition.x = window.pageXOffset;
            this.pagePosition.y = window.pageYOffset;
        } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            this.pagePosition.x = document.body.scrollLeft;
            this.pagePosition.y = document.body.scrollTop;
        } else if(document.documentElement) {
            this.pagePosition.x = document.documentElement.scrollLeft;
            this.pagePosition.y = document.documentElement.scrollTop;
        }
    },
    browserDimensions:function(){
		
        if (self.innerHeight) {
            this.windowWidth = self.innerWidth;
            this.windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            this.windowWidth = document.documentElement.clientWidth;
            this.windowHeight = document.documentElement.clientHeight;
        } else if (document.body) {
            this.windowWidth = document.body.clientWidth;
            this.windowHeight = document.body.clientHeight;
        }
        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;
        }
		
        if(yScroll < this.windowHeight){
            this.pageDimensions.height = this.windowHeight;
        } else {
            this.pageDimensions.height = yScroll;
        }

        if(xScroll < this.windowWidth){
            this.pageDimensions.width = this.windowWidth;
        } else {
            this.pageDimensions.width = xScroll;
        }
		
    },
    createWindow:function(){
	var aStyles = this.paramRev.split(';');
	var windowTitle = '';
	for (var i = 0; i < aStyles.length; i++) {
            if (aStyles[i].indexOf('title:') >= 0) {
                windowTitle = aStyles[i].replace('title:', '');
            }
	}
	
        this.getScroll();
        var x = this.pagePosition.x;
        var y = this.pagePosition.y;
        var wbox = document.createElement('div');
        wbox.setAttribute('id', 'wbox_container');
        this.myBody.appendChild(wbox);
        var content = document.createElement('div');
        content.setAttribute('id', 'wbox_content');
        var skin = '<div class="wbox_close"><div class="wbox_title" id="wbox_title_div" style="display: none;">' + windowTitle + '</div>'+
        '<a href="javascript://" onclick="javascript:WModalBoxClose()"><img src="' + WModalBoxConfig.imageClose + '" width="24" height="24"  /></a><br class="wbox_cl" />'+
        '</div>';
        content.innerHTML = skin;
        this.myBody.appendChild(content);

        var h = parseFloat(((this.windowHeight - $("wbox_content").offsetHeight)/2));
        var w = parseFloat((this.windowWidth - $("wbox_content").offsetWidth)/2);
		
        $("wbox_content").style.top = this.pagePosition.y+h+"px";
        $("wbox_content").style.left = w+"px";
        $("wbox_container").style.height = this.pageDimensions.height+"px";
    },
    createEffect:function(){
        var aStyles = this.paramRev.split(';');
        var w = null;
        var h = null;
        var s = null;
        for (var i = 0; i < aStyles.length; i++) {
            if (aStyles[i].indexOf('width:') >= 0) {
                w = aStyles[i].replace('width:', '');
            }
            else if (aStyles[i].indexOf('height:') >= 0) {
                h = aStyles[i].replace('height:', '');
            }
            else if (aStyles[i].indexOf('scrolling:') >= 0) {
                s = aStyles[i].replace('scrolling:', '');
            }
            else if (aStyles[i].indexOf('ClientFunction:') >= 0) {
                this.ClientFunction = aStyles[i].replace('ClientFunction:', '');
            }
        }
        if(w==null) w = 400;
        if(h==null) h = 320;
        var url = this.url;
        var fileType = this.fileType(url);
	
	
	// center:
	var leftValue = parseInt($("wbox_content").getStyle('left'));
	var topValue = parseInt($("wbox_content").getStyle('top'));
	if (parseInt(topValue - (h / 2)) >= 0) {
		topVal = parseInt(topValue - (h / 2));
	}
	else	
		topVal = 40;
	$('wbox_content').setStyle({ 
					width: w + "px", 
					height: (parseInt(h) + 30) + "px", 
					left: parseInt(leftValue - (w / 2)) + "px",
					top: topVal + "px"
	});
	$('wbox_title_div').show();
	
	Event.observe('wbox_container', 'click', function(event)
	{
		WModalBoxClose();
	});
	
	var myBox = $("wbox_content");
	if(fileType=="page"){
		var div_iframe = document.createElement('div');
		div_iframe.setAttribute('id', 'div_iframe');
		div_iframe.setAttribute('height', h);
		div_iframe.setAttribute('width', w);
		div_iframe.innerHTML = '<iframe id="wbox_iframe" name="wbox_iframe" onload="effectIframe()" scrolling='+s+' src="'+url+'"'+
		'frameborder="0" id="wbox_iframe" style="width:'+w+'px;height:'+h+'px;">'+
		'</iframe>';
		myBox.appendChild(div_iframe);
		$("div_iframe").hide();
				
	}
	else if(fileType=="image"){
		var div_iframe = document.createElement('img');
		div_iframe.setAttribute('id', 'div_iframe');
		div_iframe.setAttribute('height', h);
		div_iframe.setAttribute('width', w);
		div_iframe.setAttribute('src', url);
		myBox.appendChild(div_iframe);
		$("div_iframe").style.height = h;
		$("div_iframe").style.width = w;
		$("div_iframe").hide();
		$("div_iframe").setAttribute('onload', effectIframe());
	}
	else{
		var div_iframe = document.createElement('div');
		div_iframe.setAttribute('id', 'div_iframe');
		div_iframe.setAttribute('height', h);
		div_iframe.setAttribute('width', w);
		div_iframe.innerHTML = '<iframe id="wbox_iframe" name="wbox_iframe" onload="effectIframe()" scrolling='+s+' src="'+url+'"'+
		'frameborder="0" id="wbox_iframe" style="width:'+w+'px;height:'+h+'px;">'+
		'</iframe>';
		myBox.appendChild(div_iframe);
		$("div_iframe").hide();
	}
    }
}
var obj = null;
function WBoxInit(ev) {
    ev = Element.extend(ev);
    var dis = ev.readAttribute('disabled');
    if (dis != null && dis != '')
	return false;
    obj = null;
    Options = new Object();
    Options.paramRev = ev.getAttribute('rev');
    Options.url      = ev.getAttribute('href');
    obj = new wbox(Options);
    return false;
}
function effectIframe(){
    new Effect.Appear('div_iframe',{
        from: 0.0,
        to: 1.0
    });
}
function WModalBoxClose(){
    var myBody = document.getElementsByTagName('body')[0];
    myBody.removeChild($('wbox_container'));
    myBody.removeChild($('wbox_content'));	
}
function time(){}
function setScroll(){
    document.documentElement.scrollTop = obj.pagePosition.y;
    document.documentElement.scrollLeft = obj.pagePosition.x;
}
