//global variable to store last clicked thumbnail id
var clickedItemId;

$(document).ready(function(){
	dropdown();
});

function dropdown()
{
	$(".down-list").each(function(){
		$(this).width( $(this).parents(".dropdown-menu").width() + 90);
	});

	$(".auto-width").width("auto");
	
	//for itemView side panel
	$("table.toolsPanelPhotos ul.down-list").each(function(){
		$(this).css( "left", -1 * $(this).width() -10);
		$(this).css( "top", "1px" );
	});
	
/********************** hoverButton ***********************************/
	
	//unbind old listeners from hoverButton
	$( ".dropdown-menu[id=hoverButton]" ).unbind( "mouseenter" ).unbind( "mouseleave" );
	
	//bind new listeners to hoverButton
	$( ".dropdown-menu[id=hoverButton]" ).hover(
	  function () {
			$(".down-list", this).stop();	//if animation is in progress, stop
			$(".down-list", this).height("auto");	//because stop() was called
			$(".menu-first", this).addClass('slide-down');
			$(".down-list", this).slideDown(100);

			$(this).css("z-index", 3);
	  },
	  function () {
	  	$(".down-list", this).stop();	//if animation is in progress, stop
			obj = this;
			$(".down-list", this).slideUp(100, function(){
				$(".menu-first", obj).removeClass('slide-down');
				$(obj).css("z-index", 2);
			});
	  }
	);	

/********************** clickButton ***********************************/
		
	//unbind old listeners from clickButton
	$( ".dropdown-menu[id=clickButton]" ).unbind( "click" ).unbind( "mouseleave" );
	
	//bind new listeners to clickButton
	$( ".dropdown-menu[id=clickButton]" ).click(
	  function () {
	  	if( !$(this).find(".menu-first").hasClass('slide-down') )
			{
				$(".down-list", this).stop();	//if animation is in progress, stop
				$(".down-list", this).height("auto");	//because stop() was called
				$(".menu-first", this).addClass('slide-down');
				$(".down-list", this).slideDown(100);
				$(this).css("z-index", 3);
			}
			else
			{
				$(".down-list", this).stop();	//if animation is in progress, stop
				obj = this;
				$(".down-list", this).slideUp(100, function(){
					$(".menu-first", obj).removeClass('slide-down');
					$(obj).css("z-index", 2);
				});
			}
		}
	);
	
	//bind new listeners to clickButton
	$( ".dropdown-menu[id=clickButton]" ).bind("mouseleave", function () {
		$(".down-list", this).stop();	//if animation is in progress, stop
		obj = this;
		$(".down-list", this).slideUp(100, function(){
			$(".menu-first", obj).removeClass('slide-down');
			$(obj).css("z-index", 2);
		});
  });


/********************** clickTableButton ***********************************/
	//add event to whole TR (all 3 TDs) - for itemPanel owner button Share,..
	
	//unbind old listeners from clickTableButton
	$( ".dropdown-menu[id=clickTableButton]" ).parent().parent().unbind( "click" ).unbind( "mouseleave" );
	
	//bind new listeners to clickTableButton
	$( ".dropdown-menu[id=clickTableButton]" ).parent().parent().click(
	  function () {
	  	if( !$(this).find(".menu-first").hasClass('slide-down') )
			{
				$(".down-list", this).stop();	//if animation is in progress, stop
				$(".down-list", this).height("auto");	//because stop() was called
				$(".menu-first", this).addClass('slide-down');
				$(".down-list", this).slideDown(100);
				$(this).css("z-index", 3);
			}
			else
			{
		  	$(".down-list", this).stop();	//if animation is in progress, stop
				obj = this;
				$(".down-list", this).slideUp(100, function(){
					$(".menu-first", obj).removeClass('slide-down');
					$(obj).css("z-index", 2);
				});
	  	}
	 });
	//bind new listeners to clickTableButton
	$( ".dropdown-menu[id=clickTableButton]" ).parent().parent().bind("mouseleave", function () {
  	$(".down-list", this).stop();	//if animation is in progress, stop
		obj = this;
		$(".down-list", this).slideUp(100, function(){
			$(".menu-first", obj).removeClass('slide-down');
			$(obj).css("z-index", 2);
		});
  });
  
}

