YAHOO.namespace ("JP.drop_shadow"); 


/* Change these settings to match your needs */
YAHOO.JP.drop_shadow.auto_settings = {
    color: '#0A4252',
    tags: ['h1'],
    find_class: 'none',
    horizontal_offset: 1,
    vertical_offset: 2
};
/* End of settings */



YAHOO.JP.drop_shadow.auto = function(settings) {
    if (settings['color'] || settings['tags'] || settings['find_class'] || settings['horizontal_offset'] || settings['vertical_offset'] ) {
        auto_settings = settings;
    } else {
        auto_settings = YAHOO.JP.drop_shadow.auto_settings;   
    }
	var shadows = [];
	var items = [];
	var tags = auto_settings['tags'] || ['h1'];
	
	var find_class = auto_settings['find_class'] || 'jp-drop-shadow'; 
	for (tag in tags) {
	    if (find_class == 'none') {
	        // Special case if find_class is "none" then get all the nodes of the 
	        // specified tag.
	        var collection = document.getElementsByTagName(tags[tag]);
	        for (var i = 0; i < collection.length; i++) {
	            items = items.concat(collection[i]);
	        }
        } else {
	        items = items.concat(YAHOO.util.Dom.getElementsByClassName(find_class, tags[tag]));
	    }
	}

	for(var i=0, item; item=items[i]; i++) {
	    shadows[i] = new YAHOO.JP.drop_shadow.Shadow(item, auto_settings);   
    }
};

YAHOO.JP.drop_shadow.Shadow = function(el, settings) {
    if (el) {
        this.init(el, settings); 
    }
};

// Main drop shadow object
YAHOO.JP.drop_shadow.Shadow.prototype = {    
    init: function(el, settings) {
        this.el = el;

        this.color = settings['color'] || '#ccc';
        this.horizontal_offset = settings['horizontal_offset'] || 1;
        this.vertical_offset = settings['vertical_offset'] || 1;
        
        this.create_shadow();
        this.display_shadow(); 
        this.position_shadow();
    },

    create_shadow: function() {
        this.shadow = document.createElement("span");
        this.shadow.innerHTML = this.el.innerHTML;
        YAHOO.util.Dom.setStyle(this.shadow, 'color', this.color);

        YAHOO.util.Dom.setStyle(this.shadow, 'position', 'absolute');
    },


    display_shadow: function() {
        this.el.appendChild(this.shadow);
        //YAHOO.util.Dom.insertAfter(this.shadow, this.el);
    },

    position_shadow: function() {
        var z_index = YAHOO.util.Dom.getStyle(this.el, 'z-index');
        if (!parseInt(z_index)) {
            z_index = 1;
            YAHOO.util.Dom.setStyle(this.el, 'position', 'relative');
            //YAHOO.util.Dom.setStyle(this.el, 'z-index', z_index);
        }
        YAHOO.util.Dom.setStyle(this.shadow, 'z-index', -1);        
        
        var pos = YAHOO.util.Dom.getXY(this.el);

        pos[0] = pos[0] + this.horizontal_offset + parseInt(YAHOO.util.Dom.getStyle(this.el, 'padding-left'));
        pos[1] = pos[1] + this.vertical_offset;
        YAHOO.util.Dom.setXY(this.shadow, pos);
        
        var region = YAHOO.util.Dom.getRegion(this.el);
        YAHOO.util.Dom.setStyle(this.shadow, 'width', (region['right'] - region['left'])+'px');   
        

    }
};


// Having the images get wrapped on page load
YAHOO.util.Event.addListener(window, 'load', YAHOO.JP.drop_shadow.auto);

