// 8< ---[feedback.js]---
var feedback={}
feedback.Feedback=Extend.Class({
	name:'feedback.Feedback', parent:undefined,
	properties:{
		// Feedback content
		ui:undefined,
		// Element to provide feedback on
		content:undefined,
		// Used by the window resize event to remember the last prefered ui positions
		element:undefined,
		// Timer used in case of autoHide
		lastPreferedPositions:undefined,
		// The desired with of feedback
		timer:undefined,
		// The area of the content inside the feedback
		width:undefined,
		contentArea:undefined
	},
	initialize:function(){
		var __this__=this
		__this__.makeUI()
		$(__this__.ui).hide()
		$("body").append(__this__.ui)
	},
	methods:{
		makeUI:function(){
			var __this__=this
			var top=html.div({"class":"corner-top"}, html.div({"class":"corner-top-right"}), html.div({"class":"corner-top-left"}), html.div({"class":"corner-top-center"}));
			var bottom=html.div({"class":"corner-bottom"}, html.div({"class":"corner-bottom-right"}), html.div({"class":"corner-bottom-left"}), html.div({"class":"corner-bottom-center"}));
			var close=html.div({"class":"feedback-close"}, "X");
			__this__.content = html.div({"class":"feedback-content"});
			var next_content=html.div({"class":"feedback-nextcontent", "style":"display:none"});
			__this__.ui = html.div({"class":"feedback"}, top, close, __this__.content, bottom);
			__this__.ui._model = __this__;
		},
		bindUI:function(){
			var __this__=this
			$(".feedback-close", __this__.ui).click(function(){
				__this__.immediateHide()
			})
		},
		setContent:function(inputContent){
			var __this__=this
			$(__this__.content).html(inputContent)
			__this__.contentArea = Placement.getArea(inputContent);
		},
		setWidth:function(_width){
			var __this__=this
			__this__.width = _width;
			$(__this__.ui).css("width", __this__.width)
		},
		show:function(inputElement, preferedPositions){
			var __this__=this
			__this__.lastPreferedPositions = preferedPositions;
			__this__.element = inputElement;
			__this__.reposition(preferedPositions)
			if ( $(__this__.ui).is(":hidden") )
			{
				$(__this__.ui).css("z-index", "100")
				$(__this__.ui).show()
			}
		},
		immediateHide:function(){
			var __this__=this
			if ( $(__this__.ui).is(":visible") )
			{
				$(__this__.ui).hide()
				if ( (__this__.timer != undefined) )
				{
					clearTimeout(__this__.timer)
					__this__.timer = undefined;
				}
			}
		},
		hide:function(){
			var __this__=this
			if ( $(__this__.ui).is(":visible") )
			{
				$(__this__.ui).fadeOut()
				if ( (__this__.timer != undefined) )
				{
					clearTimeout(__this__.timer)
					__this__.timer = undefined;
				}
			}
		},
		autoHide:function(){
			var __this__=this
			__this__.startTimer()
			$(__this__.ui).hover(function(){
				__this__.stopTimer()
			}, function(){
				__this__.startTimer()
			})
		},
		startTimer:function(){
			var __this__=this
			__this__.timer = setTimeout(function(){
				__this__.hide()
			}, 1000);
		},
		stopTimer:function(){
			var __this__=this
			clearTimeout(__this__.timer)
		},
		handleWindowResize:function(event){
			var __this__=this
			if ( ((__this__.lastPreferedPositions != undefined) && $(__this__.ui).is(":visible")) )
			{
				__this__.reposition(__this__.lastPreferedPositions)
			}
		},
		reposition:function(preferedPositions){
			var __this__=this
			var new_position=false;
			var position_index=0;
			var element_area=Placement.getArea(__this__.element);
			var ui_area=Placement.getArea(__this__.ui);
			if ( ((__this__.contentArea.w == 0) && (__this__.contentArea.y == 0)) )
			{
				__this__.contentArea = Placement.getArea(__this__.content);
			}
			else if ( true )
			{
				ui_area.h = (__this__.contentArea.h + 38);
			}
			Extend.iterate(preferedPositions, function(prefered_position, index){
				var target_anchor=prefered_position.split("-")[0];
				var element_anchor=prefered_position.split("-")[1];
				var position=Placement.estimateAreaPosition(element_area, target_anchor, ui_area, element_anchor);
				if ( __this__.fullyVisible(position, target_anchor) )
				{
					new_position = position;
					position_index = index;
					return false
				}
			}, __this__)
			if ( (! new_position) )
			{
				var target_anchor=preferedPositions[0].split("-")[0];
				var element_anchor=preferedPositions[0].split("-")[1];
				var new_position=Placement.estimateAreaPosition(element_area, target_anchor, ui_area, element_anchor);
			}
			if ( ((new_position.x + __this__.width) > $(window).width()) )
			{
				new_position.x = (($(window).width() - __this__.width) - 25);
			}
			if ( $(__this__.ui).is(":visible") )
			{
				$(__this__.ui).animate({"left":new_position.x, "top":new_position.y})
				$(__this__.content).animate({"height":(ui_area.h - 38)})
			}
			else if ( true )
			{
				Placement.setPosition(__this__.ui, new_position)
				$(__this__.content).css("height", "auto")
			}
			__this__.addArrow(preferedPositions[position_index])
		},
		fullyVisible:function(position, targetAnchor){
			var __this__=this
			var win=$(window);
			var win_width=(win.width() + win.scrollLeft());
			var win_height=(win.height() + win.scrollTop());
			var ui_area={};
			ui_area.x = position.x;
			ui_area.y = position.y;
			ui_area.w = $(__this__.ui).width();
			ui_area.h = $(__this__.ui).height();
			if ( (((targetAnchor == "E") || (targetAnchor == "SE")) || (targetAnchor == "S")) )
			{
				if ( ((ui_area.x + ui_area.w) > win_width) )
				{
					return false
				}
				if ( ((ui_area.y + ui_area.h) > win_height) )
				{
					return false
				}
			}
			else if ( (((targetAnchor == "W") || (targetAnchor == "NW")) || (targetAnchor == "N")) )
			{
				if ( ((win_width - (ui_area.x + ui_area.w)) < 0) )
				{
					return false
				}
				if ( ((win_height - (ui_area.y + ui_area.h)) < 0) )
				{
					return false
				}
			}
			else if ( (targetAnchor == "NE") )
			{
				if ( ((ui_area.x + ui_area.w) > win_width) )
				{
					return false
				}
				if ( ((ui_area.x + ui_area.h) < win_height) )
				{
					return false
				}
			}
			else if ( (targetAnchor == "SW") )
			{
				if ( ((ui_area.x + ui_area.w) < win_width) )
				{
					return false
				}
				if ( ((ui_area.x + ui_area.h) > win_height) )
				{
					return false
				}
			}
			return true
		},
		addArrow:function(position){
			var __this__=this
			$(__this__.ui).removeClass()
			$(__this__.ui).addClass("feedback")
			position = position.split("-")[0];
			if ( (position == "E") )
			{
				$(__this__.ui).addClass("feedback-east")
			}
			else if ( (position == "W") )
			{
				$(__this__.ui).addClass("feedback-west")
			}
			else if ( (position == "N") )
			{
				$(__this__.ui).addClass("feedback-north")
			}
			else if ( (position == "S") )
			{
				$(__this__.ui).addClass("feedback-south")
			}
			else if ( (position == "NE") )
			{
				$(__this__.ui).addClass("feedback-north-east")
			}
			else if ( (position == "SE") )
			{
				$(__this__.ui).addClass("feedback-south-east")
			}
			else if ( (position == "NW") )
			{
				$(__this__.ui).addClass("feedback-north-west")
			}
			else if ( (position == "SW") )
			{
				$(__this__.ui).addClass("feedback-south-west")
			}
		}
	}
})
feedback.ItemFeedback=Extend.Class({
	name:'feedback.ItemFeedback', parent:feedback.Feedback,
	properties:{
		// source element to show the feedback on
		preferredPositions:["E-W", "W-E"],
		source:undefined
	},
	initialize:function(_source, _content){
		var __this__=this
		__this__.preferredPositions = ["E-W", "W-E"]
		__this__.getSuper(feedback.ItemFeedback.getParent())()
		__this__.source = _source;
		__this__.setContent(_content)
		__this__.setWidth(300)
		__this__.bindUI()
	},
	methods:{
		bindUI:function(){
			var __this__=this
			__this__.getSuper(feedback.ItemFeedback.getParent()).bindUI()
			$(__this__.source).hover(function(){
				__this__.stopDelay()
			}, function(){
				__this__.stopDelay()
				__this__.startDelayHideFeedBack()
			})
			$(__this__.ui).hover(function(){
				__this__.stopDelay()
			}, function(){
				__this__.startDelayHideFeedBack()
			})
		},
		startDelayShowFeedBack:function(){
			var __this__=this
			if ( (__this__.timer == null) )
			{__this__.timer = setTimeout(function(){
				__this__.showPreferredPosition()
				__this__.stopDelay()
			}, 750);}
		},
		startDelayHideFeedBack:function(){
			var __this__=this
			if ( (__this__.timer == null) )
			{__this__.timer = setTimeout(function(){
				__this__.hide()
				__this__.stopDelay()
			}, 750);}
		},
		stopDelay:function(){
			var __this__=this
			clearTimeout(__this__.timer)
			__this__.timer = null;
		},
		showPreferredPosition:function(_source){
			var __this__=this
			if ( (_source == null) )
			{
				__this__.show(__this__.source, __this__.preferredPositions)
			}
			else if ( true )
			{
				__this__.show(_source, __this__.preferredPositions)
			}
		},
		show:function(inputElement, preferedPositions){
			var __this__=this
			__this__.stopDelay()
			__this__.getSuper(feedback.ItemFeedback.getParent()).show(inputElement, preferedPositions)
		}
	}
})
feedback.ItemMenuFeedback=Extend.Class({
	name:'feedback.ItemMenuFeedback', parent:feedback.ItemFeedback,
	properties:{
		preferredPositions:undefined
	},
	initialize:function(_source, _content){
		var __this__=this
		__this__.getSuper(feedback.ItemMenuFeedback.getParent())(_source, _content)
		__this__.setWidth(150)
		__this__.preferredPositions = ["E-W", "W-E"];
	}
})
feedback.SimpleFeedback=Extend.Class({
	name:'feedback.SimpleFeedback', parent:feedback.ItemFeedback,
	properties:{
		preferredPositions:undefined
	},
	initialize:function(_source, _content){
		var __this__=this
		__this__.getSuper(feedback.SimpleFeedback.getParent())(_source, _content)
		__this__.setWidth(500)
		__this__.preferredPositions = ["S-N"];
	},
	methods:{
		bindUI:function(){
			var __this__=this
		}
	}
})
feedback.init=	function(){
		var __this__=feedback;
	}
feedback.init()
