
(function(){
	
	$('textarea.comment').autoResize({
    // On resize:
    onResize : function() {
        $(this).css({opacity:0.8});
    },
    // After resize:
    animateCallback : function() {
        $(this).css({opacity:1});
    },
    // Quite slow animation:
    animateDuration : 300,
    // More extra space:
    extraSpace : 40
	});
	
	$('#commentPreview').click(function(){
		$(this).toggleClass('edit');
		if($(this).hasClass('edit'))
			previewComment($(this));
		else
			editComment($(this));
		return false;
	});
	
	function previewComment($button){
		$button.css('visibility','hidden')
		var html;
		var cName = $('#send_comment').find('#acn, #fcn').val();
		var cCom =  $('#send_comment').find('#acc, #fcc').val();
		//console.log($('#send_comment').children().length);
		var cT = new Date()
		var currentTime = cT.getHours()+':'+cT.getMinutes();
		
		var currentYear = cT.getDate()+'.'+(cT.getMonth()+1)+'.'+cT.getFullYear();
		
		cCom = bbCode(cCom)
		
		html = '<div id="prevComment">';
		html = html + '<div class="comment">';
		html = html + '<div class="comment_head prev">';
		html = html + '<span class="comment_name">Forhåndsvisning</span>';
		html = html + '</div>';
		html = html + '<div class="comment_head">';
		html = html + 'Av: <span class="comment_name">'+cName+'</span>';
		html = html + '<span class="comment_date"></span>';
		html = html + '</div>';
		html = html + '<div class="comment_body">';
		html = html + '<p>'+cCom+'</p>';
		html = html + '</div></div></div>';
		
		$('#send_comment').children('#cForm').fadeOut(300, function(){
				$(this).css('display','none');
				$('#send_comment').before(html).fadeIn()
		});
		$button.text('Rediger');
		$button.css('visibility','visible');
		
	}
	
	function editComment($button){
		$('#prevComment').fadeOut(300, function(){
				$(this).remove();
				$('#cForm').css('display','block');
		});
		$button.text('Forhåndsvis');
		$button.css('visibility','visible');
	}
	
	$('#send_button').click(function(){
	
		var hasError = false;
		$('#cForm').children('input,textarea').each(function(){
			if(!$(this).hasClass('extra')){
				$(this).removeClass('error');
				if($(this).val()==''){
					hasError = true;
					$(this).addClass('error');
				}
			}
		});
		
		if(hasError)
			return false;
	});
	
	$('#toggleCHelp').click(function(){
		$('#commentHelp').slideToggle();
		return false;
	});
	
	$('select[id^=calid]').change(function(){
		if ($(this).val().length > 0)
			$(this).parent('form').submit();
		return false;
	});
	
	$('#datepicker').datepicker({
			dateFormat: 'yy-mm-dd',
			changeMonth : false,
			showOtherMonths : true,
			dayNamesMin: ['S', 'M', 'T', 'O', 'T', 'F', 'L'],
			monthNames: ['Januar','Februar','Mars','April','Mai','Juni','Juli','August','September','Oktober','November','Desember'],
			monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Des'],
			beforeShowDay: isSpecialDate,
			onSelect: goToEvent2,
			firstDay: 1
	});
	
	
	function genEventArray(){
		var eventArray = new Array();
		
				eventArray[0]='26.02.12';
			
				eventArray[1]='28.02.12';
			
				eventArray[2]='28.02.12';
			
				eventArray[3]='29.02.12';
			
				eventArray[4]='29.02.12';
			
				eventArray[5]='01.03.12';
			
				eventArray[6]='01.03.12';
			
				eventArray[7]='04.03.12';
			
				eventArray[8]='04.03.12';
			
				eventArray[9]='06.03.12';
			
				eventArray[10]='06.03.12';
			
				eventArray[11]='07.03.12';
			
				eventArray[12]='11.03.12';
			
				eventArray[13]='12.03.12';
			
				eventArray[14]='13.03.12';
			
				eventArray[15]='13.03.12';
			
				eventArray[16]='14.03.12';
			
				eventArray[17]='14.03.12';
			
				eventArray[18]='16.03.12';
			
				eventArray[19]='18.03.12';
			
				eventArray[20]='20.03.12';
			
				eventArray[21]='20.03.12';
			
				eventArray[22]='21.03.12';
			
				eventArray[23]='25.03.12';
			
				eventArray[24]='26.03.12';
			
				eventArray[25]='27.03.12';
			
				eventArray[26]='27.03.12';
			
				eventArray[27]='28.03.12';
			
				eventArray[28]='01.04.12';
			
				eventArray[29]='08.04.12';
			
				eventArray[30]='11.04.12';
			
				eventArray[31]='12.04.12';
			
				eventArray[32]='15.04.12';
			
				eventArray[33]='16.04.12';
			
				eventArray[34]='22.04.12';
			
				eventArray[35]='29.04.12';
			
				eventArray[36]='03.05.12';
			
				eventArray[37]='05.05.12';
			
				eventArray[38]='06.05.12';
			
				eventArray[39]='09.05.12';
			
				eventArray[40]='14.05.12';
			
				eventArray[41]='04.06.12';
			
				eventArray[42]='11.06.12';
			
				eventArray[43]='13.06.12';
			
				eventArray[44]='14.09.12';
			
				eventArray[45]='20.10.12';
			
		
		return eventArray;
	}
	
	function isSpecialDate(thedate){
		var theMonth = thedate.getMonth()+1+'';
		var theDay = thedate.getDate()+'';
		var theYear = thedate.getYear()+'';
		theYear = theYear.substr(1,2)
		
		if( jQuery.browser.msie ){
			theYear = theYear.substr(2,2)
		}
		
		var eventArray = new Array();
		
		eventArray = genEventArray();
		var eventArrayString = eventArray.toString();
		
		if(theDay.length==1)
			theDay = "0"+theDay;
		if(theMonth.length==1)
			theMonth = "0"+theMonth;
		
		theFullDate = theDay+'.'+theMonth+'.'+theYear;
		
		//if( $.inArray(theFullDate,eventArray) == -1 ) return [true,""];
		if( eventArrayString.indexOf(theFullDate) == -1 ) return [true,""];
		
    return [true, "specialDate"];
	}
	
	function goToEvent2(dateText, inst){
		var theYear = dateText.substr(2,2);
		var theMonth = dateText.substr(5,2);
		var theDay = dateText.substr(8,2);
		
		var theFullDate = theDay+'.'+theMonth+'.'+theYear;
		
		//console.log(theDay+'.'+theMonth+'.'+theYear);
		if($.inArray(theFullDate,genEventArray()) > -1 ){
			window.location.href = '/index.asp?id=38356&d='+theDay+'&m='+theMonth+'&y='+theYear+'';
		}
	}
	
	function goToEvent(dateText, inst){
		var theYear = dateText.substr(2,2);
		var theMonth = dateText.substr(5,2);
		var theDay = dateText.substr(8,2);
		
		var theFullDate = theDay+'.'+theMonth+'.'+theYear;
		
		console.log(inst);
		console.log($(this));
		
	}

	$('#right_bar_wrapper').ready(function(){
		var tH = $('#wrapper_bg_top').height();
		var sH = $('#top_menu_wrapper').height();
		$('#right_bar_wrapper').css('margin-top','-'+(tH-sH)+'px').css('display','block');
		
		//$('#right_bar_wrapper').css('margin-bottom','-'+(tH-(sH+10))+'px');
	});
	

	function bbCode(str){
		str = str.replace(/\[b\]/gi, '<span style="font-weight: bold;">');
		str = str.replace(/\[u\]/gi, '<span style="text-decoration: underline;">');
		str = str.replace(/\[i\]/gi, '<span style=" font-style: italic;">');
		str = str.replace(/\[bilde=(.+)\]/gi, '<img src="$1"/>');
		str = str.replace(/\[lenke=(.+)\]/gi, '<a href="$1">$1</a>');
		str = str.replace(/\[farge=(.+)\](.+)\[\/farge\]/gi, '<span style="color:$1">$2</span>');
		str = str.replace(/\[\/b\]/gi, '</span>');
		str = str.replace(/\[\/u\]/gi, '</span>');
		str = str.replace(/\[\/i\]/gi, '</span>');
		
		str = str.replace(/\:\)/gi, '<img src="/gfx/forum/smile.gif"/>');
		str = str.replace(/\:\(/gi, '<img src="/gfx/forum/sad.gif"/>');
		str = str.replace(/\:D/gi, '<img src="/gfx/forum/happy.png"/>');
		str = str.replace(/\:o/gi, '<img src="/gfx/forum/shocked.gif"/>');
		str = str.replace(/\:p/gi, '<img src="/gfx/forum/misc.gif"/>');
		
//		str = str.replace('[LI]', '<ul><li>');
//		str = str.replace('[Li]', '<ul><li>');
//		str = str.replace('[lI]', '<ul><li>');
//		str = str.replace('[/li]', '</li></ul>');
//		str = str.replace('[/LI]', '</li></ul>');
//		str = str.replace('[/Li]', '</li></ul>');
//		str = str.replace('[/lI]', '</li></ul>');
		str = str.replace(/\n/g, '<br/>');
		
		return str;
	}
	
	
})();

