Lista=function(options){
	this.object=null;
	this.Config={
		pozycje: new Array(),
		offset: 0,
		pozycjaHeight: 34
	}
	if(options)
    {
		jQuery.extend(this.Config, options);
	}
	this.init=function(){
		this.object=$('<div class="cgk_lista"></div>');
		var lista_html=$('<ul></ul>');
		for(i in this.Config.pozycje){
			li_html=$('<li><a href="'+this.Config.pozycje[i][1]+'">'+this.Config.pozycje[i][0]+'</a></li>');
			if(this.Config.pozycje[i][2]==1) li_html.css('font-weight','bolder');
			lista_html.append(li_html);
		}
		this.object.append(lista_html);
		if(this.Config.pozycje.length>5){
			var tmp_this=this;
			var btn_top_html=$('<a class="cgk_btn_top" href="javascript:void(0)"></a>').click(function(){
				if(tmp_this.Config.offset>0){
					lista_html.animate(
					{
						'top': '+=' + tmp_this.Config.pozycjaHeight.toString() + 'px'
					}, 
					'slow'
					);
					tmp_this.Config.offset--;					
				}
			});
			this.object.append(btn_top_html);
			var btn_bottom_html=$('<a class="cgk_btn_bottom" href="javascript:void(0)"></a>').click(function(){
				if(tmp_this.Config.offset+5<tmp_this.Config.pozycje.length){
					lista_html.animate(
					{
						'top': '-=' + tmp_this.Config.pozycjaHeight.toString() + 'px'
					}, 
					'slow'
					);
					tmp_this.Config.offset++;
				}
			});
			this.object.append(btn_bottom_html);
		}
		
	}
	this.init();
}
Kartka=function(options){
	this.object=null;
	this.Config={
		left: 0,
		months: ['stycznia','lutego','marca','kwietnia','maja','czerwca','lipca','sierpnia','września','października','listopada','grudnia'],
		days: ['niedziela','poniedziałek','wtorek','środa','czwartek','piątek','sobota'],
		kartkaWidth: 270,
		day_offset: 0
	}
	if(options)
    {
		jQuery.extend(this.Config, options);
	}
	this.init=function(){
		this.object=$('<div class="cgk_kartka"></div>').css('left',this.Config.left);
		var data=new Date();
		data.setDate(data.getDate()+this.Config.day_offset);
		var data_html=$('<div class="cgk_data"><span class="dzien">'+data.getDate()+'</span><span class="miesiac">'+this.monthName(data.getMonth())+' - '+this.dayName(data.getDay())+'</span></div>');
		this.object.append(data_html);
		var rand=Math.ceil(10*Math.random());
		var date_month_string=(data.getMonth()+1>9)?data.getMonth()+1:'0'+(data.getMonth()+1);
		var date_day_string=(data.getDate()>9)?data.getDate():'0'+(data.getDate());
		data_string=data.getFullYear()+'-'+date_month_string+'-'+date_day_string;
		if(data_string in cgk_pozycje){
			if(cgk_pozycje[data_string].length>0){
				var lista=new Lista({'pozycje':cgk_pozycje[data_string]});
				this.object.append(lista.object);
			}		
		}else{
			this.object.append($('<p><a href="http://www.cogdziekiedy.czecho.pl/formularz.php">Wiesz o jakiejś imprezie?<br><br>Prześlij info! </a></p>'));
		}
	}
	this.remove=function(){
		this.object.remove();
	}
	this.monthName=function(id){
		if(id in this.Config.months) return this.Config.months[id];
		return '';
	}
	this.dayName=function(id){
		if(id in this.Config.days) return this.Config.days[id];
		return '';
	}
	this.init();
}
Kalendarz=function(options){
	this.Config={
		slidebox: '#cgk_kalendarz',
		nextButton: '#cgk_strzalka_prawa',
		prevButton: '#cgk_strzalka_lewa',
		index:0,
		kartkaWidth: 270
	}
	if(options)
    {
		jQuery.extend(this.Config, options);
	}
	this.kartki=new Array();
	this.load=function(){
		tmp_this=this;
		$.ajax({
		  async:false,
		  url: 'http://czecho.pl/cgk_pozycje2011.php',
		  dataType: 'script',
		  success:function() {tmp_this.removeLoading();tmp_this.init();},
		  error:function() {tmp_this.removeLoading();}
		});
	}
	this.removeLoading=function(){
		$(this.Config.slidebox).css('background','none');
	}
	this.init = function(){
		tmp_this=this;
		for(var i=0;i<3;i++){
			kartka=new Kartka({'left':i*270,'day_offset':i});	
			this.addKartka(kartka,1);			
		}
		$(this.Config.nextButton).click(function(){
			tmp_this.addNextKartka();
			$(tmp_this.Config.slidebox).children('.cgk_kartka').animate({
				'left': '-=' + tmp_this.Config.kartkaWidth.toString() + 'px'
			}, 
			'slow',
			null,
			function(){tmp_this.removeKartka(0)}			
			);	
			tmp_this.Config.index++;
			tmp_this.buttonActive();
		});
		$(this.Config.prevButton).click(function(){
			if(tmp_this.Config.index>-3){
				tmp_this.addPrevKartka();
				$(tmp_this.Config.slidebox).children('.cgk_kartka').animate({
					'left': '+=' + tmp_this.Config.kartkaWidth.toString() + 'px'
				}, 
				'slow',
				null,
				function(){tmp_this.removeKartka(1)}
				);					
				tmp_this.Config.index--;
				tmp_this.buttonActive();
			}
		});
		tmp_this.buttonActive();
	}
	this.removeKartka=function(strona){
		if(this.kartki.length<=3) return null;
		if(strona==1){
			var kartka=this.kartki.pop();			
			kartka.remove();
		}else{
			var kartka=this.kartki.shift(kartka);
			kartka.remove();
		}		
	}
	this.addKartka=function(kartka,strona){
		if(strona==1){
			$(tmp_this.Config.slidebox).append(kartka.object);
			this.kartki.push(kartka);
		}else{
			$(tmp_this.Config.slidebox).prepend(kartka.object);
			this.kartki.unshift(kartka);
		}
	}
	this.addNextKartka=function(){
		var day_offset=this.Config.index+3;
		kartka=new Kartka({'left':810,'day_offset':day_offset});
		this.addKartka(kartka,1);
	}
	this.addPrevKartka=function(){
		var day_offset=this.Config.index-1
		kartka=new Kartka({'left':-270,'day_offset':day_offset});
		this.addKartka(kartka,0);
	}
	this.buttonActive=function(){
		if(this.Config.index==-3){
			$(this.Config.prevButton).removeClass('cgk_active').addClass('cgk_deactivate');
		}else{
			$(this.Config.prevButton).removeClass('cgk_deactivate').addClass('cgk_active');
		}
	}
	
}
