$(document).ready(function () {
    
    
    
    //count number of thumbs. display carousell only if needed
    var thumbNo = 5;
    if($(".pageGallery ul li").length > thumbNo){
         $(".pageGallery").jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            visible: thumbNo,
            circular: false
        });
    }else{
    
        //remove next, replace prev with next image and remove cursor pointer
        $('.carousell .next').remove();
        $('.carousell .prev').addClass('justArrow');
    }
    
    $("a[rel=pageGallery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
    
    //remove border bottom from the last search res row
    $('.ajaxSearch_result:last').css('padding-bottom', '15px');
    
    
    $('h3.question').click(function() {
        $(this).next().slideToggle();
        $(this).toggleClass('open');
    }).next().hide();
    
    //faq answer ajax load
    //$('div.faq a:not(.locked)').click(function(e){
    $('div.faq a').click(function(e){
    	
    	e.preventDefault();
    	
    	$(this).toggleClass("open");
    	
    	var answer = $(this).next();
    	var link = $(this).attr('href');
    	
    	if(answer.html()){
    		answer.empty();
    	}else{
    		answer.load(link);
    	}
    
    });
    
    $("select[name=serviceArea] option:first").attr("selected", true); 
    
    //division service Areas selection
    $('#serviceArea').change(function(){
    
    	$('#division').empty();
    	var selectedVal = $("#serviceArea option:selected").attr('value');
    	
    	if(selectedVal){
    	
    		$('#division').html(divisionsByServiceArea[selectedVal]);
    	
    	}else{
    		
    		$('#division').html('<option value=""> - Select a service area - </option>');
    	
    	}
    	
    });
    
    
    //edit profile populate from js
    var receiveEmailsExists = typeof receiveEmails != 'undefined';
	if(receiveEmailsExists){ //set receiveEmails
    	$('input[name=receiveEmails]').attr('checked', true);
    }
    
    var serviceAreaExists = typeof serviceArea != 'undefined';
    if(serviceAreaExists){ //set servia area with division
    	$("select[name=serviceArea] option[value="+serviceArea+"]").attr("selected", true); 
    	$('#division').html(divisionsByServiceArea[serviceArea]);
    	$("select[name=division] option[value="+division+"]").attr("selected", true); 
    }
    
    var clusterExists = typeof cluster != 'undefined';
    if(clusterExists){//set cluster
    	$("select[name=cluster] option[value="+cluster+"]").attr("selected", true); 
    }

    
    //home pagination
    if($('#homePagination').length > 0 ){
    	//enable home navigation
    	var perPage = 5;
    	
    	//init
    	$('#homeArticles a').show();
    	$('#homeArticles a:gt('+(perPage-1)+')').hide();
    	
    	//build navigation
    	var totalItems = $('#homeArticles a').length;
    	
    	for(i=0; i < Math.ceil(totalItems/perPage); i++){
    		$('#homePagination').append('<a href="#">'+ (i+1) +'</a>');
    	}
    	
    	$('#homePagination a').click(function(event){
    		
    		event.preventDefault();
    		
    		$('#homePagination a').removeClass('active');
    		$(this).addClass('active');
    		
    		var clickedPage = $(this).text();
    			
    		if(clickedPage == 1){
    			$('#homeArticles a').show();
    			$('#homeArticles a:gt('+(perPage-1)+')').hide();
    		}else{
    			$('#homeArticles a').hide();
    			$('#homeArticles a:gt('+( perPage*(clickedPage-1) - 1)+')').show();
    			$('#homeArticles a:gt('+ (( perPage*(clickedPage-1) - 1) + perPage) +')').hide();
    		}
    		
    	});
    	
    	
    }
    
});

