// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// Loader Class

var Loader = Class.create();

Loader.prototype = {

    initialize : function (image) {
        this.image = '/images/loading5.gif';
        this.image_html = "<img name=\"loading_image\" src=\""+this.image+"\" alt=\"Loading...\"/>";
        this.opacity = false;
        this.opacity_level = 2;
        this.init();
    },
    
    init : function () {
        html = '';
        html += '<div id="loading_window" stype="display:none;"></div>';
        html += '<div id="loading_progress" style="display:none; text-align:center;">';
        html += this.image_html;
        html += '<br />Loading ..';
        html += '</div>';
        html += '<div id="loading_wrapper" style="display:none">';
        html += '<div id="loading_content"></div>';
        html += '<div id="loading_footer_wrapper">';
        html += '<div id="loading_footer"></div></div></div>';
        
        docBody = document.getElementsByTagName('body').item(0);
        newDiv = document.createElement('div');
        newDiv.setAttribute("id", "loading");
        newDiv.style.display = 'none';
        newDiv.innerHTML = html;
        docBody.appendChild(newDiv);
    },

    getScrollPos : function() {
        var docElem = document.documentElement;
        this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
        this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
    },

    getPageSize : function() {
        var docElem = document.documentElement
        this.pageWidth = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
        this.pageHeight = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
    },

    getElementSize : function(elem) {
        this.elementWidth = elem.offsetWidth ||  elem.style.pixelWidth;
        this.elementHeight = elem.offsetHeight || elem.style.pixelHeight;
    },

    posToCenter : function(elem) {
        this.getScrollPos();
        this.getPageSize();
        this.getElementSize(elem);
        var x = Math.round(this.pageWidth/2) - (this.elementWidth /2) + this.scrollX;
        var y = Math.round(this.pageHeight/2) - (this.elementHeight /2) + this.scrollY;	
        elem.style.left = x + 'px';
        elem.style.top = y + 'px';	
    },

    setOpacity : function (elemid,value)	{
        $(elemid).style.opacity = value/10;
        $(elemid).style.filter = 'alpha(opacity=' + value*10 + ')';
    },

    loading : function() {
        //for (var i = 10; i <= 40; ++i) {
        //    setTimeout("loader.setOpacity('loading_window', " + i +")", 5);
        //}
        if (this.opacity) {
            $('loading_window').style.opacity = this.opacity_level/10;
            $('loading_window').style.filter = 'alpha(opacity=' + this.opacity_level*10 + ')';
        }

        this.getPageSize();
        this.getScrollPos();
        var ua = navigator.userAgent;
        if (ua.indexOf("MSIE ") != -1) {
            $('loading_window').style.width = this.pageWidth+'px';
        }
        $('loading_window').height = this.pageHeight + this.scrollY + 'px';
        $('loading').style.display = "";
        $('loading_progress').style.display = "";
        this.posToCenter($('loading_progress'));
        return;
    },
    
    loaded : function () {
        if (this.opacity) {
            $('loading_window').style.opacity = 0;
            $('loading_window').style.filter = 'alpha(opacity=0)';
        }
        $('loading').style.display = 'none';
        $('loading_progress').style.display = 'none';
        return;
    }
}

// End Loder Class

// Start PageController Class

var PageController = Class.create();

PageController.prototype = {

    initialize : function () {
        this.hashValie = "";
        this.init();
    },
    
    init : function () {
        this.controller = "";
        this.action = "";
        this.id = "";
        this.params = new Array();
    },
    
    listen : function () {
        if (location.hash != this.hashValue) {
            this.hashValue = location.hash;
            this.hook();
        }
    },
    
    constructRequest : function () {
        params = new Array();
        params = this.hashValue.replace("#", "").split(";");
        for (i = 0; i< params.length; ++i) {
            if (params[i] == '') {
                continue;
            }
            tmp = new Array();
            tmp = params[i].split("\.");
            if (tmp[0] == "controller" && tmp[1] != "") {
                this.controller = tmp[1];
            } else if (tmp[0] == "action" && tmp[1] != "") {
                this.action = tmp[1];
            } else if (tmp[0] == "id" && tmp[1] != "") {
                this.id = tmp[1];
            } else {
                this.params[tmp[0]] = tmp[1];
            }
        }
        if (this.controller == "") {
            this.controller = 'index';
        }
        if (this.action == "") {
            this.action = 'index';
        }
        url = "";
        url = "/" + this.controller + "/" + this.action + ((this.id != '') ? '/' + this.id : '');
        return url;
    },
    
    move : function (url) {
        this.hashValue = location.hash = url;
        return;
    },
    
    hook : function () {
        this.init();
        url = this.constructRequest();
        //alert(request);
        //return;
        new Ajax.Request(url,
            {
                asynchronous:true,
                evalScripts:true,
                method:'get',
                onLoaded:function(request){loader.loaded();},
                onLoading:function(request){loader.loading();}
            }
        );
    }
}


// End Navigator Class


var page_controller = new PageController;

Event.observe(window, 'load', function() { loader = new Loader(); }, false);
//window.setInterval('page_controller.listen()', 500);


