$(document).ready(function(){
		//make full listitems clickable 
		$('.summaries article').addClass('clickable').click(function(){
                     window.location = $('a', this).attr('href');
                });
		
		//make downloaditems clickable 
		$('#downloads li').addClass('clickable').click(function(){
                     window.location = $('a', this).attr('href');
                }); 
		//fix voor vuile input
                $('.Apple-style-span').replaceWith(function() {
                      return $(this).contents();
                    });
                
                //icon lock
                $('.locked').append('<div class="lock"/>');
                
		layoutFix();
});


$(window).load(function(){
	window.scrollTo(0, 1);
        window.scrollTo(0, 0);//hide bars from mobile safari
});


$(window).resize(function(){
		//canvas support? redraw images
		$.rotatorWidth = $('#banner').width() ;
		$('.canvas #banner .figure').each(function(){
			drawCanvas($(this));
		});

                layoutFix();
})


function layoutFix(){
    if($(window).width() > 767){
    //calendar sectie even groot maken
    $("#calContainer").height(($("#welcome").height())+"px");
    
    //redraw footer cols
    $('#sitelinks').footerCols();
    }
    else{
        $("#calContainer").height("auto");
    }
}

function anysurferFixCal(){
	//tab-selectable fixen
	$(".ui-datepicker-next, .ui-datepicker-prev").attr("href","#").live('click',function(event){event.preventDefault();});
	
	//caption fixen voor anysurfer
	$(".ui-datepicker-calendar","#calendar")
		.prepend("<caption/>")
		.children("caption")
		.attr("style","display: none;")
		.html($(".ui-datepicker-title","#calendar"))
		.fadeIn();
}

//footer multicols by calculating vertical negative offset per column
$.fn.footerCols = function(){
	offset= new Array();
	offset[0] = 0;
	for(i=1;i<5;i++){
		$('.col'+i).last().addClass('last');
		if(i<4){
			offset[i] = -25;
			$('.col'+i).each(function(){
				offset[i] += $(this).height() + 25;	
			});
			$('.col'+(i+1)+':first').attr('style','margin-top:-'+offset[i]+'px');
		}
	};
	
	this.height((offset.max()+25)+'px');
        $('.ie7 #affiliated').attr('style','top:1px'); //force redraw
}

Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
	return max;
}

//simple rotator
$.rotatorInterval = false;
$.rotatorPaused = false;

$.fn.rotator = function(slides,speed){
	if(slides){
			$.rotator = this;
			//reset stuff
			clearInterval($.rotatorInterval);
						
			//don't peek while we're working
			$.rotator.addClass('hidden').html('');
			//populate normal slides
			 $.each( slides, function(index, value) {
                                if(value.title !=''){
                                    txt = (value.url)? $('<a class="slogan clean"/>').html(value.title).attr('href',value.url) : $('<span class="slogan"/>').html(value.title);
                                }
                                else{
                                    txt = '';
                                }
                                figure = $('<div class="figure"/>').append( $('<img class="bannerImage"/>').attr('src',value.src).attr('alt',''));
                                if(txt!=''){figure.append($('<div class="figcaption"/>').append(txt))};
  				$.rotator.append(figure);
			});
			//canvas support? replace images
			$('.canvas #banner .figure').each(function(){
					drawCanvas($(this), $(this).width);
			});
                        $('.no-canvas #banner .figure').each(function(){
					$(this).append('<div class="slopeLeft"/>').append('<div class="slopeRight"/>');
			});
			
                        if(slides.length>1){
                            //control show
                            $.rotator.append($('<div id="rotatorCtrl"/>').append($('<a class="sideLink" href="#" title="pause the animation">pause</a>')));
                            $('#rotatorCtrl a').toggle(
                                                                             function(){
                                                                                    clearInterval($.rotatorInterval); 
                                                                                    $.rotatorPaused = true;
                                                                                    $(this).html('resume');
                                                                                    $(this).attr('title','resume the animation');
                                                                             },
                                                                             function(){
                                                                                     $.rotatorPaused = false;
                                                                                     $.rotatorInterval = setInterval(function(){rotate()},speed);
                                                                                     $(this).html('pause');
                                                                                     $(this).attr('title','pause the animation');
                                                                             }
                            );
                        }        
			
			
			//ready, show
			$('.figure:gt(0)', $.rotator).attr('style','display: none');
			$.rotator.removeClass('hidden');
			
                        if(slides.length>1){
                            //simple slideshow		
                            function rotate(){
                                    if(!$.rotatorPaused){
                                            $('.figure:first', $.rotator).fadeOut(500)
                                            .siblings('.figure:first').fadeIn(500)
                                            .end().appendTo($.rotator);
                                            }
                            }

                            $.rotatorInterval = setInterval(function(){rotate()},speed);

                            //stop on hover
                            $.rotator.hover(
                                    function(){clearInterval($.rotatorInterval);},
                                    function(){clearInterval($.rotatorInterval);$.rotatorInterval = setInterval(function(){rotate()},speed);}
                            );
                        }
		}
}

