
/*

------------------------------
jquery.BlankSlate.UIWrapper.js
Last Modified: 12/08/08
------------------------------

*/

//Make an element draggable

function makeDraggable(droppedElem, container) {
	$(droppedElem).draggable("destroy");
	var snapTo = '';
	if(container =='#container') {
		snapTo = '.dragElement';
		container="#blankslate"
	}
	if($(droppedElem).hasClass("designElement")){
		snapTo = false;
	}
	var droppedElemObject = $(droppedElem).find(".elementObject");
	$(droppedElem).draggable({
		grid: [1, 1],
  		opacity: .3,
  		containment: container,
  		snap: snapTo,
		helper: 'original',
		start: function(e, ui) {
			if ($(this).is('.ui-selected')) {
				initPos = {x: parseInt(this.style.left,10),y:parseInt(this.style.top,10)};
				collection = jQuery('.ui-selected:visible').not(this);
				if (collection.size() == 0) {
					initPos = false;
					collection = false;
				}
			}
		},
		drag: function(e, ui) {
			if(collection) {
				var x = ui.position.left;
         		var y = ui.position.top;
				collection.each(function() {
					if (parseInt(this.style.left,10)>=0) {
						this.style.left = parseInt(this.style.left,10)+ x - initPos.x + 'px';
					}
					if (parseInt(this.style.top,10)>=0) {
						this.style.top = parseInt(this.style.top,10)+y - initPos.y + 'px';
					}					
				});
				initPos = {x:x, y:y};

			}
		},
		stop : function(e,ui) {
			initPos = false;
			collection = false;
			var offSet = $(this).find(".dragHandle").offset();
			
			setDirty();
		}
		//snap: '.snaplinehorizontal'
	}).css("position", "absolute");
	// Hide Right Menu Element
	$(droppedElem).find(".menuElement").css("display", "none"); 
	// Display pageElement
	$(droppedElem).find(".pageElement").css("display", "block");
	// Remove floating right menu background now that we have dropped it
	$(droppedElem).removeClass("RightFloatingMenu_button"); 
	// Make dropped object selectable
	$(droppedElem).addClass("selectable");
	// For Debugging: if BlockContextMenu is 1 (true), then do notcreate the contect menu
	if (!Debug.BlockContextMenu) {
	
		$(droppedElem).contextMenu("elementMenu", {
			onContextMenu: function(e) {
				var hideMenu = function()
				{
	            	$('#jqContextMenu')
	            	.hide()
	            	.next('div') // shadow
	            	.hide();
				}
				
				var unbindMenu = function()
				{
	            	$(document)
	            	.unbind('keyup')
	            	.unbind('mousedown');
				}
				
				// escape key or clicking elsewhere will hide the menu and shadow
			    $(document)
			    .bind('keyup', function(key){
		            if ( 27 == key.keyCode)
		            {
		            	hideMenu();
						unbindMenu();
		            }
				})
				.bind('mousedown', function(click){
					if ( !$(click.target).parents('#jqContextMenu').length > 0 )
					{
						hideMenu();
					}
	
	            	unbindMenu();
				});
				
				return true;
			},
			bindings: {	
				'deleteElem': function(t) {
					$(t).remove();
				},
				'alignElemLabel' : function(t) {
					alignSelection(t, "toLabel");
				},
        		'alignElemLeft' : function(t) {
       				alignSelection(t, "left");
        		},
        		'alignElemRight' : function(t) {
       	 			alignSelection(t, "right");
        		},
        		'alignElemTop' : function(t) {
       				alignSelection(t, "top");
        		},
        		'sizeElem'  : function(t) {
        			sizeSelection(t, "width");
        		},
        		'editProps' : function(t) {
       				editProperties(t);
				},
        		'move1' : function(t) {
       				StepForward(t);
				},
        		'move2' : function(t) {
       				StepBackward(t);
				},
        		'move3' : function(t) {
       				MoveToFront(t);
				},
        		'move4' : function(t) {
       				MoveToBack(t);
				}
			}
		});
	}
	// Allow labels to be edited in place
	$(droppedElem).find(".pageElementLabel").BlankSlateEditInPlace({
		OnInputShow : function () {
			ResizeDraggable($(droppedElem));
		},
		CallbackHandler: function () { 
			ResizeDraggable($(droppedElem));
			genXML(droppedElemObject,$(droppedElemObject).attr("name"),$(droppedElemObject).attr("id"),$(droppedElem).find(".pageElementLabel").text(),$(droppedElem).width(),$(droppedElem).height(),"","","","","","","","","","","","");
		}
	});
	ResizeDraggable($(droppedElem));
}

