/**
 * Common classes such as $(id, no_cache), Utils, Mouse
 * 
 */

// Это комментарий
var Utils = function() {
    var id_unique = 'utils_';
    var id_count  = 0;
    var MSIE = (navigator.appVersion.indexOf("MSIE") == -1) ? false : true;
    
    this.addEvent = function(elm, evType, fn, useCapture) {
        if (elm.addEventListener) {
            elm.addEventListener(evType, fn, useCapture);
            return true;
        } else if (elm.attachEvent) {
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        } else {
            elm['on' + evType] = fn;
        }
    }
    
    this.addClassName = function(elem, name) {
        var reg = new RegExp('\\b'+name+'\\b');
        if (!elem.className.match(reg)) {
            elem.className += ' '+name;
        }
    }
    
    this.removeClassName = function(elem, name) {
        var reg = new RegExp('\\b'+name+'\\b');
        elem.className = elem.className.replace(reg, ' ');
    }

    this.print = function(elem) {
        var is_print = false;
        if ((arguments.length > 1) && (arguments[1])) {
            is_print = true;
        }
        var is_html = false;
        if ((arguments.length > 2) && (arguments[2])) {
            is_html = true;
        }
        
        if (typeof(elem) == 'object') {
            var text = '';
            var count = 1;
            for (var i in elem) {
                if (is_html) {
                    text += i + ":\t" + elem[i] + '<br />';
                } else {
                    text += i + ":\t" + elem[i] + "\n";
                }
                if ((count > 10) && (!is_print)) {
                    alert(text);
                    text = '';
                    count = 1;
                }
                count++;
            }
            if (is_print) {
                return text;
            } else {
                alert(text);
            }
        } else {
            alert(elem);
        }
    }
    
    this.require_once = function( name ) {
        var d = document;
        if (!d.loaded_js) d.loaded_js = new Array();
        if (d.loaded_js[name] != undefined) return false;
        
        d.loaded_js[name] = true;
        document.body.innerHTML += '<script type="text/javascript" src="/js/'+name+'"><\/script>';
        return false;
    }
    
    this.getCacheValue = function() {
        var time = new Date();
        return time.getTime();
    }
    
    this.create = function( name, attributes ) {
        var el = document.createElement( name );
        if ( typeof attributes == 'object' ) {
            for ( var i in attributes ) {
                el.setAttribute( i, attributes[i] );
                
                if ( i.toLowerCase() == 'class' ) {
                    el.className = attributes[i];  // for IE compatibility
                } else if ( i.toLowerCase() == 'style' ) {
                    el.style.cssText = attributes[i]; // for IE compatibility
                }
            }
        }
        for ( var i = 2; i < arguments.length; i++ ) {
            var val = arguments[i];
            if (typeof(val)=='string') { val = document.createTextNode( val ) };
            //if (el.tagName!='INPUT') el.appendChild( val );
            el.appendChild( val );
        }
        return el;
    }
    
    this.getElementComputedStyle = function(elem, prop) {
        if (typeof elem!="object") elem = document.getElementById(elem);
        
        // external stylesheet for Mozilla, Opera 7+ and Safari 1.3+
        if (document.defaultView && document.defaultView.getComputedStyle) {
            if (prop.match(/[A-Z]/)) prop = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
            return document.defaultView.getComputedStyle(elem, "").getPropertyValue(prop);
        }
        
        // external stylesheet for Explorer and Opera 9
        if (elem.currentStyle) {
            var i;
            while ((i=prop.indexOf("-"))!=-1) prop = prop.substr(0, i) + prop.substr(i+1,1).toUpperCase() + prop.substr(i+2);
            return elem.currentStyle[prop];
        }
        
        return "";
    }
    
    this.in_array = function(arr, needle) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == needle) {
                return true;
            }
        }
        return false;
    }
    
    this.setOpacity = function(elem, value) {
        if (typeof(elem) == 'string') elem = $(elem, true);
        if (MSIE) {
            elem.style.filter = 'alpha(opacity='+value+')';
        } else {
            elem.style.opacity = (value / 100.0);
        }
    }
    
    this.getOpacity = function(elem) {
        if (typeof(elem) == 'string') elem = $(elem, true);
        if ( MSIE ) {
            var match = '';
            if (match = elem.style.filter.match(/alpha\(opacity=([0-9]+)\)/)) {
                return match[1];
            } else {
                return 100;
            }
        } else {
            return (this.getElementComputedStyle( elem, 'opacity' ) * 100);
            //opacity = parseFloat(elem.style.opacity) * 100.0;
        }
    }
    
    this.delChildNodes = function(elem) {
        if (typeof(elem) == 'string') elem = $(elem, true);
        while (elem.childNodes.length > 0) {
            this.delChildNodes(elem.childNodes[0]);
            elem.removeChild( elem.childNodes[0] );
        }
    }
    
    this.getId = function(elem) {
        if (elem.id == '') {
            elem.id = id_unique+'_'+this.getCacheValue()+'_'+id_count;
            id_count++;
        }
        return elem.id;
    }
    
    return this;
}();

var Mouse = new function() {
    this.x = 0;
    this.y = 0;
    this.x_old = 0;
    this.y_old = 0;
    this.x_offset = 0;
    this.y_offset = 0;
    
    Utils.addEvent(document, 'mousemove', function(event){
        if (!event) event = window.event;
        Mouse.x = parseInt(event.clientX + document.documentElement.scrollLeft);
        Mouse.y = parseInt(event.clientY + document.documentElement.scrollTop);
        Mouse.x_offset = parseInt(Mouse.x - Mouse.x_old);
        Mouse.y_offset = parseInt(Mouse.y - Mouse.y_old);
        Mouse.x_old = Mouse.x;
        Mouse.y_old = Mouse.y;
    }, true);
}();
