var Gallery = (function(){
	var sizes = [];
	var data = [];
	var isShowingContent = true;
	var arrowUpShown = false;
	var currentImageIndex = 0;
	var rightArrowShown = true;
	var leftArrowShown = false;
	
	var loadImage = function(i) {
		if(i < data.length) {
 			if(data[i].loaded == false) {
				data[i].loaded = true;
				var img = new Image();
				img.onload = function(){
					$(".spinner_"+i).css("display","none");
					$(".galleryImage_"+i).attr("src",this.src);
					$(".galleryImage_"+i).attr("width",sizes[i].width);
					$(".galleryImage_"+i).attr("height",sizes[i].height);
					$(".galleryImage_"+i).fadeIn(600);
				};
				img.src = data[i].src;
			}
		}
	};
	
	var windowScroll = function(e){
		var ww = $(window).width(); //window width
		var sx = $("#viewport").scrollLeft(); //scroll x
		for(var i=0;i<sizes.length;++i) {
			var fw = 0; //full width
			for(var j=0;j<i;++j) {
				fw += sizes[j].width;
			}
			var ew = sizes[i].width+fw; //end width (img width + full width)
			if(sx+ww > ew) {
				if(i+1 < data.length) {
					loadImage(i+1);
				}
			}
		}
		
		// Check and update currentIndex
		var cw = 0;
		for(var k=0;k<currentImageIndex+1;++k) {
			cw += sizes[k].width;
		}

		if(sx >= cw && cw > 0) {
			if(currentImageIndex < sizes.length-1)
				currentImageIndex++;
		} else if(sx < cw-sizes[currentImageIndex].width) {
			if(currentImageIndex > 0)
				currentImageIndex--;
		}
		
		//This is to hide the content at the beginning of the page
		var cb = sizes[0].width+sizes[1].width*0.2; //content boundry
		if(sx > 300) {
			hideContent();
		} else {
			showContent();
		}
		
		var contW = $("#images_cont").width();
		if(sx + ww == contW) {
			if(rightArrowShown == true) {
				rightArrowShown = false;
				$("#rightScroll").fadeOut();
			}
		} else {
			if(rightArrowShown == false) {
				rightArrowShown = true;
				$("#rightScroll").fadeIn();
			}
		}
		if(sx == 0) {
			if(leftArrowShown == true) {
				leftArrowShown = false;
				$("#leftScroll").fadeOut();
			}
		} else {
			if(leftArrowShown == false) {
				leftArrowShown = true;
				$("#leftScroll").fadeIn();
			}
		}
	};
	
	var resize = function(e){
		sizes = [];
		var fullWidth = 0;
		var winW = $(window).width();
		var winH = $(window).height();
		var h;
		var w;
		for(var i=0;i<data.length;++i) {
			w = Math.round(winH*data[i].width/data[i].height);
			fullWidth += w;
			sizes.push({width:w,height:winH});
			$(".gal_img_cont_"+i).css({
				width:w,
				height:winH
			});
			$(".galleryImage_"+i).attr("width",w);
			$(".galleryImage_"+i).attr("height",winH);
		}
		$("#images_cont").css({
			width:fullWidth,
			height:winH
		});
	};
	
	var hideContent = function(){
		if(isShowingContent == true){
			isShowingContent = false;
			$(".gallery_content").fadeOut(1000);
		}
	};
	
	var showContent = function(){
		if(isShowingContent == false){
			isShowingContent = true;
			$(".gallery_content").fadeIn(1000);
		}
	};
	var toggleArrowUp = function(dir,id) {
		if(dir == "up") {
			if(arrowUpShown == true) {
				arrowUpShown = false;
				$(".content .up img",posts[id]).fadeOut(600);
			}
		} else if(dir == "down") {
			if(arrowUpShown == false) {
				arrowUpShown = true;
				$(".content .up img",posts[id]).fadeIn(600);
			}
		}
	};
	
	var obj = {};
	obj.load = function(d) {
		data = d;
		var fullWidth = 0;
		var winW = $(window).width();
		var winH = $(window).height();
		var h;
		var w;
		for(var i=0;i<data.length;++i) {
			w = Math.round(winH*data[i].width/data[i].height);
			fullWidth += w;
			sizes.push({width:w,height:winH});
			$(".gal_img_cont_"+i).css({
				width:w,
				height:winH
			});
		}
		$("#images_cont").css({
			width:fullWidth,
			height:winH
		});
		loadImage(0);
		loadImage(1);
		$("#viewport").bind({
			scroll:windowScroll
		});
		$(window).bind({
			resize:resize,
			orientationchange:function(e) {
				resize();
			}
		});
	};
	obj.contentScrollDown = function() {
		$("#images_cont").bind("mouseup",function(){
			clearInterval(scrollLoop);
			$(this).unbind();
		});
		var viewH = $(".gallery_content .textView").height();
		var h = $(".gallery_content .textView .textBox").height();
		var dif = viewH-h;
		scrollLoop = setInterval(function(){
			var topDif = $(".gallery_content .textView .textBox").css("top");
			topDif = parseInt(topDif,0);
			if(topDif > dif) {
				$(".gallery_content .textView .textBox").css("top",topDif-15);
				toggleArrowUp('down',id);
			}
		},50);
	};
	
	obj.contentScrollUp = function() {
		$("#images_cont").bind("mouseup",function(){
			clearInterval(scrollLoop);
			$(this).unbind();
		});
		scrollLoop = setInterval(function(){
			var topDif = $(".gallery_content .textView .textBox").css("top");
			topDif = parseInt(topDif,0);
			if(topDif < 0) {
				$(".gallery_content .textView .textBox").css("top",topDif+15);
			} else {
				toggleArrowUp('up',id);
			}
		},50);
	};
	obj.followLink = function(link) {
		document.location.href = link;
	};
	obj.scrollLeft = function(){
		var dest = 0;
		if(currentImageIndex-1 >= 0) {
			for(var i=0;i<currentImageIndex-1;++i) {
				dest += sizes[i].width;
			}
			if(dest >= 0) {
				$("#viewport").animate({scrollLeft:dest});
				/*
				var cur = $(window).scrollLeft();
				var interval = setInterval(function(){
					if(cur >= dest) {
						window.scrollTo(cur,0);
						cur-=200;
					} else {
						window.scrollTo(dest,0);
						clearInterval(interval);
					}
				},15);
				*/
			}
		}
	};
	obj.scrollRight = function(){
		var dest = 0;
		if(currentImageIndex+1 < data.length) {
			//console.log(currentImageIndex);
			for(var i=0;i<currentImageIndex+1;++i) {
				dest += sizes[i].width;
			}
			//console.log(dest);
			var fw = $("#images_cont").width();
			if(dest < fw) {
				$("#viewport").animate({scrollLeft:dest});
				/*
				var cur = $(window).scrollLeft();
				//console.log("current scroll x: "+cur);
				var interval = setInterval(function(){
					if(cur <= dest) {
						window.scrollTo(cur,0);
						cur+=200;
					} else {
						window.scrollTo(dest,0);
						clearInterval(interval);
					}
				},15);
				*/
			}
		}
	};
	
	/****************** Modal Gallery Methods ******************/
	
	var galleries = [];
	var blankURL;
	var spinnerURL;
	var activeGallery;
	var thumbCreateIndex = 0;
	var numPages = 0;
	var currentPage = 1;
	obj.galleryOpen = false;
	
	var loadModal = function(id){
		thumbCreateIndex = 0;
		$("#modal_gallery .modalGalImgCont #gallery_main_image").attr("src", blankURL);
		$("#modal_gallery .modalGalImgCont #spinner").css("display","block");
		$("#modal_gallery .thumbbox").html("");
		
		activeGallery = galleries[id];
		
		var img = new Image();
		img.onload = function(){
			$("#modal_gallery .modalGalImgCont #spinner").css("display","none");
			$("#modal_gallery .modalGalImgCont #gallery_main_image").attr("src", this.src);
		};
		img.src = activeGallery[1][0].src;
	
		createThumb();
	};
	
	var createThumb = function() {
		if(thumbCreateIndex < activeGallery[1].length) {
			var thumb = document.createElement("div");
			thumb.className = "thumb";
			thumb.id = thumbCreateIndex;
			$(thumb).bind("click",function(){
				var i = $(this).attr("id");
				i = parseInt(i);
				Gallery.displayImage(i);
			});
			
			var spinner = new Image();
			spinner.src = spinnerURL;
			spinner.width = 30;
			spinner.height = 30;
			spinner.className = "spinner";
			$(thumb).append(spinner);
			
			var thumbImg = new Image();
			thumbImg.onload = function(){
				$(".spinner",thumb).css("display","none");
				this.width = 60;
				this.height = 60;
				$(thumb).append(this)
				thumbCreateIndex++;
				createThumb();
			};
			thumbImg.src = activeGallery[1][thumbCreateIndex].thumb;
			$("#modal_gallery .thumbbox").append(thumb);
		}
	};
	
	obj.modalInit = function(json) {
		galleries = json;
		//console.log(galleries);
		blankURL = $("#modal_gallery .modalGalImgCont #gallery_main_image").attr("src");
		spinnerURL = $("#modal_gallery .modalGalImgCont #spinner").attr("src");
		
	};
	obj.open = function(id) {
		var w = $(window).width();
		var h = $(window).height();
		var gw,gh,ih,ich;
		if(w <= 1024) {
			gw = 800;
			gh = 550;
			ih = 350;
			ich = 355;
		} else {
			gw = 910;
			gh = 628;
			ih = 408;
			ich = 410;
		}
		$("#modal_gallery").css({
			left:w*0.5-gw*0.5,
			top:h*0.5-gh*0.5,
			width:gw,
			height:gh
		});
		$("#modal_gallery #gallery_main_image").attr("height",ih);
		$("#modal_gallery .modalGalImgCont").css ("height",ich);
		
		numPages = galleries[id][1].length/10;
		var rem = galleries[id][1].length%10;
		if(rem > 0) {
			numPages = numPages-(rem/10);
			numPages++;
		}
		
		obj.galleryOpen = true;
		consoleLog("num pages: "+numPages);
		consoleLog("num images: "+galleries[id][1].length);
		
		if(typeof galleries[id] == "object") {
			$("#modal_gallery").fadeIn(500);
			loadModal(id);
		}
	};
	obj.close = function(id) {
		$("#modal_gallery").fadeOut(200);
		obj.galleryOpen = false;
	};
	obj.displayImage = function(id) {
		//console.log(id);
		$("#modal_gallery #spinner").css("display","block");
		$("#modal_gallery #gallery_main_image").css("display","none");
		var imgData = activeGallery[1][id];
		var img = new Image();
		img.onload = function(){
			$("#modal_gallery #spinner").css("display","none");
			$("#modal_gallery #gallery_main_image").attr("src",imgData.src);
			$("#modal_gallery #gallery_main_image").fadeIn(1000);
			activeGallery[0] = id;
		};
		img.src = imgData.src;
	};
	obj.nextImage = function(){
		var curIndex = activeGallery[0];
		
		if(curIndex < activeGallery[1].length-1) {
			consoleLog("adding an index");
			curIndex++;
		} else {
			consoleLog("set index to 0");
			curIndex = 0;
		}
		consoleLog("current index: "+curIndex);
		Gallery.displayImage(curIndex);
		consoleLog(curIndex/10,currentPage);
		if(curIndex/10 >= currentPage || curIndex == 0) {
			consoleLog("adding a page");
			consoleLog("numb pages: "+numPages); 
			if(currentPage < numPages) {
				$(".thumbbox").animate({
					left: '-=750'
				});
				currentPage++;
			} else {
				consoleLog("reset to 1");
				currentPage = 1;
				$(".thumbbox").css("left",0);
			}
			consoleLog("current Page: "+currentPage);
		}
	};
	obj.prevImage = function() {
		var curIndex = activeGallery[0];
		
		if(curIndex > 0) {
			consoleLog("minusing an index");
			curIndex--;
		} else {
			curIndex = activeGallery[1].length-1;
		}
		consoleLog("current index: "+curIndex);
		Gallery.displayImage(curIndex);
		consoleLog(curIndex/10,currentPage-1);
		if(curIndex/10 <= currentPage-1 || curIndex == activeGallery[1].length-1) {
			consoleLog("minusing a page");
			if(currentPage > 1) {
				$(".thumbbox").animate({
					left: '+=750'
				});
				currentPage--;
			} else {
				currentPage = numPages;
				var end = 750*(numPages-1);
				end = end*-1;
				$(".thumbbox").css("left",end);
			}
			consoleLog(currentPage);
		}
	};
	return obj;
})();
