/* 
  - Requires Jquery 1.2.1
*/

jQuery(document).ready(function(){
	//hide popup
	jQuery('#expanded_details .popup .close_button').click(function(eventObj){
		jQuery('#popped').remove();
		eventObj.preventDefault();
	});
	jQuery('#expanded_details .popup').addClass('popup_hidden');
	jQuery('#expanded_details .popup').removeClass('popup');
		
	//add click/mouse action
	jQuery('#cupboard_bg area').click(SCE_showPopup);
	jQuery('#cupboard_bg area').mouseover(SCE_showLabel);
	jQuery('#cupboard_bg area').mouseout(SCE_hideLabel);

	//if this has an anchor link..
	if(document.location.hash != ''){
		var thisHash = document.location.hash;
		//jQuery("#cupboard_bg area[href='" + thisHash + "']").trigger('click');
	}

});

function SCE_showLabel(eventObj){
	var theArea = eventObj.target;
	
	//if is itself
	if(jQuery(eventObj.target).hasClass('sce_label')){
		theArea = jQuery("#cupboard_bg area[title='" + theLabel + "']");
	}
	var theLabel = jQuery(theArea).attr('title');
	var coords = jQuery(theArea).attr('coords').split(",");
	
	jQuery(theArea).attr('title', '');	
	jQuery("<p class='sce_label'>" + theLabel + "</p>").appendTo("#cupboard");

	//set pos
	var popupTop = parseInt(coords[3]) + 5;
	var popupLeft = parseInt(coords[0]) -10 ;
	
	if(coords.length > 4){
		//if this isn't a rect shape
		popupLeft = parseInt(coords[2]) ;
	}
	
	if(jQuery(theArea).hasClass('left')){
		popupLeft = popupLeft - 20;
	}

	jQuery('#cupboard .sce_label').css('top', popupTop + 'px');
	jQuery('#cupboard .sce_label').css('left', popupLeft + 'px');
	
}
function SCE_hideLabel(eventObj){
	//hide all current labels
	var theArea = jQuery("#cupboard_bg area[title='']");
	jQuery(theArea).attr('title', jQuery('#cupboard .sce_label').html());	
	jQuery('#cupboard .sce_label').remove();
}


function SCE_showPopup(eventObj){
	var popup_selector = jQuery(eventObj.target).attr('href'); 
	var position_class = jQuery(eventObj.target).attr('class');
	var coords = jQuery(eventObj.target).attr('coords').split(",");
	
	//hide all current popups
	jQuery('#popped').remove();

	//show this popup
	//document.location.hash = popup_selector; 
	var newPopup = jQuery(popup_selector).clone(true);
	jQuery(newPopup).attr('id', 'popped');
	jQuery(newPopup).appendTo("#cupboard")
	jQuery(newPopup).removeClass('popup_hidden');
	jQuery(newPopup).addClass('popup');	
	jQuery(newPopup).addClass(position_class);
	
	//set position
	var popupTop = 0;
	var popupLeft = (parseInt(coords[0]) - parseInt(jQuery(newPopup).css('left'))) + 5;
	
	if(coords.length > 4){
		//if this isn't a rect shape
		popupLeft = (parseInt(coords[2]) - parseInt(jQuery(newPopup).css('left'))) + 5;
	}
	
	if (jQuery(eventObj.target).hasClass('top')){
		popupTop = parseInt(coords[1]) - (parseInt(jQuery(newPopup).height()) + 30);
	}else{
		popupTop = parseInt(coords[3]) + 10;
	}
	
	if (jQuery(eventObj.target).hasClass('left')){
		popupLeft = popupLeft + 5;
	}
	
	jQuery(newPopup).css('top', popupTop + 'px');
	jQuery('#popped .uparrow').css('left', popupLeft + 'px');
	jQuery('#popped .downarrow').css('left', popupLeft + 'px');
	
	eventObj.preventDefault();
	eventObj.stopPropagation();
}