/********************** clickIconButton ***********************************/
//for thumbnails and mainPhoto
function thumbnailDropDown()
{ 
	//unbind old listeners from clickIconButton
	$( ".dropdown-menu[id=clickIconButton]" ).unbind( "click" ).unbind( "mouseleave" );
	
	$( ".dropdown-menu[id=clickIconButton]" ).click( function (e) {
  	//if menu missing (e.g. for buyMenu on thumbnails )
  	if( $(this).find(".down-list").length == 0 )
  	{//copy hidden menu
  		if( $(this).attr("name") == "thumbnailToolsMenu" )
  		{
  			 $(this).append( $("#thumbnailToolsMenuContent").html() );
  		}
  	}
		if( !$(this).find(".menu-first").hasClass('slide-down') )
		{	//slide down section
			//store ID of thumbnail to global variable - for albumView, itemView and combinedView
			if( $(this).parents(".thumbnail:first").length > 0 )	//if clicked to thumbnail in albumview
				clickedItemId = $(this).parents(".thumbnail:first").attr("id");
			else 	//if clicked to large image in itemView
			{
				if(typeof(actualItemId) != "undefined")
					clickedItemId = actualItemId;
			}
			if( $(this).attr("name") == "thumbnailToolsMenu" )
  		{
  			//for showing OK mark in Tools menu
  			if( $("#toolsCoverItemId").text() == clickedItemId )
  			{
  				$("#toolsMenu_notCover", this).hide();
  				$("#toolsMenu_isCover", this).show();	
  			}
  			else
  			{
  				$("#toolsMenu_isCover", this).hide();	
  				$("#toolsMenu_notCover", this).show();
  			}
  			
  			if( $("#toolsBioItemId").text() == clickedItemId )
  			{
  				$("#toolsMenu_notPortrait", this).hide();
  				$("#toolsMenu_isPortrait", this).show();	
  			}
  			else
  			{
  				$("#toolsMenu_isPortrait", this).hide();	
  				$("#toolsMenu_notPortrait", this).show();
  			}
  			
  			if( $("#toolsFeaturedItemsId").text().indexOf(";"+ clickedItemId+";") > -1 )
  			{
  				$("#toolsMenu_notFeatured", this).hide();
  				$("#toolsMenu_isFeatured", this).show();	
  			}
  			else
  			{
  				$("#toolsMenu_isFeatured", this).hide();	
  				$("#toolsMenu_notFeatured", this).show();
  			}
  		}
  		//var cssLeft = $(".down-list", this).width() - $(this).width() + 2;
  		var iconLeftOffset = $(this).offset().left - $(this).parent().offset().left;
  		var iconRightOffset = $(this).parent().width() - iconLeftOffset;
  		
  		var cssLeft = $(".down-list", this).width() - iconRightOffset + 2;
  		
  		if( $(this).offset().left > cssLeft )
  			$(".down-list", this).css( "left", -1 * cssLeft );
  		else
  			$(".down-list", this).css( "left", -1 * $(this).offset().left );
  		
  		$(".down-list", this).stop();	//if animation is in progress, stop
			$(".down-list", this).height("auto");	//because stop() was called
			$(".menu-first", this).addClass('slide-down');
			$(".down-list", this).slideDown(100);
			$(this).css("z-index", 3);
		}	
		else
		{ //slide up section
			//for Tools Menu slide up only after click to Icon (not menu item)
			if( $(this).attr("name") == "thumbnailToolsMenu" )
			{
				if( !$(e.target).parent().hasClass("menu-first") )
				{
					return;
				}
			}
			
	 		$(".down-list", this).stop();	//if animation is in progress, stop
			obj = this;
			$(".down-list", this).slideUp(100, function(){
				$(".menu-first", obj).removeClass('slide-down');
				$(obj).css("z-index", 2);
			});
  	}
  });
  
  //bind mouseleave
  $( ".dropdown-menu[id=clickIconButton]" ).bind("mouseleave", function () {
		$(".down-list", this).stop();	//if animation is in progress, stop
		obj = this;
		
		$(".down-list", this).slideUp(100, function(){
			$(".menu-first", obj).removeClass('slide-down');
			$(obj).css("z-index", 2);
		});
  });
}