// JavaScript Document

// create timer function
jQuery.timer = function (interval, callback) {
	var interval = interval || 100;
	if (!callback)
		return false;
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		this.internalCallback = function () {
			callback(self);
		};
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);	
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		var self = this;
	};
	return new _timer(interval, callback);
};

$(document).ready(function() {
	
	$("#submenu").css("display", "block");
	
	var j = 1;
	$("div.section").each(function() {
		$(this).attr("id", "section"+j).css("display","none");
		$("body").prepend("<a name=\"#panel" + j + "\"></a>");
		j++;
	});
	
	$("a.anchor").each(function() {
		$(this).click(function() {
			//$("div.temp").replaceWith($("div.temp").find("div.section").clone(true));
			
			$("div.temp").after($("div.temp").html());
			
			//$("div.temp").find("a").removeAttr("");
			
			$("div.temp").remove();
			
			$("a.anchor").each(function(){
				var src = $(this).find("img").attr("m_src");
				$(this).find("img").attr("src", src);
			});
			
			var chil_image = $(this).find("img");
			
			var new_src = chil_image.attr("src").split('.')[0] + "_active.gif";
			
			$(this).find("img").attr("src",new_src);
			
			$("a.active").removeClass("active");
			$(this).addClass("active");
			var id = $(this).attr("href");
			id = "#" + id.split('#')[1];
			
			_pi = id.charAt(6);
			
			if(_pi == "1")
			{
				_pi = '1';
			}
			
			$("div.section").hide().removeClass("first");
			$("div.start:visible").hide();
			if(id == "#start")
			{
				$("div.start").fadeIn();
				return;
			}
			id = "div" + id.replace("#panel", "#section");
			
			//alert(id);
			
			// ------------- CREATE CUSTOM SCROLL AND HIDE NATIVE SCROLL BAR
			
			wrapperHeight = $("#copy").height()-30;
			sectionHeight = $(id).height();
			if($("body").attr("id") != "shop")
			{
				sectionWidth = 380;
			} else {
				sectionWidth = 580;
			}
			
			
			$(id).wrap("<div class=\"temp\"></div>");
			
			$("#scrollBtns").remove();
			
			if (sectionHeight > wrapperHeight) {
				$(id).css({
					width: sectionWidth-30
				});
				
				
				
				$("#copy").prepend("<div id=\"scrollBtns\"><div id=\"scrollUp\"></div><div id=\"scrollDown\"></div></div>");
				
				maxScroll = sectionHeight - wrapperHeight+120;
				var s = 0;
				var direction = "";
				function doScroll() {
					$.timer(40, function (timer) {
						if (s >= 0 && s < maxScroll && direction == "up") {
							$("div.temp").scrollTop(s+10);
							s+=10;
						}
						else if (s > 0 && direction == "down") {
							$("div.temp").scrollTop(s-10);
							s-=10;
						}
						else if (direction = "stop") {
							$("div.temp").scrollTop(s);
							timer.stop();
						}
						else {
							$("div.temp").scrollTop(s);
							timer.stop();
						}
					});
				}
				
				$("#scrollDown").hover(function() {
					direction = "up";
					doScroll();
				}, function() {
					direction = "stop";
					doScroll();
				});
				$("#scrollUp").hover(function() {
					direction = "down";
					doScroll();
				}, function() {
					direction = "stop";
					doScroll();
				});
			} else {
				$(id).css({
					width: sectionWidth
				});
				
			}
			// ------------- END CUSTOM SCROLL
			
			
			$(id).addClass("first").fadeIn(300, function(){
				
				$("#content_wrapper").find("#image img").hide();
				
				$("#wine_"+_pi).fadeIn('200');
				
			
				
				
			});
			
			$(this).blur();
		});
	});
	
	$(".main_anchor").click(function(){
			var id = $(this).attr("href");
			id = "#" + id.split('#')[1];
			
			//alert(id);
			
			//id = "div" + id.replace("#panel", "#section");
		$("ul#winesMenu").fadeOut(400, function(){
			//$(id).show();
			
			$("div#content_wrapper").fadeIn(300, function(){
				$("a.anchor[href*=\"" + id + "\"]").click();
				
			});
		});
		

		
	});
	
	 var page = document.location.toString();
	if (page.match('#')) { // the URL contains an anchor
		// click the navigation item corresponding to the anchor
		
		
		
		var myAnchor = '#' + page.split('#')[1];
		
		
		$("ul#winesMenu").fadeOut(400, function(){
			//$(id).show();
			
			$("div#content_wrapper").fadeIn(300, function(){
				$("a.anchor[href*=\"" + myAnchor + "\"]").click();
				
			});
		});
		
	} else {
		$("a#menu0").click();
	}



	
	
	$("ul.gallery_list").each(function(i){
		$(this).addClass("car" + i);
	});
	
	$("ul.gallery_list li").click(function(){
		var _s = $(this).find("img").attr("alt") + "/570/680";
		
		var _im = $(".img_contain").find("img").fadeOut("slow",function(){
			$(this).remove();
			var i = new Image(); 
			i.src = _s;
			i.style.display = "none";
			i.style.margin = "auto";
			i.onload = function(){
				var _w = i.width;
				var _h = i.height;
				
			
				if(_w > 430)
				{
					_left = -(_w - 430)/2;
				} else {
					_left = 0;
				}
				
				_top =-(_h - 380)/2;
				
				 // alert ( _w + " x " + _h +"\n" + _left +" x " + _top);
				
				i.style.position = "relative";
				i.style.top = _top + "px";
				i.style.left = _left + "px";
				
				$(i).fadeIn("slow");
			}; 
		    $(".img_contain").append(i);
		});	
		
		
		
		//alert(_s);
	});
	
	$("a.galleryAnchor").click(function(e){
		e.preventDefault();
		
		var _p = $(this).attr("href");
		
		var _c = _p.split('#')[1];
		
		
		$("ul.active_gal").slideUp(300,function(){
			$("ul.car"+ _c).slideDown(300).addClass("active_gal");
		}).removeClass("active_gal");
		
	
		
		$(this).blur();
	});
	
	
	$(".about_movr").click(function(){
		
		var _id = $(this).attr("href");
		
		$("#start_here").fadeOut("fast", function(){
			
			$(_id).fadeIn();
			
			$("#image img:visible").fadeOut("fast",function(){
				$(_id+"_img").fadeIn();
			});
			
		});
		
		return false;
	})
	
	$(".about_back").click(function(){
		
		$(".about_containr:visible").fadeOut("fast",function(){
			
			$("#start_here").fadeIn();
			
			$("#image img:visible").fadeOut("fast",function(){
				$("#about_start_img").fadeIn();
			});
			
		});
		
		return false;
		
	});
	
	if($("#shorty"))
	{
		if($("#tall").height() > 377)
		{
			$("#scrollBtns").show();
			
			_ht = $("#tall").height() + 81;
			
			maxScroll = _ht - 377;
			
			//$("#debug").html("Tall Ht: " + _ht + "  MAX: " + maxScroll);
			
			var s = 0;
			var direction = "";
			
			var _step = 0;
			
			function doScrollNew() {
				
			$.timer(40, function (timer) {
				
				_step = _step + 1;
				//$("#debug").html("Tall Ht: " + _ht + "  MAX: " + maxScroll + " -> NEW { Current: " + s + " - Step: "+ _step);
				
				if (s >= 0 && s < maxScroll && direction == "up") {
					$("#shorty").scrollTop(s+10);
					s+=10;
				}else if (s > 0 && direction == "down") {
					$("#shorty").scrollTop(s-10);
					s-=10;
				}else if (direction == "stop") {
					$("#shorty").scrollTop(s);
					timer.stop();
				}else {
					$("#shorty").scrollTop(s);
					timer.stop();
				}
			});
			}
			
			$("#scrollDown").hover(function() {
				direction = "up";
				doScrollNew();
			}, function() {
				direction = "stop";
				doScrollNew();
			});
			$("#scrollUp").hover(function() {
				direction = "down";
				doScrollNew();
			}, function() {
				direction = "stop";
				doScrollNew();
			});
		} else {
			
			//$("#debug").html("nope");
		}
		
	}	

	
	$("#follow").click(function(){
		
		if($(this).hasClass("close"))
		{
			
			$("#follow_container").fadeOut();
			
			$(this).removeClass("close");
			
			return false;
			
		}
		
		$(".socContainer:visible").fadeOut("fast",function(){
			
			$(".switcher").removeClass("close");
			
		});
		
		var _this = $(this);
		
		$("#follow_container").fadeIn("slow",function(){
			
			_this.addClass("close");
			
		});
		
		
		
		return false;
		
	});

	
	$("#share").click(function(){
		
		if($(this).hasClass("close"))
		{
			
			$("#share_container").fadeOut();
			
			$(this).removeClass("close");
			
			return false;
			
		}
		
		$(".socContainer:visible").fadeOut("fast",function(){
			
			$(".switcher").removeClass("close");
			
		});
		
		var _this = $(this);
		
		$("#share_container").fadeIn("slow",function(){
			_this.addClass("close");
		});
		
		
		
		return false;
		
	});
	
	
	
});