$(document).ready(function(){
	if($('#set-filmstrip').length > 0){
		setup_gallery();
	}
	
	if($('#set-photos .photos-list').length > 0){
		//mask_thumb();
		thumb_popup();
	}
	
	//setupp different moving caption boxes
	if($('.boxgrid.caption').length > 0){
		setup_box_caption();
	}
	if($('.boxgrid.peek').length > 0){
		setup_box_peek();
	}
	
	$(window).bind("resize", resize_canvas);
	
	resize_canvas();
});

function resize_canvas() {
	b_width = $(window).width()-$('.column_left').width()-100;
	b_height = $(window).height()-300;
	
	// $('.photos-linear').css('height', b_height+'px');
	// $('.photos-linear .items').children('div:first').css('height', b_height+'px');
	
	var cssObj = {
        'height' :  b_height+'px',
        'width' : b_width+'px',
		'margin': '0px auto'
      }
	
	$('.photos-linear').css(cssObj);
	$('.photos-linear .items .item').css(cssObj);
}


function setup_gallery(){
	
	var set = $('.photos-linear').scrollable({
		size: 1,
		api: true,
		clickable: true,
		keyboard: false,
		easing: 'easeInQuad'
	});
	
	
	//getIndex is broken. fun.
	var filmstrip = $('#set-filmstrip .filmstrip').scrollable({api: true, size:10});
	
	
	var set_items = $("#set-photos .photos-linear .items div").click(function() { 
	  	var index = set_items.index(this);
		filmstrip.click(index);
	});
	var filmstrip_items = $("#set-filmstrip .filmstrip .items div").click(function() { 
	  	var index = filmstrip_items.index(this);
		set.seekTo(index);
	});
	$(filmstrip_items[0]).click();
	
	$('#set-photos .photos-linear .items .item').hover(  
		function() {  
			//display heading and caption  
			$(this).children('div:first').stop(false,true).animate({top:0},{duration:200, easing:'easeOutQuart'});    
		},  

		function() {
			console.log(this);
			//hide heading and caption  
			$(this).children('div:first').stop(false,true).animate({top:-50},{duration:200, easing: 'easeOutQuart'});   
		}  
	);
}

function mask_thumb(){
	$('#set-photos .photos-list .items .item img').each(function(){
		var img = $(this).attr('src');
		$(this).attr('src', public_folder+'images/global/thumb_mask.png');
		$(this).css('background-image', 'url('+img+')');
	});
}

function thumb_popup(){
	$("#set-photos .photos-list .items .item a").overlay({
		color: '#ccc',
		expose: '#f1f1f1',
		effect: 'apple',
		target: '#image-popup'/*,
		onBeforeLoad: function() {
			var popup = this.getTrigger().find('.popup');
			var wrap = this.getContent().find("div.wrap");
			$(wrap).html(popup);
		}*/
	}).gallery({
		speed: 800,
		autohide: false
	});
}

function setup_box_peek(){
	$('#gallery .sets .boxgrid.peek').hover(function(){
		$(".cover", this).stop().animate({top:'200px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
	});
}

function setup_box_caption(){
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:200});
	}, function() {
		$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:200});
	});
}