YAHOO.namespace ("gaia.app.Marketplace"); 

YAHOO.gaia.app.Marketplace.Itemdetails = function() {

    //hides the reset button
    var reset= document.getElementById("reset_btn");
    if(reset) {
        YAHOO.util.Dom.addClass("reset_btn", "displayNone");
    }

    // get all the variation palette anchors
    var allpalettes = document.getElementById("palette_anchor");
    YAHOO.util.Event.addListener(allpalettes, "click", this.showItemOptions, this);
	
    // attaches event for closing the palette div
    var allclosebuttons = YAHOO.util.Dom.getElementsByClassName("palette_close");
    YAHOO.util.Event.addListener(allclosebuttons, "click", this.hideItemOptions, this);     
	
    // attaches event for opening poses
    var poseanchors = YAHOO.util.Dom.getElementsByClassName("poses_anchor");
    YAHOO.util.Event.addListener( poseanchors, "click", this.showPoses, this);
    
    // attaches event where pose palette is hidden on clicking poseless variation
    var poseless = YAHOO.util.Dom.getElementsByClassName("poseless_item");
    if( poseless){
	    YAHOO.util.Event.addListener( poseless, "click", this.hideAllPalettes, this);    
	}
	
    // hides the iframe
    var iframe = document.getElementsByName("item_preview");
    for(var i=0; i<iframe.length && iframe; i++){
	    YAHOO.util.Dom.setStyle( iframe[i], "display", "none");
	}
		
    // hides the palette
    YAHOO.util.Dom.setStyle('alt_1', 'display', 'none');

    //hides the poses
    var poses = YAHOO.util.Dom.getElementsByClassName("poses_palette");
    for(var i=0; i<poses.length && poses; i++)
    {
    	YAHOO.util.Dom.setStyle( poses[i], 'visibility', 'hidden' );
    }
}

//event on click try-it-on: show items and iframe
YAHOO.gaia.app.Marketplace.Itemdetails.prototype.showItemOptions = function(e, scope) {

    YAHOO.util.Event.preventDefault(e);    
    
    // Close all palettes
    var allpalettes = YAHOO.util.Dom.getElementsByClassName("alt_palette");
    YAHOO.util.Dom.setStyle(allpalettes, 'display', 'none');

    YAHOO.util.Dom.setStyle('alt_1', "display" , "block" );
    
    //shows the iframe
    var iframe = document.getElementsByName("item_preview");
    var reset_link = document.getElementById("reset_link");
    for (var i=0; iframe && i<iframe.length; i++) {
        YAHOO.util.Dom.setStyle( iframe[i], "display", "block");
        iframe[i].src = reset_link; //yay hard codes :P
    }
	
    //hide the try-it-on button 
    YAHOO.util.Dom.addClass(this, "displayNone");
	
    //show the reset button
    YAHOO.util.Dom.replaceClass('reset_btn', "displayNone", "displayBlock");
	
}

//hides all the pose palettes
YAHOO.gaia.app.Marketplace.Itemdetails.prototype.hideAllPalettes=function(theevent, scope) {
	
    //hides the poses
    var poses = YAHOO.util.Dom.getElementsByClassName("poses_palette");
    YAHOO.util.Dom.setStyle(poses, 'visibility', 'hidden' );

}


YAHOO.gaia.app.Marketplace.Itemdetails.prototype.hideItemOptions=function(e, scope) {
    
    YAHOO.util.Event.preventDefault(e);
    
    //get the id of the div we want to hide
    var idparts = this.id.split("_");
    YAHOO.util.Dom.setStyle("poses_"+idparts[2], "visibility" , "hidden" );	
}

//shows the poses 
YAHOO.gaia.app.Marketplace.Itemdetails.prototype.showPoses=function(e, scope) {

    YAHOO.util.Event.preventDefault(e);    
    
    // Close all palettes
    var allpalettes = YAHOO.util.Dom.getElementsByClassName("poses_palette");
    YAHOO.util.Dom.setStyle(allpalettes, 'visibility', 'hidden');

     var parent_container = document.getElementById( "alt_1");
    clicky_xy = [];
    clicky_xy[0] = YAHOO.util.Dom.getX(parent_container) + 40;
    clicky_xy[1] = YAHOO.util.Dom.getY(parent_container)+10;
    
    //show the selected palette
    var idparts = this.id.split("_");    
    var palette_name = 'poses_'+idparts[2];    
    YAHOO.util.Dom.setStyle(palette_name, "visibility" , "visible" );
 
 
 
 
   
    
    YAHOO.util.Dom.setXY(palette_name, clicky_xy);    
    
}
new YAHOO.gaia.app.Marketplace.Itemdetails();
YAHOO.util.Event.onContentReady('item_graphs', function() { new YAHOO.gaia.app.Marketplace.Itemdetails(); });


YAHOO.util.Short(function($){
	var $previewAquariumButton = $('#previewAquariumButton');
	var $previewAquarium;
	
	if($previewAquariumButton.length) {
		
		$previewAquariumButton.bind('click', function(e){
			YAHOO.util.Event.stopEvent(e);
			createAquariumPopup();
			$('#previewAquarium').css('display', 'block');
		});
	};
	
	var createAquariumPopup = function(){
		var html = '<div id="previewAquarium" >'+
			'<iframe height="194" width="200" src="" frameborder="0"></iframe>'+
			'<div style="text-align:center"><img id="closeAquarium" style="margin: 5px;cursor: pointer;"'+
			' src="http://'+GAIA_config('graphics_server')+'/images/store2/btn_popup_cancel.png" /></div></div>';
		
		if ($('#previewAquarium').length == 0) {
			$(html).appendTo(document.getElementById('aquariumContainer'));
			
			$previewAquarium = $('#previewAquarium');
			
			getAquariumInfo($('#previewAquariumButton')[0].title);
			
			$('#closeAquarium').bind('click', function(e){
				$previewAquarium.css('display', 'none');			
			});
		}
		else {
			getAquariumInfo($('#previewAquariumButton')[0].title);
		}
	
	}
	
	var getAquariumInfo = function(_itemid) {
		$.ajax({
			url: '/aquarium/details/'+_itemid,
			success: function(o) {
				o = eval(o);
				var url = ('/aquariumViewer/FishTankViewer.html?location=preview&version='+GAIA_config('cache_value')+'&type='+o.sub_type+'&item_id='+ _itemid + '&graphicsServer=http://'+GAIA_config('graphics_server')+'/images/Gaia_Flash/aquarium/').replace('s.cdn', 's2.cdn');
				$('#previewAquarium').find('iframe').attr('src', url);
			}
		});
				
	
	}

});
