function rsvp_submit(theform) {
	theform.action = '/events/event-rsvp-submit';
	theform.method = 'post';
	AjaxRequest.submit(theform,{
		'onSuccess':function(req){
			document.getElementById('rsvp-submit').innerHTML = req.responseText;
		}
	});
}

function get_url_nodate(){
	var loc = $('#page_url').val();
	var pages = loc.split('/');
	//remove the current date if there is date at the end
	var old_date = pages[pages.length-1];
	loc = loc.replace('/'+old_date,'');
	return loc;
}
$(document).ready(function(){
	var ul = $('.calendarstrip ul');
	if(ul.length>0){
		var loc = get_url_nodate();
		var curr = 0;
		var maxLeft = 0;
		var maxRight = 0;
		var speed = 200;
		
		var li = $('li',ul);
		var li_width = li.width();
		var week = ['NED','PON','UTO','SRI','ČET','PET','SUB'];
		var num_ele = 7;
		var btn_css = {
						'display':'block'
					}
		$('.calendarstrip_button_next').css(btn_css).click(function(){
			move('forward');
		});
		$('.calendarstrip_button_prev').css(btn_css).click(function(){
			move('prev');
		});
		var lastTime = parseInt($('.timestamp', li.last()).val());
		var firstTime = parseInt($('.timestamp', li.first()).val());
		//alert(firstTime+'\n'+lastTime);
		
		function move(direction){
			var record = false;
			
			if(direction=='forward'){
				if(curr<0){
					curr = 0;
					maxRight+=Math.abs(maxLeft);
					maxLeft = 0;
				}
				curr += num_ele;
			} else {
				curr -= num_ele;
			}
			var left = -(curr*li_width+curr*2);
			if(curr<maxLeft){
				maxLeft = curr;
				record = true;
			}
			if(curr>maxRight){
				maxRight = curr;
				record = true;
			}
			if(record){
				for(var x = 0; x<num_ele; x++){
					ul = $('.calendarstrip ul');
					var width = ul.width();
					var currTime = 0;
					
					var newDate = new Date();
					ul.width(width+li_width+14);
					if(direction=='forward'){
						maxRight = curr;
						var cloned = li.last().clone(false).appendTo(ul).attr('class','calendarstrip_day_inactive');
						lastTime += 86400;
						currTime = lastTime;
					}else{
						var cloned = li.first().clone(false).prependTo(ul).attr('class','calendarstrip_day_inactive');
						firstTime -= 86400;
						currTime = firstTime;
						left = 0;
					}
					
					$('.timestamp',cloned).val(currTime);
					newDate.setTime(currTime*1000);
					$('.calendarstrip_day_number',cloned).text(newDate.getDate());
					//$('.calendarstrip_day_link',cloned).attr('href',loc+'/'+get_date(newDate.getTime()).toLowerCase());
					//OVO AKO ZELIMO NAPRAVITI LINK A NE AJAX
					//$('.calendarstrip_day_link',cloned).attr('href',loc+'/'+get_date(newDate.getTime()).toLowerCase());
					$('.calendarstrip_day_name',cloned).text(week[newDate.getDay()]);
					delete newDate;
				}
				if(direction=='prev'){
					ul.css({'left':-(li_width*num_ele+2*num_ele)});
					//speed = 100;
				}
			}
			//console.log(curr+' record left:'+maxLeft+' record right:'+maxRight+' left:'+left);
			ul.animate({'left': left } , speed,
			function() {
			});
		}
	}
});
function get_date(timestamp){
	var month = new Array(12);
	month[0]="01";
	month[1]="02";
	month[2]="03";
	month[3]="04";
	month[4]="05";
	month[5]="06";
	month[6]="07";
	month[7]="08";
	month[8]="09";
	month[9]="10";
	month[10]="11";
	month[11]="12";
	if(typeof(timestamp)=='string'){
		var split = timestamp.split('/');
		return split[1]+'-'+month[parseInt(split[0])-1]+'-'+split[2];
	}else{
		var newDate = new Date();
		newDate.setTime(timestamp);
		var date = newDate.getDate()+'-'+month[newDate.getMonth()]+'-'+newDate.getFullYear();
		delete newDate;
		return date;
	}
};

function  flat_date_picker_change_date_weekly(date){
	//if month starts with 0, such as 07, it messes up the array 'month' in the function above
	if(date.charAt(0) === '0')
		date = date.substr(1);
	var newDate = get_date(date);
	loc = get_url_nodate();
	//alert(loc+'/'+newDate);
	window.location = loc+'/'+newDate;
};

function expand_event(obj){
	var sib = $('.hidden_info',$(obj).parent());
	if(sib.css('display')=='none'){
		sib.slideDown('fast');
	}else{
		sib.slideUp('fast');
	}
}

