YAHOO.namespace('gaia.app'); 

YAHOO.gaia.app.Collectibles = function() { }

//this function creates the events where onclick, the avatar image is updated
//with the preview
YAHOO.gaia.app.Collectibles.prototype.prepareGallery = function() {
    
    if (!document.getElementsByTagName) { return false; }
    if (!document.getElementById('the_collectibles')) { return false; }
    
    var gallery = document.getElementById('the_collectibles');
    var links = gallery.getElementsByTagName('a');
    
    for (var i=0; i< links.length; i++) {
        YAHOO.util.Event.addListener(links[i], 'click', this.generateNewPreview, this); 
    }
	
    var cancel = document.getElementById("reset_btn");
    YAHOO.util.Event.addListener(cancel, 'click', this.generateNewPreview, this); 
}


//this function swaps the avatar out with a new one with items on it
YAHOO.gaia.app.Collectibles.prototype.generateNewPreview=function(e, scope) {
    YAHOO.util.Event.preventDefault(e);
    
    var source = this.getAttribute("href");	
    var avatarpic= document.getElementById("preview_mc_avatar");
    avatarpic.setAttribute("src", source);
}

//this function prepares the events where onclick, the item variations palette shows up
YAHOO.gaia.app.Collectibles.prototype.prepareVariations = function() {
    
    //get all the variation palette anchors
    var allpalettes = document.getElementsByName('palette_anchor');
    
    //for each of the actual items, add a listenter so that on click,
    //it calls the function to show the palette
    for (var i=0; i< allpalettes.length; i++) {
        YAHOO.util.Event.addListener(allpalettes[i], 'click', this.showItemOptions, this); 
    }
    
    //attaches event for closing the palette div
    var allclosebuttons = document.getElementsByName("palette_close");
    for (var i=0; i< allclosebuttons.length; i++) {
        YAHOO.util.Event.addListener(allclosebuttons[i], 'click', this.hideItemOptions, this); 
    }
}

//this function shows the item options palette when an item with variations is clicked
YAHOO.gaia.app.Collectibles.prototype.showItemOptions=function(e, scope) {
    
    YAHOO.util.Event.preventDefault(e);
    
    //get the id of the div we want to show
    var id = this.getAttribute("id");
    var idparts = id.split("_");
    var palette_name = 'alt_' + idparts[1];
    
    //get all existing palettes and close them all
    var allpalettes = YAHOO.util.Dom.getElementsByClassName("alt_palette");
    for (var i=0; i<allpalettes.length; i++) {
        YAHOO.util.Dom.setStyle(allpalettes[i], "visibility" , "hidden" );
    }

    //var clicky_xy = YAHOO.util.Dom.getXY(id);
    clicky_xy = [];
    clicky_xy[0] = YAHOO.util.Dom.getX(id);
    clicky_xy[1] = 700;
    
    //new Array(700+(idparts[1]*20),700)
    // show the div
    YAHOO.util.Dom.setStyle(palette_name, "visibility" , "visible" );
    // the multiplication part cascades the palettes
    YAHOO.util.Dom.setXY(palette_name, clicky_xy, false );
	
}


//this function hides the item options palette 
YAHOO.gaia.app.Collectibles.prototype.hideItemOptions=function(e, scope) {
    
    YAHOO.util.Event.preventDefault(e);
    
    // get the id of the div we want to hide
    var id = this.getAttribute("id");
    var idparts = id.split("_");
    
    var palette = document.getElementById("alt_"+idparts[2]);
    
    //show the div
    YAHOO.util.Dom.setStyle( palette, "visibility" , "hidden" );	
	
}


function init() {
    var gallery = new YAHOO.gaia.app.Collectibles();
    gallery.prepareGallery();
    gallery.prepareVariations();
}

//something has to call the prepare gallery function when content is ready
YAHOO.util.Event.onContentReady('value_of_mc', init);

