$ (document).ready( function (){
	
	//setup the carousel
	$(".gallery_container").jCarouselLite({
		btnNext: "#gallery_next",
		btnPrev: "#gallery_prev",
		easing: "easeOutCirc",
		speed: 600,
		visible: 1,
		scroll: 1,
		 beforeStart: function(a) {
        	$('#gallery_next').css('display', 'none');
			$('#gallery_prev').css('display', 'none');

       },
       afterEnd: function(a) {
        	$('#gallery_next').css('display', 'block');
			$('#gallery_prev').css('display', 'block');
       }

	});

	//and the lighbox
	$("a[rel='cbox1']").colorbox();
	$("a[rel='cbox1']").colorbox({current:""});
	
	//and the video lighbox
	//$("a[rel='cbox2']").colorbox({iframe:true, width:871, height:490});
	$("a[rel='cbox2']").colorbox({iframe:true, width:760, height:490});
	$("a[rel='cbox2']").colorbox({current:""}); 	
 																																							 																											
	// page defaults
	var numPages = parseInt($('#gallery_num_pages').text());
	var pageid = parseInt($('#gallery_pageid').text());
	check_controls(pageid, numPages);	
	
	//click on the forward button...
	$('#gallery_next').click( function(){
		
		pageid = parseInt($('#gallery_pageid').text());
		new_pageid = pageid + 1;
		if(new_pageid > numPages){
			new_pageid = 1;	
		}					
		changepage(pageid, new_pageid, numPages);
						
	});

	//...and the back button
	$('#gallery_prev').click( function(){
		
		pageid = parseInt($('#gallery_pageid').text());
		new_pageid = pageid - 1;
		if(new_pageid < 1){
			new_pageid = numPages;	
		}							
		changepage(pageid, new_pageid, numPages);
			
	});

	//show / hide the foward and back controls			
	function check_controls (current_pageid, numPages){
		if(numPages > 1){
			$('#gallery_next').css('display', 'block');
			$('#gallery_prev').css('display', 'block');
		}
		/*
		if(current_pageid < numPages){
			$('#gallery_next').css('display', 'block');
		}else{
			$('#gallery_next').css('display', 'none');
		}
		if(current_pageid > 1){
			$('#gallery_prev').css('display', 'block');
		}else{
			$('#gallery_prev').css('display', 'none');
		}
		*/
			
	}
	
	function changepage(oldpageid, new_pageid, numPages){
				
		//set the new pageid
		$('#gallery_pageid').text(new_pageid);
		
		//update the hidden div's etc
		$('#gallery_page_no').text('Page '+new_pageid+' of '+numPages);	
		$('#gallery_pageid').text(new_pageid);	
		//show/ hide the foward and back buttons...					
		//check_controls(new_pageid, numPages);
		
	}
														
	
});
