// JavaScript Document
$(document).ready(function() {

    $('#footer .container ul li:first').addClass('first');

    var colsize = '';

    if ($('#content .leftColumn').size() > 0 && $('#content .rightColumn .hst-container').children().length > 0) {
        colsize = 'M';
    }
    else if($('#content .rightColumn').find('.links').length > 0) {
        colsize = 'M';
    }
    else {
        if ($('#content .leftColumn').size() > 0) {
            if ($('#content .leftColumn').is('.begrippen')) {
                colsize = 'Begr';
            }
            else {
                colsize = 'L';
            }
        } else if ($('#content .rightColumn .hst-container').children().length > 0) {
            colsize = 'XL';
        } else {
            colsize = 'XXL';
        }
    }

    if ($('#main').is(":empty")) {
        $('#main').hide();
    }

    $('#main, #bottomBanner .banner').addClass(colsize);

    $('.fancybox').fancybox({
        'cyclic'         : 'true',
        'titlePosition'    : 'inside',
        'titleFormat'    : formatTitle
    });

    /* Readspeaker */
    $('#main').prepend('<div id="xp1"></div>');

    /* FAQ */
    $('.faq .question').click(function(event) {
    	if ($(this).next('.answer').css('display') != 'block') {
    		$('.answer').slideUp();
    	}
        event.preventDefault();
        $(this).next('.answer').slideToggle();
    });

    /* Carousel.*/
    if ($("#carousel").length > 0) {
        $("#carousel").scrollable({circular: true, cloned: 'false', keyboard: false, speed: 1000, next: '', prev: '' }).autoscroll({interval: 7000}).navigator("#carousel .navi");
        api = $("#carousel").data("scrollable");
        $("#carousel .items").first().css("left", "-"+$(this).find(".module.banner").first().css('width'));
    }
    
    /* Gallery */
    setTimeout( function() {
    	$(".gallery .scrollable").each(function(){
    		maxheight = Math.max.apply(Math, $(this).children('.items').children().map(function(){ return $(this).height(); }).get());
        	$(this).height(maxheight);
        	$(this).parent().find('.browse').each(function(){
        		$(this).css('margin-top', (maxheight - $(this).height()) / 2)
        	});
    	});
    	$(".gallery .scrollable").scrollable({circular: true, speed: 1000 });
	}, 200 );
    

    /* Newsletter */
    if ($('#newsletter').size() > 0) {

        $('#newsletter form input:text').each(function() {
            var origVal = $(this).val();

            $(this).focus(function() {
                if ($(this).val() == origVal) {
                    $(this).val("");
                }
            });
            $(this).blur(function() {
                if ($(this).val() == "") {
                    $(this).val(origVal);
                }
            });
        });

        $.validator.addMethod("notNaam", function(value, element) {
            return value != "Naam";
        });

        $('#newsletter form').validate({
            "rules": {
                "name": {
                    "required" : true,
                    "notNaam" : true
                },
                "emailAddress": {
                    "required": true,
                    "email": true
                }
            },
            "submitHandler": function(formDomRef) {
                $(formDomRef).ajaxSubmit({
                    "success" : function(responseText, statusText, xhr, form) {
                        $('#newsletter #newsletterSuccess').css('visibility', 'visible');
                        $('#newsletter form').hide();
                    },
                    "error" : function(xhr, textStatus, errorThrown) {
                        //TODO We should warn the user
                    }
                });
            },
            "messages": {
                "name":  {
                    "required": "U heeft geen naam ingevuld. Wij kunnen uw aanvraag hierdoor niet verwerken.",
                    "notNaam" : "U heeft geen naam ingevuld. Wij kunnen uw aanvraag hierdoor niet verwerken."
                },
                "emailAddress": {
                    "required": "U heeft geen e-mailadres ingevuld. Wij kunnen uw aanvraag hierdoor niet verwerken.",
                    "email": "U heeft een onjuist emailadres opgegeven. Wij kunnen uw aanvraag hierdoor niet verwerken."
                }
            }
        });
    }
    
//    /* Search Filter */
//    $('.filter li a').click(function(event){
//    	$(this).parent().toggleClass('selected');
//    });
    
    /* Sort by */
    $('#sort a').click(function(event){
    	event.preventDefault();
    	$('#sort a').removeClass('active');
    	$(this).addClass('active');
    });
    
    /* Link by clicking anywhere in assignment */
    $('.module.assignment:not(.detail)').click(function(event){
    	window.location = $(this).find('a.link').attr('href');
    });

    /* Link by clicking anywhere in article */
    $('.module.article').click(function(event){
        window.location = $(this).find('a.link').attr('href');
    });

    /* Rating */
    function displayDocentRating() {
		  $(".rate").removeClass("rate").addClass("docentRating");
		  $(".docentRating").html('<li></li>');
		  $(".docentRating").children("li").addClass("stars-"+$.cookie("rated-"+$("#assignmentUID").text())+"0");
    }
    
    
    if($.cookie("rated-"+$("#assignmentUID").text()) == null){
    	$(".rate").children("li").click(
			function () {
			  $(this).addClass("on");
			  $(this).prevAll().addClass("on");
			  $(this).nextAll().removeClass("on");
			  $("#ratingField").val($(this).text());
			  
			  $.post($("#ratingForm").attr('action'), $("#ratingField"));
			  $.cookie("rated-"+$("#assignmentUID").text(), $("#ratingField").val());
			  displayDocentRating();
			});
		
		$(".rate").children("li").hover(
			function () {
			  $(this).addClass("hover");
			  $(this).prevAll().addClass("hover-on");
			  $(this).nextAll().addClass("hover-off");
			},
			function () {
			  $(this).siblings().add(this).removeClass("hover hover-on hover-off");
			});
    } else {
    	displayDocentRating();
    };
    
    /* print option teacher details */
    $('#printTeacherDetails').change(function(){
    	if ( $('#printTeacherDetails').is(':checked') ) {
    		$('.teacherDetails').removeClass('hideForPrint');
    		$('#reactions').removeClass('hideForPrint');
    	} else {
    		$('.teacherDetails').addClass('hideForPrint');
    		$('#reactions').addClass('hideForPrint');
    	}
    });
    
    
    /* show/hide reaction form*/
    if($("#postReaction").css('display') == 'none'){
        $('#showReactionForm').click(function() {
          
          $("#postReaction").slideToggle();
      });
    } else {
        $("#postReaction .submit").val('Plaats een reactie');
    }    
    
    /* if IE then width of easyform select box will become auto when focussed */
    if($.browser.version < '9'){
        $(".ef-field select").mousedown(function(){
            if($.browser.msie) {
                $(this).css("width","auto");
            }
        });
        
        $(".ef-field select").change(function(){
            if ($.browser.msie) {
                $(this).css("width","275px");
            }
        });

        $(".ef-field select").blur(function(){
            if ($.browser.msie) {
                $(this).css("width","275px");
            }
        });
    }
    
    /* give last module in moduleColumns no margin-right */
    
    $('.bannerColumns').find('.hst-container .hst-container-item').last().find('.module.banner').addClass('banner-last');
    
});

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    //return '<div>' + (title && title.length ? '<strong>' + title + '</strong>' : '' ) + ' (' + (currentIndex + 1) + '/' + currentArray.length + ')</div>';
    return '<div class="text"><p class="title">' + (title && title.length ? title : '' ) + '</p>'
            + 'afbeelding ' + (currentIndex + 1) + ' van ' + currentArray.length + '</div>';
}

