


(function($j) {

	
	$j.fn.calendar = function(options) {
	  var defaults = {
		  date:				new Date(),
	    current_year: 		new Date().getFullYear(),
	    current_month: 		new Date().getMonth(),
	    current_day: 		  new Date().getDate(),
	    container:			this
	    
	  };
	  
	  var opts = $j.extend(defaults, options);
	  
	  initCalendar(opts);
	};
	
	
	
	/*
	 * 	Init Calendar
	 */
	
	    var langurl = 'whatson/agenda';
    if(icl_lang == 'en') langurl = '/en/whatson/calendar';
		
	function initCalendar(opts) {




		$j(opts.container).append(
			  $j('<ul>').attr('id','months').append(
				  $j('<li>').append(
					  $j('<a>')
					  	.addClass('active')
					  	.attr('href',langurl+'?date='+new Date(opts.date.getFullYear(), opts.date.getMonth(), 1).getDate()+'-'+(new Date(opts.date.getFullYear(), opts.date.getMonth(), 1).getMonth()+1) +'-'+new Date(opts.date.getFullYear(), opts.date.getMonth(), 1).getFullYear())
					  	.html(numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth()).getMonth(),'month')+' '+new Date(opts.date.getFullYear(), opts.date.getMonth()).getFullYear())
					  	.attr('title',numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth()).getMonth(),'month')+' '+new Date(opts.date.getFullYear(), opts.date.getMonth()).getFullYear())
					  	.click(function(){ clickMonth(this,opts);  })
				  ),
				  $j('<li>').append(
					  $j('<a>')
					  	.attr('href',langurl+'?date='+new Date(opts.date.getFullYear(), opts.date.getMonth()+1, 1).getDate()+'-'+(new Date(opts.date.getFullYear(), opts.date.getMonth()+1, 1).getMonth()+1) +'-'+new Date(opts.date.getFullYear(), opts.date.getMonth()+1, 1).getFullYear())
					  	.html(numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth() +1).getMonth(),'month')+' '+new Date(opts.date.getFullYear(), opts.date.getMonth() +1).getFullYear())
					  	.attr('title',numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth() +1).getMonth(),'month')+' '+new Date(opts.date.getFullYear(), opts.date.getMonth() +1).getFullYear())
					  	.click(function(){ clickMonth(this,opts); })
				  ),
				  $j('<li>').append(
					  $j('<a>')
					  	.attr('href',langurl+'?date='+new Date(opts.date.getFullYear(), opts.date.getMonth()+2, 1).getDate()+'-'+(new Date(opts.date.getFullYear(), opts.date.getMonth()+2, 1).getMonth()+1) +'-'+new Date(opts.date.getFullYear(), opts.date.getMonth()+2, 1).getFullYear())
					  	.html(numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth() +2).getMonth(),'month')+' '+new Date(opts.date.getFullYear(), opts.date.getMonth() +2).getFullYear())
					  	.attr('title',numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth() +2).getMonth(),'month')+' '+new Date(opts.date.getFullYear(), opts.date.getMonth() +2).getFullYear())
					  	.click(function(){ clickMonth(this,opts); })
				  ),
				  $j('<li>').attr('id','month_nav').append(
					  $j('<a>').attr('href','#').addClass('down').click(function(){ clickArrow('down',opts); return false; }),
					  $j('<a>').attr('href','#').addClass('up').click(function(){ clickArrow('up',opts); return false; })
				  )
			  )			  
		);
		
		$j(opts.container).append($j('<ul>').attr('id','days'));
		
		

		for(var  i = 0; i < new Date(opts.current_year, opts.current_month,1 + i).getDate() ; i++) { 
			$j('#days').append(
				  $j('<li>').append(
					  $j('<a>')
					  	.attr('href',langurl+'?day=true&date='+new Date(opts.date.getFullYear(), opts.date.getMonth(), 1 + i).getDate()+'-'+(new Date(opts.date.getFullYear(), opts.date.getMonth(), 1 + i).getMonth()+1) +'-'+new Date(opts.date.getFullYear(), opts.date.getMonth(), 1 + i).getFullYear())
					  	.html(new Date(opts.date.getFullYear(), opts.date.getMonth(), 1 + i).getDate()) 					  	
					  	.prepend(
					  		$j('<span>').html(numberToName(new Date(opts.date.getFullYear(), opts.date.getMonth(), 1 + i).getDay(),'day'))		
					  	)
				  )
			)
		}
		
		$j('#days li:eq('+ (opts.current_day -1) +') a').addClass('active');
		
		if(opts.current_month == new Date().getMonth()){
			$j('#months li .up').removeClass('up').addClass('up_inactive').unbind().click(function(){ return false; });
		}
		
	 Cufon.replace('#months li a', { fontFamily: 'TradeGothicLTBold', hover: true });  
	 Cufon.replace('#days li a', { fontFamily: 'TradeGothicLTBold', hover: true });
	}
	
	
	
	
	/* 
	 * 	Month event handler
	 */
	
	function clickMonth(trigger,opts){
	    var href 			= $j(trigger).attr('href').split('?date=');
	    var date			= href[1].split('-');
	    opts.current_year 	= parseInt(date[2]);
	    opts.current_month 	= parseInt(date[1])-1;
	    opts.current_day	= 1;

	    $j('#months li a').removeClass('active');
	    $j(trigger).addClass('active');
	    	
		
		$j('#days').html('');
		
		for(var  i = 0; i < new Date(opts.current_year, opts.current_month, 1 + i).getDate(); i++) { 
			$j('#days').append(
				  $j('<li>').append(
					  $j('<a>')
					  	.attr('href',langurl+'?day=true&date='+new Date(opts.current_year, opts.current_month +1, 1 + i).getDate()+'-'+new Date(opts.current_year, opts.current_month +1, 1 + i).getMonth()+'-'+new Date(opts.current_year, opts.current_month +1, 1 + i).getFullYear())
					  	.html(new Date(opts.current_year, opts.current_month, 1 + i).getDate()) 
					  	.prepend(
					  		$j('<span>').html(numberToName(new Date(opts.current_year, opts.current_month, 1 + i).getDay(),'day'))		
					  	)
				  )
			)
		}
		
		if(opts.date.getFullYear() == opts.current_year && opts.date.getMonth() == opts.current_month ){ //console.log();
			$j('#days li:eq('+ (opts.date.getDate() -1) +') a').addClass('active');
		}
		
		Cufon.replace('#months li a', { fontFamily: 'TradeGothicLTBold', hover: true });	
		Cufon.replace('#days li a', { fontFamily: 'TradeGothicLTBold', hover: true });
	}
	
	
	
	
	
	/*
	 * 	arrow event handler
	 */
	
	function clickArrow(direction,opts){
		
		// Arrow down
		if(direction == 'down'){

			 if($j('#months li:eq(2) a.active').length > 0){
				 $j('<li>').append(
					 $j('<a>')
					  	.attr('href',langurl+'?date='+new Date(opts.current_year, opts.current_month +1,1).getDate()+'-'+(new Date(opts.current_year, opts.current_month+1).getMonth()+1)+'-'+(new Date(opts.current_year, opts.current_month+1).getFullYear()))
					  	.html(numberToName(new Date(opts.current_year, opts.current_month +1).getMonth(),'month')+' '+new Date(opts.current_year, opts.current_month +1).getFullYear())
					  	.attr('title',numberToName(new Date(opts.current_year, opts.current_month +1).getMonth(),'month')+' '+new Date(opts.current_year, opts.current_month +1).getFullYear())
					  	.click(function(){ clickMonth(this,opts); })
				 ).insertBefore('#months #month_nav');

				 $j('#months li a.active').parent().next().find('a').click();
				 $j('#months li:first').remove();
			 }
			 else{
				 $j('#months li a.active').parent().next().find('a').click();
			 }
			
		}
		// Arrow up
		else {
			
			 if($j('#months li:eq(0) a.active').length > 0){
				 $j('#months').prepend(
					 $j('<li>').append(
						  $j('<a>')
						  	.attr('href',langurl+'?date='+new Date(opts.current_year, opts.current_month -1,1).getDate()+'-'+(new Date(opts.current_year, opts.current_month-1).getMonth()+1)+'-'+(new Date(opts.current_year, opts.current_month-1).getFullYear()))
						  	.html(numberToName(new Date(opts.current_year, opts.current_month -1).getMonth(),'month')+' '+new Date(opts.current_year, opts.current_month -1).getFullYear())
						  	.attr('title',numberToName(new Date(opts.current_year, opts.current_month -1).getMonth(),'month')+' '+new Date(opts.current_year, opts.current_month -1).getFullYear())
						  	.click(function(){ clickMonth(this,opts);  })
					 )
				 )

				 $j('#months li a.active').parent().prev().find('a').click();
				 $j('#months li:last').prev().remove();
			 }
			 else{
				 $j('#months li a.active').parent().prev().find('a').click();
			 }
			
		}	

		if(new Date(opts.current_year, opts.current_month).getMonth() == new Date().getMonth() && new Date(opts.current_year, opts.current_month).getFullYear() == new Date().getFullYear()){
			$j('#months li .up').removeClass('up').addClass('up_inactive').unbind().click(function(){ return false; });
			if(opts.date.getFullYear() == opts.current_year && opts.date.getMonth() == opts.current_month ){ //console.log();
				$j('#days li:eq('+ (opts.date.getDate() -1) +') a').addClass('active');
			}
		}else{
			$j('#months li .up_inactive').removeClass('up_inactive').addClass('up').click(function(){ clickArrow('up',opts); return false; });
		}
		
		//cufon
		Cufon.replace('#months li a', { fontFamily: 'TradeGothicLTBold', hover: true });	
		Cufon.replace('#days li a', { fontFamily: 'TradeGothicLTBold', hover: true });
	}
	

	
	/*
	 * number To Name 
	 */
	
	function numberToName(number,type) {
		if(type == 'month'){
      if (icl_lang == 'en') {
        var months = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'];
      } else {
        var months = ['JANUARI', 'FEBRUARI', 'MAART', 'APRIL', 'MEI', 'JUNI', 'JULI', 'AUGUSTUS', 'SEPTEMBER', 'OKTOBER', 'NOVEMBER', 'DECEMBER'];
      }
			return months[number];
		}else{
      if (icl_lang == 'en') {
        var days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
      }
      else {
        var days = ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'];
      }
			return days[number];
		}
	}
	
})(jQuery);




