calendar={
	//
	ajax_day:"/events/embed/get_day.php",
	ajax_month:"/events/embed/get_month.php",
	month:null,
	day:null,
	cal:null,
	cat:null,
	regexp: new RegExp("[^0-9]", "g"),
	init:function(){
		calendar.setLinks();
		
		// Loading Animation
		$("#calendar-day").after('<div id="calendar-day-loading">Loading&hellip;</div>');
		$("#calendar-day-loading").hide();
		
		$("#calendar-month").after('<div id="calendar-month-loading">Loading&hellip;</div>');
		$("#calendar-month-loading").hide();
		
		// check for specific calendar
		calendar.cal = getQuerystring("cal");
		calendar.cat = getQuerystring("cat");
		
		// check if hash exists
		if(parent.location.hash > '') {
			var init_date = parent.location.hash;
			init_date = init_date.replace("#","");
			calendar.month = init_date;
			calendar.day = init_date;
			calendar.loadDay();
			calendar.loadMonth();
		}
	},
	setLinks:function(){
		var month_links = $("table.small-cal caption a");
		jQuery.each(month_links, function() {
			var this_href = calendar.getURLdate($(this).attr("href"),3);
			//$(this).attr("href",'#' + this_href);
			$(this).click(function () {
				if(this_href.length == 6) calendar.month = this_href;
				calendar.loadMonth();
				return false;
			});
		});
		var day_links = $("table.small-cal td a");
		jQuery.each(day_links, function() {
			var this_href = calendar.getURLdate($(this).attr("href"));
			$(this).attr("href",'#' + this_href);
			$(this).click(function () {
				if(this_href.length == 8) calendar.day = this_href;
				$("table.small-cal td").removeClass("open");
				$(this).parent().addClass("open");
				calendar.loadDay();
			});
		});
	},
	loadDay:function(){
		$("#calendar-day").fadeOut(250,function() { // fade out day
			$("#calendar-day-loading").show(); // show loading
			$.post(calendar.ajax_day, { date: calendar.day , cal: calendar.cal , cat: calendar.cat }, function(data){
				$("#calendar-day").html(data); // load data
				$("#calendar-day-loading").hide(); // hide loading
				$("#calendar-day").fadeIn(250); // fade in day
			});
		});
		calendar.setAddLink();
	},
	loadMonth:function(){
		$("#calendar-month").fadeOut(250,function() { // fade out day
			$("#calendar-month-loading").show(); // show loading
			$.post(calendar.ajax_month, { date: calendar.month , cal: calendar.cal , cat: calendar.cat }, function(data){
				$("#calendar-month").html(data); // load data
				$("#calendar-month-loading").hide(); // hide loading
				$("#calendar-month").fadeIn(250); // fade in day
				calendar.setLinks();
			});
		});
	},
	getURLdate:function(val,num){
		if(!num) num = 4;
		val = val.replace('http://' + window.location.hostname,'');
		val = val.split("/");
		var dateval = '';
		for(i=0;i<num;i++) {
			dateval += val[i+2];
		}
		dateval = dateval.replace(calendar.regexp,'');
		return dateval;
	},
	setAddLink:function(){
		if($("#btn-new-event").length > 0) {
			var old_url = $("#btn-new-event").attr("href");
			old_url = old_url.split("?");
			new_url = old_url[0] + "?date=" + calendar.day;
			$("#btn-new-event").attr("href",new_url);
		}
	}
}
function getQuerystring(key, default_) {
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}


