function getCal(div_id, calendar_id, size, type, month, year, event_types) {
	if (event_types == null || event_types == 'undefined') event_types = 'null';
	
	var cal_scroller = getEle('cal_scroll');
	if (cal_scroller) {
		document.getElementById('cal_scroll').getCalendar(month, year);
	}
	var url = '/app/modules/cal/xml/calendar.php';

	var myRequest = new ajaxObject(url);
	myRequest.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {
			
			var calendarDiv = document.getElementById(div_id);
			calendarDiv.innerHTML = responseText;
			
			var scripts = calendarDiv.getElementsByTagName("script");
			for (var i=0; i<scripts.length; i++) { eval(scripts[i].text); }
			
			//setTips();
			if(size=='small'){
				setTips();
			} else {
				updateSize();
				setupFancyBox();
			}
		}
	}
	var qs = 'size='+size+'&type='+type+'&m='+month+'&y='+year+'&calid='+calendar_id+'&event_types='+event_types;
	myRequest.update(qs);
}

(function($){
	$.fn.wrapChildren = function(options) {
	
		var options = $.extend({
			childElem : undefined,
			sets : 1,
			wrapper : 'div'
			}, options || {}
		);
		
		if (options.childElem === undefined) return this;
		
		return this.each(function() {
			var elems = $(this).children(options.childElem);
			var arr = [];
			
			elems.each(function(i,value) {
				arr.push(value);
				if (((i + 1) % options.sets === 0) || (i === elems.length -1)){
					var set = $(arr);
					arr = [];
					set.wrapAll($("<" + options.wrapper + ">"));
				}
			});
		});
	}
})(jQuery);

function updateSize(){
	jQuery('.calendar-grid')
	.wrapChildren({
		childElem:'.cal-weeks:not(eq(0))',
		sets:7,
		wrapper:'div class="cal-weeks-cont"'
	});

	
	var offset = 0;

	jQuery(".cal-weeks-cont").each(function(){
		var max_height = 40;
		
		//get the max height
		jQuery(this).find('.cal-weeks').each(function(){			
			//add the new height
			if(jQuery(this).outerHeight(true) > max_height){
				max_height = jQuery(this).outerHeight(true);
			}
		});
		
		//set the new height
		jQuery(this).find('.cal-weeks').height(max_height+offset);
	});
	
	//show the calendar
	jQuery('.calendar-grid').css({ 'visibility':'visible' });
}

function setupFancyBox(){
	jQuery('.fancybox_ajax').fancybox({
		hideOnContentClick:false,
		titleShow:false
	});
}

