Event.addBehavior({

	/* BIN - AJAXIFY */
	'#gal-bin' : function(e){if(!this.hasClassName("bin_ajax")){this.addClassName('bin_ajax');}},

	/* BIN - TOGGLE:ON */
	'#gal-bin a.bin_orgbin,#gal-bin a.bin_orgalbum' : function(e)
	{
		this.observe('click', function(event)
		{
			Event.stop(event);
			var url=window.location.protocol +"//"+ window.location.hostname + window.location.pathname+"?show_bin=1&content_type=xml";
			new Ajax.Request(url,{ method:'post',onCreate:function(){ajaxCursorStart();},onComplete:function(){ajaxCursorEnd();},onSuccess:function(transport){refreshById(transport.responseXML,"div","gal-bin");}});
		},false);
	},

	/* BIN - TOGGLE:OFF */
	'#gal-bin a.close' : function(e)
	{
		this.observe('click', function(event)
		{
			Event.stop(event);
			var url=window.location.protocol +"//"+ window.location.hostname + window.location.pathname+"?content_type=xml";
			new Ajax.Request(url,{method:'post',onCreate:function(){ajaxCursorStart();},onComplete:function(){ajaxCursorEnd();},onSuccess:function(transport){refreshById(transport.responseXML,"div","gal-bin");}});
		},false);
	},
	
  /* BIN - PAGING */
	'#gal-bin ul.navigation li a' : function(e)
	{
		if(this.readAttribute("href"))
		{
			this.observe('click', function(event)
			{
				Event.stop(event);
				var url=window.location.href;
				/* IE6 DOESNT GIVE YOU THE CONTENT OF THE HREF BUT WHAT IT INTERPRETES THE LINK TO BE... SO IT COMPLETES THE URL AND ADDS PROTOCOL, DOMAIN AND PATH BY ITSELF */
				if(url.indexOf("http://")>-1){url=this.readAttribute("href") + "&content_type=xml";}else{url+=this.readAttribute("href") + "&content_type=xml";}
				new Ajax.Request(url,{method:'post',onCreate:function(){ajaxCursorStart();},onComplete:function(){ajaxCursorEnd();},onSuccess:function(transport){refreshById(transport.responseXML,"div","gal-bin");}});
			},false);
		}
	},
	
	/* BIN - SUBMIT FORM */
	'#bin_form' : function(e)
	{
		this.submit = function()
		{
			$("media_actions").setStyle({display:'none'});
			actionValue = "";
			for (var i=0; i<$("bin_form").action.length; i++){if($("bin_form").action[i].checked){actionValue=$("bin_form").action[i].getValue();}}
			var url=window.location.href;
			if(url.indexOf("?")>-1){url+="&content_type=xml&show_bin=1";}else{url+="?content_type=xml&show_bin=1";}
			url+=this.firstChild.className.replace(/page/,"&page=");
			var pars=this.serialize();
			new Ajax.Request(url,
			{
				method:'post',
				postBody:pars,
				onCreate:function(){ajaxCursorStart();},
		    onComplete:function(){ajaxCursorEnd();},
				onSuccess:function(transport)
		    {
					if(actionValue=="media_bin_delete_confirm"){refreshById(transport.responseXML,"div","gal-bin");}
					if($("albums")){refreshById(transport.responseXML,"ul","albums");}
					if(top.$$(".myprofile")[0]){refreshById(transport.responseXML,"div",top.$$(".myprofile")[0].readAttribute("id"));}
					if($("album_content")){refreshById(transport.responseXML,"div",$$("div.album")[0].readAttribute("id"));}
				}
			});
			this.reset();
		}
	},
	
	/* BIN - DROPDOWN - SHOW/HIDE */
	'#bin_form div.edit input' : function(e)
	{
		this.observe('blur', function(event){setTimeout('$("media_actions").setStyle({display:"none"});', 200);});
		this.observe('focus', function(event)
			{
				/* Uncheck all other boxes */
				var binBoxes = $$('div.edit input');for(i=0;i<binBoxes.length;i++){binBoxes[i].checked=false;}
				if(Prototype.Browser.IE){this.checked=true;}
				/* Repositions and shows the "media_actions" box */
				var newC = Position.cumulativeOffset(this).toString();
				var offC = Position.cumulativeOffset($("gal-bin-body")).toString();
				var newTop = newC.substring(newC.indexOf(',')+1,newC.length)-offC.substring(offC.indexOf(',')+1,offC.length)-2;
				var newLeft = newC.substring(0,newC.indexOf(','))-offC.substring(0,offC.indexOf(','))-21;
				$("media_actions").setStyle({display:'block',top:newTop+'px',left:newLeft+'px',zindex:'900'});
			}
		);
	},
	
	/* BIN - RADIO BUTTON LABEL - IE FIX */
	'#media_actions fieldset div label' : function(e){if(Prototype.Browser.IE){this.observe('click', function(event){$(this.readAttribute("for")).checked=true;$("bin_form").submit();});}},
	
	/* BIN - DROPDOWN - AUTO-SUBMIT ON SELECT */
	'#media_actions fieldset div input' : function(e){this.observe('change', function(event){$("bin_form").submit();});},
	
	/* BIN - THUMBNAILS - POPUP PREVIEWS */
	'#bin_form .options' : function(e)
	{
		this.observe('click', function(event){this.focus();});
		this.observe('focus', function(event)
		{
			var miniviewHTML = new Element('div',{id:'bin-preview'});
			var miniviewIMG = new Element('img',{src:this.firstChild.readAttribute('src').replace('thumb3','thumb1'),alt:'preview',style:'width:15em;height:12em;border:1px solid;'});
			miniviewHTML.appendChild(miniviewIMG);
			if(!Prototype.Browser.IE){new Insertion.After(this, serializeNode(miniviewHTML));}
		});
		this.observe('blur', function(event){$("bin-preview").remove();});
	}
});