// Make a dropped draggable element also resizable
function MakeResizable(droppedElem, handles) {
	var MinX = 20;
	var MinY = 50;
	if ($(droppedElem).attr("id") == "sectionMenu") {
		MinX = 50;
		MinY = 50;
	}
	
	if (undefined == handles)
	{
		var handles = 'e';
	}

	var sizeHeight = false;
	var doResize = true;
	
	// don't resize internal elements for design menu -> add text
	if ( $(droppedElem).hasClass("designElement") ) {
		
		if ( $(droppedElem).hasClass("addText") )
		{
			doResize = false;
			handles = 'e,s,se';
		}
		
		if ( $(droppedElem).hasClass("addImage") )
		{
			doResize = false;
			handles = 'e,s,se';
		}

		if ( $(droppedElem).hasClass("addRSS") )
		{
//			doResize = false;
//			handles = 'e,s,se';
			
			$(droppedElem).resizable({
				containment: '#blankslate',
				resize: function(){
					$(droppedElem).bsaf_rssItemsHeight();
			        setDirty();
				}
			});
			
			return;
		}
		
		if ( $(droppedElem).hasClass("addBlock") )
		{
			doResize = false;
			
			$(droppedElem).resizable({
				containment: '#blankslate',
				minWidth: 1,
				minHeight: 1,
				resize: function(){
					$('.pageElement', droppedElem).css('height', '100%');
			        setDirty();
				}
			});
			
			return;
		}
	}
	
	if ($(droppedElem).attr("id") == "textAreaMenu") {
		handles = 'e,s';
		sizeHeight = true;	
	}
	if ($(droppedElem).attr("id") == "sectionMenu") {
		handles = 'n,e,s,w';
		sizeHeight = true;
		doResize = false;
	}

	var droppedElemObject = $(droppedElem).find(".elementObject");
	$(droppedElem).resizable({
		containment: '#blankslate',
		handles : handles,
		ghost: true,
		autohide: true,
		minHeight: MinX,
		minWidth: MinY,
		stop: function(ev, ui) {
		  if(doResize){
			LabelWidth = $(this).find(".pageElementLabel").width();
			$(droppedElemObject).css({"width":$(this).width() - LabelWidth - 20 + "px"});	
			$(this).find(".pageElement").css({"width":$(this).width() - 10 + "px"});
			$(this).find(".pageElement").css({"height":$(this).height() + "px"});
			if (sizeHeight) {
				$(droppedElemObject).css({"height": $(this).height() - 10 + "px"});
			}
			ResizeDraggable($(droppedElem));
		  }
		  
			if ( $(droppedElem).hasClass("designElement") && $(droppedElem).hasClass("addText") ) {
				$(droppedElem)
				.find('iframe:first')
				.css('height', $(droppedElem).height() -10 );
				
			}
			setDirty();
		}
	});
}

// Resize dropped elementObject
function ResizeDraggable(elm) {
	if ($(elm).find("#addBreak").length > 0) {
		var ObjectWidth = $(elm).width();
		$(elm).css("height", "15px");
		$(elm).find("#addBreak").css("width", ObjectWidth - 5);
		$(elm).find("#addBreak").css("height", "15px");
		$(elm).find("hr").css("width", ObjectWidth);
		return true;
	}
	
	var DraggableID = $(elm).attr("id");
	var LabelWidth = $(elm).find(".pageElementLabel").width();
	var ObjectWidth = $(elm).find(".pageElementObject").width();
	var ObjectHeight = $(elm).find(".pageElementObject").height();
	var PageElementWidth = $(elm).find(".pageElement").width();
	var ElementWidth = $(elm).width();
	if (LabelWidth!=null && LabelWidth !="null" && ObjectWidth != null && ObjectWidth != "null" && ObjectHeight != null && ObjectHeight != "null"){
		var NewWidth = LabelWidth + ObjectWidth;
		$(elm).find(".pageElement").css("width", NewWidth + 10 + "px");
		$(elm).css("width", NewWidth + 10 + "px");
		$(elm).css("height", ObjectHeight + "px");
	}
}

/*
//Resize dropped elementObject
function ResizeDraggable(elm) {
	var letterWidth = 7;
	var letterCount = $(elm).find(".pageElementLabel").text().length;
	var newLabelWidth = (letterWidth * letterCount);
	$(elm).find(".pageElementLabel").css("width", newLabelWidth + "px");
	//$(elm).find(".pageElementLabel").css("border", "red");
	var DraggableID = $(elm).attr("id");
	if (DraggableID == "buttonMenu") {
		nWidth = $(elm).find("input").width() + 10;
		$(elm).css("width", nWidth);
		$(elm).find(".pageElement").css("width", nWidth);
		$(elm).find(".pageElementObject").css("width", nWidth);
		return true;
	}
	var LabelWidth = $(elm).find(".pageElementLabel").width();
	var ObjectWidth = $(elm).find(".pageElementObject").width();
	var ObjectHeight = $(elm).find(".pageElementObject").height();
	var PageElementWidth = $(elm).find(".pageElement").width();
	var ElementWidth = $(elm).width();

	if (LabelWidth!=null && LabelWidth !="null" && ObjectWidth != null && ObjectWidth != "null" && ObjectHeight != null && ObjectHeight != "null"){
		var NewWidth = LabelWidth + ObjectWidth;

		$(elm).find(".pageElement").css("width", NewWidth + 10 + "px");
		$(elm).css("width", NewWidth + 20 + "px");
		$(elm).css("height", ObjectHeight + "px");
	}

}
*/

function DesignMenuBehaviors(elem)
{
	if ( $(elem).hasClass('addText') )
	{
		$('.pageElement', $(elem))
		.dblclick(function(){
			$(this).makeRTE();
		});
	}
	
	if ( $(elem).hasClass('addImage') )
	{
		$(elem).resizable('destroy')
		.resizable({
			containment: '#blankslate',
			aspectRatio: true,
			start: function(){
				$('#blankslate').bind('mouseleave', function(){
					$('.ui-resizable-handle', elem).mouseup();
				});
			}
		})
		.draggable('option', 'containment', '#blankslate');
		
		$('.pageElement', $(elem))
		.bind(
			'showPropsDialog',
			function(){
				$(this).RSSPropsDialog();
			}
		);
	}
	
	if ( $(elem).hasClass('addRSS') )
	{
		$(elem).bsaf_rssSetup();
		
		$('.pageElement', $(elem))
		.bind(
			'showPropsDialog',
			function(){
				$(this).RSSPropsDialog();
			}
		);
	}
}

function inputMenuBehaviors(elem)
{
	if ( $(elem).hasClass('addButton') )
	{
		$('.pageElement', $(elem))
		.bind(
			'showPropsDialog', 
			function(){
				$(this).buttonPropsDialog();
			}
		);

	}
}
