/**
 * 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) {
                if(evType == 'mousewheel') {
                    elm.addEventListener('DOMMouseScroll', fn, useCapture ? useCapture : false);
                }
	        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.isClassName = function(elem, name) {
        var reg = new RegExp('\\b'+name+'\\b');
        return (elem.className.match(reg)) ? true : false;
    }
	
	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) {
	    if (typeof(elem) == 'object') {
            var text = '';
            var count = 1;
            for (var i in elem) {
                text += i + ":\t" + elem[i] + "\n";
                if (count > 10) {
                    alert(text);
                    text = '';
                    count = 1;
                }
                count++;
            }
            alert(text);
        } else {
            alert(elem);
        }
	}
	
	this.require_once = function( name ) {
	    var d = document, h = d.getElementsByTagName('HEAD')[0];
	    if (!d.loaded_js) d.loaded_js = new Object();
	    if (d.loaded_js[name] != null) return false;
	    d.loaded_js[name] = true;
        
        if (!window.opera) {
            var s = d.createElement("SCRIPT");
            var _4a = function(){
                s.language = "JavaScript";
                if (s.setAttribute) {
                    s.setAttribute("src", name);
                } else {
                    s.src = name;
                }
                h.insertBefore(s, h.lastChild);
            };
            window.setTimeout(_4a, 10);
        } else {
            var s = d.createElement("SPAN");
            s.style.display = "none";
            h.insertBefore(s, h.lastChild);
            s.innerHTML = "Workaround for IE.<s" + "cript></" + "script>";
            var _4a = function(){
                s = s.getElementsByTagName("SCRIPT")[0];
                s.language = "JavaScript";
                if (s.setAttribute) {
                    s.setAttribute("src", name);
                } else {
                    s.src = name;
                }
            };
            window.setTimeout(_4a, 10);
        }
	    return false;
	}
	
	this.getCacheValue = function() {
	    var time = new Date();
	    return time.getTime();
	}
    
    this.create = function( name, attributes ) {
        var el = null;
        if (MSIE && (name.toLowerCase() == 'input')) {
            var n = '';
            if ( typeof attributes == 'object' ) {
                for ( var i in attributes ) {
                    if (i.toLowerCase() == 'name') {
                        n = attributes[i];
                    }
                }
            }
            if (n == '') {
                el = document.createElement( name );
            } else {
                el = document.createElement( '<input name="'+n+'">' );
            }
        } else {
            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 = document.getElementById(elem);
        if (MSIE) {
            elem.style.filter = 'alpha(opacity='+value+')';
        } else {
            elem.style.opacity = (value / 100.0);
        }
    }
    
    this.getOpacity = function(elem) {
        if (typeof(elem) == 'string') elem = document.getElementById(elem);
        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 = document.getElementById(elem);
        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;
    }
    
    this.IsCompatibleBrowser = function() {
        var sAgent = navigator.userAgent.toLowerCase() ;
        
        // Internet Explorer 5.5+
        if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
        {
            var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
            return ( sBrowserVersion >= 5.5 ) ;
        }
    
        // Gecko (Opera 9 tries to behave like Gecko at this point).
        if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
            return true ;
    
        // Opera 9.50+
        if ( window.opera && window.opera.version ) {
            var v = parseFloat(window.opera.version());
            //if ((v >= 9.5) && (v < 10)) return true ;
            return true;
        }
        
        // Adobe AIR
        // Checked before Safari because AIR have the WebKit rich text editor
        // features from Safari 3.0.4, but the version reported is 420.
        if ( sAgent.indexOf( ' adobeair/' ) != -1 )
            return ( sAgent.match( / adobeair\/(\d+)/ )[1] >= 1 ) ; // Build must be at least v1
    
        // Safari 3+
        if ( sAgent.indexOf( ' applewebkit/' ) != -1 )
            return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 ) ;    // Build must be at least 522 (v3)
    
        return false ;
    }
    
	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
	);
} ();