$(document).ready(function() {
	if ($.url.segment(1) && !isNaN($.url.segment(1))){
		$.getJSON("http://www.davehillphoto.com/index.php/api/gallery/media_items/?id=" + $.url.segment(1), function(data){					
			fireBox($.url.segment(1), data[0].width, data[0].height, data[0].name, data[0].media_caption);
		});	
	}
	
	var bts = $.controller.array("gallery/galleries_media_items", {gallery_id: 8, include: "media_item", order: "position", limit: 6});
	$(".date").formatDate("F d, Y  H:iA", "taken_at");
	$("ul.bts-items li").format(function(elem, data){
		$(elem).find("h3 a").click(function(){
			fireBox(data.media_item[0].id, data.media_item[0].width, data.media_item[0].height, data.media_item[0].name, data.media_item[0].media_caption);
			return false;
		}).text(data.media_item[0].name);
		$(elem).find(".photo a").click(function(){
			fireBox(data.media_item[0].id, data.media_item[0].width, data.media_item[0].height, data.media_item[0].name, data.media_item[0].media_caption);
			return false;
		});
		if(data.media_item[0].bts_image){
			$(elem).find("img").attr("src", "http://www.davehillphoto.com/index.php/upload/media/p_" + data.media_item[0].bts_image).hide();
		} else {
			$(elem).find(".photo").remove();
			$(elem).find(".desc").css("marginLeft","0");
		}
		$(elem).find("p").html(data.media_item[0].media_caption);
		return true;
	});
	$("ul.bts-items").template(bts, {success:function(){
		$("ul.bts-items li img").each(function(){
			$(this).load(function(){
				var width = $(this).width();
				var height = $(this).height();
				if(width > height && width > 300){
					var arg = height / 200;
					var new_w = width / arg;
					if(new_w < 300) {
						var arg = width / 300;
						var new_h = height / arg;
						$(this).css({"margin-top":"-"+Math.round((new_h - 176) / 2) + "px", "width":"300px"});
					} else {
						$(this).css({"margin-left":Math.round((new_w - 300) / 2) + "px", "margin-top":"-12px", "height":"200px"});
					}
				} else {
					var arg = width / 300;
					var new_h = height / arg;
					$(this).css({"margin-top":"-"+Math.round((new_h - 176) / 2) + "px", "width":"300px"});			
				}		
				$(this).show();		
			});
		});	
	}});
	
	// pagination
	var limit = 6;
	var totalItems = 0;
	var pages = 0; 
	var page = 1; 
	var offset = 0;
	
	$(bts).observe("contents", function(){
		arrayId = '';
		var opts = {
			limit: limit,
			gallery_id: 8
		}
		var countOpts = $.extend({}, opts);
		delete countOpts.offset;
		delete countOpts.limit;
		jsonString = "http://www.davehillphoto.com/index.php/api/gallery/galleries_media_items/count?" + $.serialize(countOpts);
		$.getJSON(jsonString, function(data){
			totalItems = parseInt(data);
			pages = ((totalItems/limit) + totalItems)/limit-1;
			pages = Math.round(pages + 0.5); //round up
			if (((limit*pages)-limit)-(pages-2) == totalItems) { pages = pages-1; }
			if (totalItems == 1) { pages = 1; }
			$("#paginator_links").text("");
			if (page < pages){
				$("div.arrow-right").html("<a id='pagination_next_link' href=''></a>");
				$("#pagination_next_link").click(function(){
					page ++;
					offset = (page-1)*(limit);
					bts.conditions.offset = offset;
					bts.retrieve();
					return false;
				});
			} else {
				$("div.arrow-right").html("");
			}
			if (page > 1){
				$("div.arrow-left").html("<a id='pagination_prev_link' href=''></a>");
				$("#pagination_prev_link").click(function(){
					page --;
					offset = (page-1)*(limit);
					bts.conditions.offset = offset;
					bts.retrieve();
					return false;
				});
			} else {
				$("div.arrow-left").html("");
			}
			for(var i, i=1; i<=pages; i++){
				if (page == i){
					$("#paginator_links").append("<li class='pagination-link active'><a href='javascript:void(0)'>" + i + "</a></li>");
				} else {
					var link = $("<li class='pagination-link'><a href=''>" + i + "</a></li>");
					link.click((function(i2){
						return function(){
							page = i2;
							offset = (i2-1)*(limit);
							bts.conditions.offset = offset;
							bts.retrieve();
							return false;
						}
					})(i));
					$("#paginator_links").append(link);
				}
			}
		});
	});		
	
});


function fireBox(mediaID, width, height, name, caption) {

	win_w = $(window).width();
	win_h = $(window).height();
	scrolltop = $(window).scrollTop();
	scrollleft = $(window).scrollLeft();	
	
	if (width == ''){ width = '400px';}
	if (height == ''){ height = '400px';}
	var loadingTimer;
	loadingFrame = 1;

	var heightmargin = (Math.round((win_h - (parseInt(height) + 110)) / 2) + scrolltop) + 'px';

	function showLoading() {
		clearInterval(loadingTimer);
		$("#fs-loading").css({'left': ((win_w - 40) / 2 + scrolltop), 'top': ((win_h - 40) / 2 + scrollleft)}).show();
		loadingTimer = setInterval(animateLoading, 66);
	}

	function animateLoading(el, o) {
		if (!$("#fs-loading").is(':visible')){
			clearInterval(loadingTimer);
			return;
		}
		$("#fs-loading > div").css('top', (loadingFrame * -40) + 'px');
		loadingFrame = (loadingFrame + 1) % 12;
	}

	var fs_overlay = $("<div></div>").attr("id","fs-overlay").hide();
	$("body").append(fs_overlay);
	var fs_holder = $("<div></div>").attr("id","fs-holder").hide();
	$("body").append(fs_holder);
	var fs_close = $("<div></div>").attr("id","fs-close").click(function(){fireBoxClose();});
	fs_holder.append(fs_close);

	var fs_body = $("<div></div>").attr("id","fs-body").css({"width": (parseInt(width) + 28)+"px","margin-top": heightmargin});
	fs_holder.append(fs_body);

	var fs_loading = $("<div></div>").attr("id","fs-loading");
	fs_body.append(fs_loading);
	var fs_loaddiv = $("<div></div>");
	fs_loading.append(fs_loaddiv);

	var fs_content = $("<div></div>").attr("id","fs-content");	
	$(fs_content).html($("#bts-popup").html());
	$(fs_content).find("h4").text(name);
	$(fs_content).find("p").html(caption);
	$(fs_content).find(".video").html('<iframe src="http://www.davehillphoto.com/public/popup.html?id='+ mediaID+'" frameborder="0" scrolling="no" width="'+width+'" height="'+height+'"></iframe>');
	fs_body.append(fs_content);
	
	$("#fs-overlay").fadeIn(300);	
	showLoading();

	$("#fs-loading").remove();
	$("#fs-holder").fadeIn(300);
	
	return false;
}

function fireBoxClose(){
	$("#fs-holder").fadeOut(300, function() {
		$("#fs-holder").remove();
	});
	$("#fs-overlay").fadeOut(400, function() {
		$("#fs-overlay").remove();
	});
	
	return false;
}