// Get a style property (name) of a specific element (elem)
function getStyle( elem, name ) {
	// If the property exists in style[], then it's been set recently (and is current)
	if (elem.style[name])
		return elem.style[name];

	// Otherwise, try to use IE's method
	else if (elem.currentStyle)
		return elem.currentStyle[name];

	// Or the W3C's method, if it exists
	else if (document.defaultView && document.defaultView.getComputedStyle) {
		// It uses the traditional 'text-align' style of rule writing, instead of textAlign
		name = name.replace(/([A-Z])/g,"-$1");
		name = name.toLowerCase();

		// Get the style object and get the value of the property (if it exists)
		var s = document.defaultView.getComputedStyle(elem,"");
		return s && s.getPropertyValue(name);

	// Otherwise, we're using some other browser
	} else
		return null;
}

// Find the full, possible, width of an element (not the actual,
// current, width)
function fullWidth( elem ) {
    // If the element is being displayed, then offsetWidth
    // should do the trick, barring that, getWidth() will work
    if ( getStyle( elem, 'display' ) != 'none' )
        return elem.offsetWidth || getWidth( elem );

    // Otherwise, we have to deal with an element with a display
    // of none, so we need to reset its CSS properties to get a more
    // accurate reading
    var old = resetCSS( elem, {
        display: '',
        visibility: 'hidden',
        position: 'absolute'
    });

    // Figure out what the full width of the element is, using clientWidth
    // and if that doesn't work, use getWidth
    var w = elem.clientWidth || getWidth( elem );

    // Finally, restore the CSS properties back to what they were
    restoreCSS( elem, old );

    // and return the full width of the element
    return w;
};

function hide( elem ) {
    var curDisplay = getStyle( elem, "display" );

    if ( curDisplay != "none")
        elem.$oldDisplay = curDisplay;

    elem.style.display = "none";
};

function show( elem ) {
    elem.style.display = elem.$oldDisplay || "block";
};


function setOpacity( elem, level ) {

	if ( elem.filters ) {
        //elem.filters.alpha.opacity = level;
		elem.style.filter = 'alpha(opacity='+ level +')';	
	}

    else
        elem.style.opacity = level / 100;		
};


function fadeIn( elem, duration, callback ) {
	
    setOpacity( elem, 0 );

    show( elem );

	cb = callback || function() { };

	var speed = (duration/100);

    for ( var i = 0; i <= 100; i += 5 ) {
		(function(){
            var pos = i; 

            setTimeout(function(){

                setOpacity( elem, pos );

				if(pos == 100)
					cb();

            }, ( pos + 1 ) * speed );
        })();
    }
};

function fadeOut( elem, duration, callback ) {
	
    setOpacity( elem, 100 );

	cb = callback || function() { };

	var speed = (duration/100);

    for ( var i = 0; i <= 100; i += 5 ) {
		(function(){
            var pos = i; 

            setTimeout(function(){

                setOpacity( elem, (100-pos) );
				
				if(pos == 100)
					cb();
				
            }, pos * speed );
        })();
    }
};

// A function used for setting a set of CSS properties, which
// can then be restored back again later
function resetCSS( elem, prop ) {
    var old = {};

    // Go through each of the properties
    for ( var i in prop ) {
        // Remember the old property value
        old[ i ] = elem.style[ i ];

        // And set the new value
        elem.style[ i ] = prop[i];
    }

    // Retun the set of changed values, to be used by restoreCSS
    return old;
};

// A function for restoring the side effects of the resetCSS function
function restoreCSS( elem, prop ) {
    // Reset all the properties back to their original values
    for ( var i in prop )
        elem.style[ i ] = prop[ i ];
};

parseQueryString = function(str) {
	// Build an empty URL structure in which we will store
	// the individual query values by key.
	var objURL = {};

	// Use the String::replace method to iterate over each
	// name-value pair in the query string. Location.search
	// gives us the query string (if it exists).
	str.replace(
		new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
		// For each matched query string pair, add that
		// pair to the URL struct using the pre-equals
		// value as the key.
		function( $0, $1, $2, $3 ){
			objURL[ $1 ] = $3;
		}
	);
	return objURL;
};