$(document).ready(function(){
/*	//$('.calendar_panels').e4eColumnsFE();
	// get main bar height
	var dateListHeight = $('#calendar_event_list_holder').outerHeight();
	// get panel height
	var monthHeight = $('#calendar_month_block').outerHeight();
	if($('#calendar_month_sidebar').outerHeight() <dateListHeight){
		// set height
		$('#calendar_month_summary').height(dateListHeight - monthHeight - 29);
	}else{
		// set height
		$('#calendar_event_list_holder').height($('#calendar_month_sidebar').height());
	}
	
*/	
	if ($("#admin_ribbon").size() === 0) {
	 $('.calendar_eventList a').loadCal();
  }
});

$.widget("ui.loadCal", {
	_init: function() {
		var self = this; 
		this.element.click(function(evt){
			evt.preventDefault(); 
			self.popOut(evt);
		}); 
	},
	_version: "0.1",
	_isOpen: false,
	popOut: function(evt) {
		if (this._isOpen) {
			this.show(evt);
		}else{
			this._isOpen = true;
			this.popup = this.getHTML(evt);
			$(document.body)
				.append(this.getOverlay())
				.append(this.popup);
			this.position(evt);
		}
	},
	isOpen: function() {
		return this._isOpen;
	},
	version: function(){
		return this._version;
	},
	getHTML: function(evt){
		var linkAdd = (this.element.attr('href')).split('#');
		linkAdd = linkAdd[0]+' #calendar_event_list';
		var theHTML = $('<div />')
			.addClass('calendar-loadPop-holder')
			.css('z-index',1001)
			.append(
				$('<div/>')
					.addClass('calendar-loadPop')
					.load(linkAdd)
			);
		return theHTML;
	},
	getOverlay: function(){
		var self = this;
		$(document)
			.bind('keydown.dialog-overlay', function(event) {
				self.hide();
			});
		this.overlay = ($('<div/>')
			.appendTo(document.body)
			.css({
				position:'absolute',
				height:self.height,
				width:self.width,
				top:0,
				left:0,
				zIndex:1000
			})
			.click(function(){
				self.hide();
			})
		)
	},
	height: function() {
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollHeight = Math.max(
				document.documentElement.scrollHeight,
				document.body.scrollHeight
			);
			var offsetHeight = Math.max(
				document.documentElement.offsetHeight,
				document.body.offsetHeight
			);

			if (scrollHeight < offsetHeight) {
				return $(window).height() + 'px';
			} else {
				return scrollHeight + 'px';
			}
		// handle "good" browsers
		} else {
			return $(document).height() + 'px';
		}
	},
	width: function() {
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollWidth = Math.max(
				document.documentElement.scrollWidth,
				document.body.scrollWidth
			);
			var offsetWidth = Math.max(
				document.documentElement.offsetWidth,
				document.body.offsetWidth
			);

			if (scrollWidth < offsetWidth) {
				return $(window).width() + 'px';
			} else {
				return scrollWidth + 'px';
			}
		// handle "good" browsers
		} else {
			return $(document).width() + 'px';
		}
	},
	hide: function() {
		this.overlay.hide();
		this.popup.hide();
	},
	show: function(evt) {
		this.position(evt);
		this.overlay.show();
		this.popup.show();
	},
	position: function(evt) {
		var posTop = this.element.offset().top - this.popup.outerHeight() - 15,
			posLeft = this.element.offset().left + 20;
		this.popup
			.css({
				top:posTop,
				left:posLeft
			})
	}
});
$.extend($.ui.loadCal, {
	getter: ['isOpen','version']
});
