/**** Add application wide javascripts below this point  ******/
var pcount = 0;
function lastItem(carousel, item){

}
function itemVisibleInCallback(carousel, item, i, state, evt){
	$('#gallery_list img').removeClass('highlight');
	$('.jcarousel-item img').fadeTo('fast', 0.5);

	size = carousel.size();
	current = $(item).attr('jcarouselindex');
	if(state == "next" ){
		i--;
	}else if(state == "prev"){
		i++;
	}else if(current != size) {
		i--;
	}
	
	$('#gallery_image_'+(i-1)).addClass('highlight');
	$('.jcarousel-item').removeClass('current-pic');
	$('.jcarousel-item-'+i).addClass('current-pic');
	$('.jcarousel-item-'+i + " img").fadeTo('fast', 1);		
	$('.jcarousel-item-'+i+ " .bigreflect").fadeTo('fast', 1);
	$('#viewer li').css('display', 'none');
	$('#viewer li#img_viewer_'+(i-1)).css('display', 'block');
};

function initCall(carousel){
	
	jQuery('#big_carousel a').bind('click', function() {
	  return false;
	});
	jQuery('#gallery_list a').bind('click', function() {
		rel = jQuery.jcarousel.intval(this.rel);
		carousel.scroll(rel);
		$('#gallery_list img').removeClass('highlight');
		$('#gallery_image_'+rel).addClass('highlight');
		$('.jcarousel-item img').fadeTo('fast', 0.5);
		$('.jcarousel-item-'+(rel+1) + " img").fadeTo('fast', 1);		
		$('.jcarousel-item-'+(rel+1)+ " .bigreflect").fadeTo('fast', 1);		
	  return false;
	});
	
}
jQuery(document).ready(function() {
		/*small carousel on school list page*/
		if($('#carousel').length){
    	jQuery('#carousel').jcarousel({
        wrap: 'both',
				scroll: 1,
				auto:0
    	});
		}
		/*large carousel on school gallery page*/
		if($('#big_carousel').length){
			$('#big_carousel').removeClass('hide');
    	jQuery('#big_carousel').jcarousel({
    		itemVisibleInCallback: {onBeforeAnimation: itemVisibleInCallback},
				initCallback: initCall,
				scroll:1
			});
		}
		/*highlight first gallery image*/
		$('#gallery_image_1').addClass('highlight');
		/*loading images*/
		$('#gallery_list div.inline_gallery_image').addClass('loading');
		$('#big_carousel li').addClass('loading');
		$('#carousel li').addClass('loading');		
		/*remove loading class*/
		$('#gallery_list div.inline_gallery_image img').load(function(){$('#gallery_list div.inline_gallery_image').removeClass("loading");});
		$('#big_carousel li img').load(function(){$('#big_carousel li').removeClass("loading");});
		$('#carousel li img').load(function(){$('#carousel li').removeClass("loading");});
		
		/*map page highlighting*/
			/*on hover, swap the background image*/
			$('#map .school-link').hover(
				function(){
					rel = $(this).attr('rel');
					$('#map .school-link').removeClass('highlight');
					$('#map #'+rel+'-link').addClass('highlight');
					$('#map #'+rel+'-marker').addClass('highlight');					
				},
				function(){
					$('#map .school-link').removeClass('highlight');
				}
			);
			/*hide contact details*/
			$('#map #extras .school-details').addClass('hide');
			$('#map #extras #map-instructions').removeClass('hide');
			/*on click show contact details*/
			$('#map .school-link').click(function(){
				/*highlight bits*/
				rel = $(this).attr('rel');
				$('#map .school-link').removeClass('highlight');
				$('#map .school-link').removeClass('active');
				$('#map #school_navigation li').removeClass('active');
				$('#map #'+rel+'-link').addClass('highlight');
				$('#map #school_navigation #'+rel+'-link').parent().parent().addClass('active');
				$('#map #'+rel+'-marker').addClass('active');
				/*hide instructions, show contact details*/
				$('#map #extras #map-instructions').addClass('hide');
				$('#map #extras .school-details').addClass('hide');
				$('#map #extras #'+rel+'-details').removeClass('hide');
				return false;
			});
});
	

