(function($) {

$.makouriGallery = {

	verticalOffset: -77,
	
	horizontalOffset: 0,
	overlayOpacity: .80,  
	overlayColor: '#000',
	repositionOnResize: true,
	elm: '#gallery-pod',

	
	
	init: function(gallery_id){
		
		
	if(gallery_id==null) gallery_id=1;
	$.makouriGallery._show(gallery_id);
	//$.makouriGallery._initCarousel();
	$.makouriGallery._loadGallery(gallery_id);
	
	
	
	

	},
	
// ------------------------------------------------------------------------------------------------------------------


	_show: function(gallery_id){
		
		$.makouriGallery._hide();
	
		// IE6 Fix
		var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
		
		$("BODY").append(
		// - add gallery html here
		);
		
		$("#gallery-pod").css({
				position: pos,
				zIndex: 99999,
				padding:0,
				margin:0
		});
		
		
		
		$.makouriGallery._reposition();
		$.makouriGallery._maintainPosition(true);
		
		$(".close", $("#gallery-pod")).unbind("click");
			
	$(".close", $("#gallery-pod")).bind("click", function(){
					$.makouriGallery._hide();									  
														  })
		
		$("#gallery-pod").show();
		
		
	},
	



// ------------------------------------------------------------------------------------------------------------------


	_initCarousel: function(){
		
			
		
// var conveyor = $(".content-conveyor", $("#gallery-pod")),
	
var conveyor = $(".content-conveyor");
	
		
  // _item = $(".item", $("#gallery-pod"));
	var _item = $(".item");
	var _viewer = $(".viewer");
	var _firstitem = $(".firstitem");
	
    //set length of conveyor
	
	var _width = 66;

	//_width = _width.replace("px","");
	
	//console.log(_width);
	var _conveyorWidth = _item.length * _width;
	//alert(_conveyorWidth);
    conveyor.css("width", _item.length * _width);
			
			//alert("here");
    //config
	
	
    var sliderOpts = {
    max: (_item.length * _width) - parseInt($(".viewer", $("#gallery-pod")).css("width")),
	  //max: (_item.length * _width) - parseInt($(".viewer", $("#gallery-pod")).css("width")),
      slide: function(e, ui) { 
        conveyor.css("left", "-" + ui.value + "px");
      }
    };

//alert(sliderOpts.max);
    //create slider
    $("#slider").slider(sliderOpts);



		
	},
	
	
// ------------------------------------------------------------------------------------------------------------------	


_zoomBox: function(status,src){
	
	switch(status){
		case 'show':
		$("#zoomframe").hide();
	$("BODY").append('<div id="zoom_box"><a class="close"></a><img src="images/ajax-loader.gif" width="35" height="35" alt="" id="zoom-loader" /><div id="zoomframe"></div></div>');
	
	var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
	
	
	var zoom_arr = src.split('"');
	
	var zoomImgWidth = zoom_arr[5];
	var zoomImgHeight = zoom_arr[7];
	
	//var move = (maxWidth - imgWidth)/2;
/*
	
	$("#zoomframe").animate({
						 width: imgWidth,
						 height: imgHeight
						 },200);
	*/
	
		$("#zoom_box").css({
						width: zoomImgWidth,
						height: zoomImgHeight,
						position: pos,
						zIndex: 100001,
						padding: 0,
						margin: 0
					});
		
		
		
	$("#zoom_box .close").click(function(){
			$.makouriGallery._zoomBox("hide");							 
										 
										 })
										 
										
					   
	$.makouriGallery._overlay("show");
	
	$.makouriGallery._repositionZoom();
	$.makouriGallery._maintainPosition(true);
	
	// - preload zoom image
	var srcArr = Array(src);
	
	var preloaded = $.makouriGallery._preloadImages(srcArr,function(result){
																	
	//console.log("zoom has loaded");
	
	$("#zoomframe").html(src);
	$("#zoomframe").fadeIn("slow");
	$("#zoom-loader").hide();
	
	$.makouriGallery._repositionZoom();
	$.makouriGallery._maintainPosition(true);
});
	
	break;
	case 'hide':
	$("#zoom_box").remove();
	$.makouriGallery._overlay("hide");
	break;
	}
},
	
// ------------------------------------------------------------------------------------------------------------------	

	_overlay: function(status){
		switch(status){
			case 'show':
			$.makouriGallery._overlay('hide');
				$("BODY").append('<div id="popup_overlay"></div>');
				$("#popup_overlay").css({
										position: 'absolute',
										zIndex: 100000,
										top: '0px',
										left: '0px',
										width: '100%',
										height: $(document).height(),
										background: $.makouriGallery.overlayColor,
										opacity: $.makouriGallery.overlayOpacity
									});
			break;
			case 'hide':
			$("#popup_overlay").remove();
			break;
		}
		
		},
	

// ------------------------------------------------------------------------------------------------------------------	
	
	
	_hide: function(){
		$("#gallery-pod").hide();
		$.makouriGallery._maintainPosition(false);
	},
	
	
// ------------------------------------------------------------------------------------------------------------------	
	
	_reposition: function(elm){
		
		var top = (($(window).height() / 2) - ($("#gallery-pod").outerHeight() / 2)) + $.makouriGallery.verticalOffset;
		var left = (($(window).width() / 2) - ($("#gallery-pod").outerWidth() / 2)) + $.makouriGallery.horizontalOffset;
		if( top < 0 ) top = 0;
		if( left < 0 ) left = 0;
		
		// IE6 fix
		if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
		$("#gallery-pod").css({
					top: top + 'px',
					left: left + 'px'
				});
		
		$("#popup_overlay").height( $(document).height() );
		
			},
			
			
// ------------------------------------------------------------------------------------------------------------------	
	
	_repositionZoom: function(elm){
		
		var top = (($(window).height() / 2) - ($("#zoom_box").outerHeight() / 2)) + $.makouriGallery.verticalOffset;
		var left = (($(window).width() / 2) - ($("#zoom_box").outerWidth() / 2)) + $.makouriGallery.horizontalOffset;
		if( top < 0 ) top = 0;
		if( left < 0 ) left = 0;
		
		// IE6 fix
		if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
		$("#zoom_box").css({
					top: top + 'px',
					left: left + 'px'
				});
		
		$("#popup_overlay").height( $(document).height() );
		
			},
			
			
// ------------------------------------------------------------------------------------------------------------------	

	_setMenu: function(gallery_id){
	
	// - reset menu
	var id=1;
	$("#menu a",$.makouriGallery.elm).each(function(){
		$(this).attr("rel",id);
		$(this).removeClass("selected");
		$(this).unbind("click");
	$(this).bind("click", function(){
		// - call gallery
		$(this).addClass("selected");
		$("#frame").html("");
		$("#loader").show();
		$.makouriGallery._loadGallery($(this).attr("rel"));
								});
	
	id++;
	});
	
	$(".gallery"+gallery_id,$.makouriGallery.elm).addClass("selected");

	
	},
	
	
// ------------------------------------------------------------------------------------------------------------------	

_preloadImages: function(_images, callback){

	
	var gotime = _images.length;

	$.each(_images,function(e) {
		var arr = this.split('"');
		
		var src = arr[1];
	//	console.log(src);
		$(new Image()).load(function() {
			if (--gotime < 1){
				callback(true) ;
				//break;
			}
		}).attr('src',src);
	});	  
		  
		  
		 // return _images.length;
		  
},

// ------------------------------------------------------------------------------------------------------------------	

_loadDetail: function(src,zoom_src,caption){
$("#frame").hide();
$("#loader").show();
	// - get proportions of loaded image
	var arr = src.split('"');
	
	var imgWidth = arr[5];
	var imgHeight = arr[7];
	var currWidth = $("#detail").css("width");
	var maxWidth = 382; 
	currWidth = currWidth.replace("px","");
	
	var move = (maxWidth - imgWidth)/2;
	
	$("#detail").animate({
						 left: move,
						 width: imgWidth,
						 height: imgHeight
						 },200);
	
	var srcArr = Array(src);
	var preloaded = $.makouriGallery._preloadImages(srcArr,function(result){
																	
	//console.log("detail loaded");	
	$("#frame").html(src);
	$("#frame").fadeIn("slow");
	$("#loader").hide();
	
	// - click action for loaded image
	$("#frame").unbind("click");
	$("#frame").click(function(){
				
			
				
				$.makouriGallery._zoomBox('show',zoom_src);
							   });
	
	
	$("#caption").text(caption);
});

				},
	

// ------------------------------------------------------------------------------------------------------------------		
			
			
	_loadGallery: function(gallery_id){
	
	// - clear current gallery
	$(".content-conveyor").html("");
	
	$("#detail_pic").remove();
	$.makouriGallery._setMenu(gallery_id);
	$("#caption").text("");
	
	//$("#carousel").append("fjfkjfjkf");
	// - load gallery with ajax here	
	
	var params = "gallery_id="+gallery_id;
	$.ajax({
	url: "ajax/ajax.getGallery.php",
	type: "POST",
	dataType: "json",
	data: params,
	success : function(data){
		var carouselLength = data.tn_media.length;
	
	
	var html="";
	for(i=0;i<carouselLength;i++){
		//console.log(data.media[i]);
		var first="";
		if(i==0) first=" firstitem";
		html+= '<li class="item'+first+'">'+data.tn_media[i]+'</li>';
	}
	
	
	//var preloaded = $.makouriGallery._preloadImages(data.tn_media);
	
	$.makouriGallery._preloadImages(data.tn_media,function(result){
	//console.log("loaded");
	
	$("#slider").remove();
	$("#carousel").append('<div id="slider"></div>');
	$(".content-conveyor").html(html);
	
	//console.log(data.captions);
	
	$.makouriGallery._loadDetail(data.media[0],data.zoom_media[0],data.captions[0]);

	
	if(carouselLength>5){
		
	$("#slider").show();
	
	$.makouriGallery._initCarousel();
	
	} else {
		$("#slider").hide();
	}
	
	var index=0;
	$(".content-conveyor img",$.makouriGallery.elm).each(function(){
		$(this).attr("rel",index);
		$(this).unbind("click");
	$(this).bind("click", function(){
		// - call detail pic
		$.makouriGallery._loadDetail(data.media[$(this).attr("rel")],data.zoom_media[$(this).attr("rel")],data.captions[$(this).attr("rel")]);
		
								});
	index++;
	});
	
	
	//
	
	});
	
	

		
	
	
	
	
	
	
	}
	})	
	},
	
// ------------------------------------------------------------------------------------------------------------------	

	
	_maintainPosition: function(status) {
		if( $.makouriGallery.repositionOnResize ) {
			switch(status) {
				case true:
				$(window).bind('resize', $.makouriGallery._reposition);
				$(window).bind('resize', $.makouriGallery._repositionZoom);
				break;
				case false:
				$(window).unbind('resize', $.makouriGallery._reposition);
				$(window).unbind('resize', $.makouriGallery._repositionZoom);
				break;
				}
				}
				}
	//
}
	
	
	// Shortcut functions
	jMakouriGallery = function(gallery_id){
		$.makouriGallery.init(gallery_id);
	};

	})(jQuery);