var Page = (function(){
	var posts;
	var data;
	var sizes = [];
	var avgW;
	var scrollLoop;
	var arrowUpShown = false;
	var currentPageIndex = 0;
	var rightArrowShown = true;
	var leftArrowShown = false;
	
	var resize = function(e) {
		sizes = [];
		var fullWidth = 0;
		var winW = $(window).width();
		var winH = $(window).height();
		var h,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});
		}
		posts.each(function(i){
			$(this).css({
				width:sizes[i].width,
				height:winH
			});
			$("img.bgImg",posts[i]).attr("width",sizes[i].width);
			$("img.bgImg",posts[i]).attr("height",sizes[i].height);
			var ch = $(".textView", this).height();
			var cl;
			switch(data[i].position) {
				case "left":
					if(winW > 1024) {
						cl = 210;
					} else {
						cl = 160;
					}
					break;
				case "right":
					if(winW < sizes[i].width) {
						$(".content",posts[i]).css({
							right:sizes[i].width-winW+50
						});
					}
					
					if(winW > 1024) {
						cl = 210;
					} else {
						cl = 160;
					}
					break;
				case "middle":
					if(winW < sizes[i].width) {
						$(".content",this).css({
							left:winW*0.5-242
						});
					} else {
						$(".content",this).css({
							left:sizes[i].width*0.5-242
						});
					}
					if(winW > 1024) {
						cl = 210;
					} else {
						cl = 160;
					}
					break;
				case "bottom":
					var bcw = (winW < 1024) ? 500 : 800;
					if(sizes[i].width < bcw) {
						$(".content",this).css ({
							width:sizes[i].width-80
						});
					}
					if(winW < sizes[i].width) {
						$(".content",this).css({
							left:winW*0.5-$(".content",this).width()*0.5
						});
					} else {
						$(".content",this).css({
							left:sizes[i].width*0.5-$(".content",this).width()*0.5
						});
					}
					if(winW > 1024) {
						cl = 130;
					} else {
						cl = 80;
					}
					break;
				default:
					break;
			}
			if(ch == cl) {
				$(".scrollBtn", this).fadeIn();
			}
		});
		$("#main_content").css({
			width:fullWidth,
			height:winH
		});
		var clientCont = $(".client_content");
		if(clientCont) {
			clientCont.css({
				left:winW*0.5-460,
				top:160
			});
		}
		if(typeof Gallery != "undefined") {
			if(Gallery.galleryOpen == true) {
				var gw,gh,ih,ich;
				if(winW <= 1024) {
					gw = 800;
					gh = 550;
					ih = 350;
					ich = 355;
				} else {
					gw = 910;
					gh = 628;
					ih = 408;
					ich = 410;
				}
				$("#modal_gallery").css({
					left:winW*0.5-gw*0.5,
					top:winH*0.5-gh*0.5,
					width:gw,
					height:gh
				});
				$("#modal_gallery #gallery_main_image").attr("height",ih);
				$("#modal_gallery .modalGalImgCont").css ("height",ich);
			}
		}
		
	};
	
	var loadImage = function(index) {
		consoleLog(index);
		if(index < data.length) {
			if(data[index].loaded == false) {
				var img = new Image();
				img.onload = function(){
					$("img.spinner",posts[index]).css("display","none");
					$("img.bgImg",posts[index]).attr("width",sizes[index].width);
					$("img.bgImg",posts[index]).attr("height",sizes[index].height);
					$("img.bgImg",posts[index]).attr("src",this.src);
					$("img.bgImg",posts[index]).fadeIn(1000);
					$(".content",posts[index]).fadeIn(1000, function(){
						var ch = $(".textView", this).height();
						var cl;
						switch(data[index].position) {
							case "left":
							case "right":
							case "middle":
								if($(window).width() > 1024)
								cl = 210;
								else
								cl = 160;
								break;
							case "bottom":
								if($(window).width() > 1024)
								cl = 130;
								else
								cl = 80;
								break;
						}
						
						if(ch == cl) {
							$(".scrollBtn", this).fadeIn();
						}
					});
					data[index].loaded = true;
				};
				img.src = data[index].src;
			}
		}
	};
	
	var windowScroll = function(e) {
		var ww = $("#viewport").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<currentPageIndex+1;++k) {
			cw += sizes[k].width;
		}

		if(sx >= cw && cw > 0) {
			if(currentPageIndex < sizes.length-1)
				currentPageIndex++;
		} else if(sx < cw-sizes[currentPageIndex].width) {
			if(currentPageIndex > 0)
				currentPageIndex--;
		}
		var contW = $("#main_content").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 index = $(window).scrollLeft() + avgW;
		index = Math.floor(index/avgW);
		//console.log(e.currentTarget);
		if(index < data.length) {
			loadImage(index);
		}
		*/
	};
	
	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;
		posts = $(".post");
		var fullWidth = 0;
		var winW = $(window).width();
		var winH = $(window).height();
		var h,w;
		
		//if(posts.length > 1) {
			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});
			}
		/*
		} else {
			if(winW > winH) {
				h = Math.round(winW*data[0].height/data[0].width);
				w = winW;
			} else {
				w = Math.round(winH*data[0].width/data[0].height);
				h = winH;
			}
			fullWidth = w;
			sizes.push({width:w,height:h});
		}
		*/
		posts.each(function(i){
			$(this).css({
				width:sizes[i].width,
				height:winH
			});
		
			switch(data[i].position) {
				case "left":
					break;
				case "right":
					if(winW < sizes[i].width) {
						$(".content",posts[i]).css({
							right:sizes[i].width-winW+50
						});
					}
					break;
				case "middle":
					if(winW < sizes[i].width) {
						$(".content",this).css({
							left:winW*0.5-242
						});
					} else {
						$(".content",this).css({
							left:sizes[i].width*0.5-242
						});
					}
					break;
				case "bottom":
					var bcw = (winW < 1024) ? 500 : 800;
					if(sizes[i].width < bcw) {
						$(".content",this).css ({
							width:sizes[i].width-80
						});
					}
					if(winW < sizes[i].width) {
						$(".content",this).css({
							left:winW*0.5-$(".content",this).width()*0.5
						});
					} else {
						$(".content",this).css({
							left:sizes[i].width*0.5-$(".content",this).width()*0.5
						});
					}
					break;
				default:
					break;
			}
		});
		$("#main_content").css({
			width:fullWidth,
			height:winH
		});
		$("#viewport").bind({
			scroll:windowScroll
		});
		$(window).bind({
			resize:resize,
			orientationchange:function() {
				resize();
			}
		});
		
		var clientCont = $(".client_content");
		if(clientCont) {
			clientCont.css({
				left:winW*0.5-460,
				top:160
			});
		}
		
		loadImage(0);
		loadImage(1);
	};
	
	obj.scrollLeft = function() {
		var dest = 0;
		if(currentPageIndex-1 >= 0) {
			for(var i=0;i<currentPageIndex-1;++i) {
				dest += sizes[i].width;
			}
			if(dest >= 0) {
				$("#viewport").animate({scrollLeft:dest});
			}
		}
	};
	
	obj.scrollRight = function(){
		var dest = 0;
		if(currentPageIndex+1 < data.length) {
			for(var i=0;i<currentPageIndex+1;++i) {
				dest += sizes[i].width;
			}
			
			var fw = $("#main_content").width();
			if(dest < fw) {
				$("#viewport").animate({scrollLeft:dest});
			}
		}
	};
	
	obj.contentScrollDown = function(id) {
		$(posts[id]).bind("mouseup",function(){
			clearInterval(scrollLoop);
			$(this).unbind();
		});
		var viewH = $(".content .textView",posts[id]).height();
		var h = $(".content .textView .textBox",posts[id]).height();
		var dif = viewH-h;
		scrollLoop = setInterval(function(){
			var topDif = $(".content .textView .textBox",posts[id]).css("marginTop");
			topDif = parseInt(topDif,0);
			if(topDif > dif) {
				$(".content .textView .textBox",posts[id]).css("marginTop",topDif-15);
				toggleArrowUp('down',id);
			}
		},50);
	};
	
	obj.contentScrollUp = function(id) {
		$(posts[id]).bind("mouseup",function(){
			clearInterval(scrollLoop);
			$(this).unbind();
		});
		scrollLoop = setInterval(function(){
			var topDif = $(".content .textView .textBox",posts[id]).css("marginTop");
			topDif = parseInt(topDif,0);
			if(topDif < 0) {
				$(".content .textView .textBox",posts[id]).css("marginTop",topDif+15);
			} else {
				toggleArrowUp('up',id);
			}
		},50);
	};
	obj.followLink = function(link) {
		document.location.href = link;
	};
	
	return obj;
})();