//tilted images cutoff
function drawCanvas(jqObj){
	$('canvas',jqObj).remove();								   
	jqObj.append($('<canvas class="bannerImage"/>'));
	
	var c    = $('canvas', jqObj)[0];
	var ctx  = c.getContext('2d');
	c.width  = $.rotatorWidth;

	var img = new Image;
	img.onload = function(){
		slashedRectWithBG( ctx, c.width, 50 , img );
	};
	img.src = $('img' , jqObj).attr('src');
	$('img' , jqObj).addClass('hidden');	
}


function slashedRectWithBG( ctx, w, slash, img ){
	h = img.height;
	iw = img.width
	ctx.save();
	ctx.beginPath();
	ctx.moveTo( slash, 0 );
	ctx.lineTo( w, 0 );
	ctx.lineTo( w-slash,h );
	ctx.lineTo( 0, h );
	ctx.closePath();
	ctx.clip(); // Clip to whatever path is on the context
	ctx.drawImage(img,0,0,iw,h);
	ctx.restore(); // Get rid of the clipping region
}

var specialDays = new Array();

function getSpecialDaysInit() {
    var myDate = new Date();
    getSpecialDays(myDate.getFullYear(), myDate.getMonth()+1)
}

function getSpecialDays(year, month) {
    arrayIndex = year+'-'+month
    if (specialDays[arrayIndex] == undefined) {
        $.ajax({
            type: 'POST',
            async: false,
            dataType: 'json',
            url: '/ajaxrequest/event/geteventdays/',
            data: 'year='+year+'&month='+month,
            success: function(data) {
                specialDays[arrayIndex] = $.parseJSON(data.specialDays);
            }
        });
    }
}

function isSpecialDay(date) {
    var year = date.getFullYear()
    var month = parseInt(date.getMonth()+1);

    arrayIndex = year + '-' + month;
    specialDaysRequestedMonth = specialDays[arrayIndex]
    day = date.getDate().toString();
    if (specialDaysRequestedMonth !== undefined) {
        if ($.inArray( day, specialDaysRequestedMonth) >= 0) {
            return [true, "specialDate"];
        }
        else {
            return [false, ""];
        }
    }
    return [false, ""];
}

function getEventDescriptions(dateText) {
    $('#eventdetails').html('');
    $('#calendar').addClass('wait');
    var dateArray = dateText.split('/');
    year = dateArray[2];
    month = dateArray[0];
    day = dateArray[1];
    $.ajax({
        type: 'POST',
        async: true,
        dataType: 'json',
        url: '/ajaxrequest/event/geteventdescription/',
        data: 'year='+year+'&month='+month+"&day="+day,
        success: function(data) {
            anysurferFixCal();
            $('#eventdetails').html(data.htmlcontent);
            $('#calendar').removeClass('wait');
        }
    });
}

function forwardToCalenderDayList(dateText) {

    var dateArray = dateText.split('/');
    year = dateArray[2];
    month = dateArray[0];
    day = dateArray[1];
    alert('ga naar' + year +'-'+month+'-'+day);
}