cal_scroll={
	cal_offset: null,
	cal_margin: null,
	col_offset: null,
	col_offest_right: null,
	init:function(){
		cal_scroll.setValues();
		
		cal_scroll.scroller();
		$(window).scroll(function() {
			cal_scroll.scroller();
		});
		$(window).resize(function() {
			cal_scroll.setValues();
		});
	},
	setValues:function(){
		cal_scroll.cal_margin = parseInt( $("#calendar-month").css("margin-top") );
		cal_scroll.cal_offset = parseInt( $("#calendar-month").offset().top ) - cal_scroll.cal_margin;
		cal_scroll.col_offset = parseInt( $("#col-2").offset().top );
		var viewport_width = parseInt( $(window).width() );
		var content_width = parseInt( $("#main-wrap").width() );
		cal_scroll.offset_right = ((viewport_width - content_width) / 2) + 10;
	},
	scroller:function(){
		var page_scroll = parseInt( $( document ).scrollTop() );
		//alert("Page: " + page_scroll + ".  Cal: " + cal_scroll.cal_offset);
		if(page_scroll > cal_scroll.col_offset) {
			$("#col-2").css({
				'position' : 'fixed',
				'top' : '0',
				'right' : cal_scroll.offset_right + 'px'
			});
		}
		else {
			$("#col-2").css({
				'position' : 'static',
				'top' : 'auto',
				'right' : 'auto'
			});
		}
	},
	iescroller:function(){
		var page_scroll = parseInt( $( document ).scrollTop() );
		if(page_scroll > cal_scroll.cal_offset) {
			var scroll_diff = (page_scroll - cal_scroll.cal_offset);
			$("#calendar-month").css("margin-top",(scroll_diff + cal_scroll.cal_margin) + "px");
		}
		else {
			$("#calendar-month").css("margin-top",cal_scroll.cal_margin + "px");
		}
	}
}


cal_filter={
	visibility:null,
	type:null,
	init:function(){
		// add html
		$("div.filters").append('<div id="filter-canvas"><form method="post" action="/events/filters/" id="filter-form"><div id="cal-filters"><h4>Source Filters</h4><ul></ul></div><div id="cat-filters"><h4>Category Filters</h4><ul></ul></div><div id="filter-actions"><a href="#" id="filter-set"><span>Set Filters</span></a> <a href="#" id="filter-cancel"><span>Cancel</span></a></div></form></div>');
		
		// set events
		$("#filter-cat-add").click(function(){ cal_filter.show('cat'); return false; });
		$("#filter-source-add").click(function(){ cal_filter.show('cal'); return false; });
		$("#filter-cancel").click(function(){ cal_filter.cancel(); return false; });
		$("#filter-set").click(function(){ cal_filter.set(); return false; });	
		
		// populate
		$("#cat-filters ul").load("/events/filters/ #category-filters li", function(){cal_filter.check('cat'); } );
		$("#cal-filters ul").load("/events/filters/ #source-filters li", function(){cal_filter.check('cal');} );
	},
	show:function(type){
		cal_filter.type = type;
		
		// make sure it's hidden and then display appropriate filters
		$("#filter-canvas").slideUp(400,function(){ $("#cat-filters").hide(); $("#cal-filters").hide(); $("#" + type + "-filters").show(); $("#filter-canvas").slideDown(); });
		
		// visibility
		cal_filter.visibility = true;
	},
	check:function(type){
		// check presets
		var presets = getQuerystring(type);
		presets = presets.split('+');
		
		var filters = $("#" + type + "-filters input");
		jQuery.each(filters, function() {
			var value = $(this).val();
			if(in_array(value,presets)) $(this).attr("checked","checked");
			//if(in_array('van', ['Kevin', 'van', 'Zonneveld'])) $(this).attr("checked","checked");
		});
		
		//$("#" + type + "-filters ul").columnize({ columns: 3 }); 
	},
	cancel:function(){
		$("#filter-canvas").slideUp();
		cal_filter.visibility = false;
	},
	set:function(){
		$("#filter-form").submit();
	}
}

$(document).ready(function(){ // jquery onload
	calendar.init();
	cal_scroll.init();
	cal_filter.init();
});


function in_array (needle, haystack, argStrict) {
	var key = '', strict = !!argStrict;
	if (strict) {
		for (key in haystack) {
			if (haystack[key] === needle) {
				return true;
			}
		}
	} else {
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
			}
		}
	}
	return false;
}
