
$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


NEWS = {};


NEWS.init = function() {
	$("a[rel*='external']").live("click", function(e) {
		e.preventDefault();
		var href = $(this).attr("href");
		window.open(href);
	});
	
	$("form#contactForm").clearForm();
	
	$('.autoGrow').elastic();
	
	$.localScroll();
}

NEWS.scrollToWork = function (location, navArray) {
	var thumbnailVP = $("#thumbnailViewport");
	var descriptionVP = $("#descriptionViewport");
	var prevBtn = $("#workControls").find(".workPrev");
	var nextBtn = $("#workControls").find(".workNext");
	descriptionVP.stop().animate({opacity: 0.25,filter: "alpha(opacity=25)"}, 200, function() {
		$(this).scrollTo("#" + location + "-description", 200).animate({opacity: 1,filter: "alpha(opacity=100)"}, 500);
	});
	
	$("#workControls").find("li").removeClass("inactive");
	if ($.inArray(location, navArray) == 0) {
		NEWS.disableWorkNavButton("prev");
	} else if ($.inArray(location, navArray) == navArray.length-1) {
		NEWS.disableWorkNavButton("next");
	}
}

NEWS.updateThumbIndicator = function (nextThumb) {

	$(".thumbIndicator").html("");
	$("#" + nextThumb).closest(".clientThumbnails").find("li.featuredThumbnail").each(function() {
		if (nextThumb == $(this).attr("id")) {
			thumbClass = "active";
		} else {
			thumbClass = "inActive";
		}
		$(".thumbIndicator").append("<li class='" + thumbClass + "'><a href='#' class='jumpToThumb' rel='" + $(this).attr('id') + "'>Thumbnail</a></li>")
	});
}

NEWS.disableWorkNavButton = function(button) {
	switch (button) {
		case "prev": {
			$("#workControls").find(".workPrev").addClass("inactive");	
			break;
		}
		case "next": {
			$("#workControls").find(".workNext").addClass("inactive");		
			break;
		}
	}
}

NEWS.scrollArray = function() {
	var arrscrollNav = [];

	$("#listOfscrolls > li.scrollContainer").each(function() {
		arrscrollNav.push($(this).attr("id"));
	});
	
	return arrscrollNav;
}

$(document).ready(function() {

	NEWS.init();
		
	$("ul#navBar > li").bind("click", function(e) {

		$("ul#navBar").find("li").removeClass("active");

		$(this).addClass("active");
	});
	

	$(function() {
		$(".fieldContainer > input").bind("focus", function() {
			$(this).next("small").fadeOut("fast");		
		}).bind("blur", function() {
			if ($(this).val() == "") {
				$(this).next("small").fadeIn("fast");
			}
		});
	}); 
	
	
	$(function () {
		var thumbnailVP = $("#thumbnailViewport");
		var descriptionVP = $("#descriptionViewport");
		var arrWorkNav = [];

		descriptionVP.find("li.featuredDescription").each(function() {
			arrWorkNav.push($(this).attr("id").replace("-description", ""));
		});
		
		if (arrWorkNav.length) {

			if (location.hash == "") {
				location.hash = arrWorkNav[0];
			}

			NEWS.scrollToWork(location.hash.replace("#", ""), arrWorkNav);

			NEWS.updateThumbIndicator(location.hash.replace("#", "") + "-thumbnail-1");
		}
		
		$("a.scrollTo").bind("click", function(e) {
			e.preventDefault();

			var scrollLocation = $(this).attr("rel");
			

			if (scrollLocation == "next" || scrollLocation == "prev") {

				var currentLocation = $.inArray(location.hash.replace("#",""), arrWorkNav);
								
				switch (scrollLocation) {
					case "next": {
						if (currentLocation >= arrWorkNav.length - 1) {
							scrollLocation = 0;
						} else {
							scrollLocation = arrWorkNav[currentLocation + 1]
						}						
						break;
					}
					case "prev": {
						if (currentLocation <= 0) {
							scrollLocation = 0;
						} else {
							scrollLocation = arrWorkNav[currentLocation - 1]
						}						
						break;
					}					
				}				
			}
			
			if (scrollLocation) {

				location.hash = scrollLocation;
				

				NEWS.scrollToWork(scrollLocation, arrWorkNav);	
				

				NEWS.updateThumbIndicator(scrollLocation + "-thumbnail-1");
							
			}
		}).bind("mousedown", function() {
			$(this).closest("li").addClass("click");
		}).bind("mouseup", function() {
			$(this).closest("li").removeClass("click");
		});
		

		$("ul.thumbControls > li > a").bind("click", function(e) {
			e.preventDefault();
			var currentThumb = $(this).closest(".featuredThumbnail").attr("id");
			var arrCurrentThumb = currentThumb.split("-");
			var currPos = Number(arrCurrentThumb[arrCurrentThumb.length-1]);
			
		
 			if ($(this).closest("li").hasClass("thumbPrev")) {
				arrCurrentThumb[arrCurrentThumb.length-1] = currPos-1;
			} else if ($(this).closest("li").hasClass("thumbNext")) {
 				arrCurrentThumb[arrCurrentThumb.length-1] = currPos+1;
 			} 			
 			

 			var nextThumb = arrCurrentThumb.join("-");
 			

 			if ($("#" + nextThumb).length) { 			
	 			thumbnailVP.stop().scrollTo("#" + nextThumb, 200);
 			}
 			
			NEWS.updateThumbIndicator(nextThumb);
		});
		
		$("ul.clientThumbnails > li > a").bind("click", function(e) {
			e.preventDefault();
			var currentThumb = $(this).closest(".featuredThumbnail").attr("id");
			var arrCurrentThumb = currentThumb.split("-");
			var currPos = Number(arrCurrentThumb[arrCurrentThumb.length-1]);
			

 			arrCurrentThumb[arrCurrentThumb.length-1] = currPos+1;
 			

 			var nextThumb = arrCurrentThumb.join("-");
 			

 			if ($("#" + nextThumb).length) { 			
	 			thumbnailVP.stop().scrollTo("#" + nextThumb, 200);
 			}
 			
			NEWS.updateThumbIndicator(nextThumb);
		});
		
		$("a.jumpToThumb").live("click", function(e) {
			e.preventDefault();
			
			var theThumb = $(this).attr("rel");
			
 			if ($("#" + theThumb).length) { 	
	 			thumbnailVP.stop().scrollTo("#" + theThumb, 200);
				
				NEWS.updateThumbIndicator(theThumb);
			}
		});
		
	});
	

	$(function () {
		var scrollVP = $("#footerscrollsViewport");
		arrscrollNav = NEWS.scrollArray();
				
		thisscrollID = arrscrollNav[0];
		
		$("ul#scrollControls > li").bind("click", function(e) {
			e.preventDefault();

			var thisPos = $.inArray(thisscrollID, arrscrollNav);
			
			if ($(this).hasClass("scrollPrev")) {
				nextPos = thisPos - 1;				
			} else {
				nextPos = thisPos + 1;
			}
			nextID = arrscrollNav[nextPos];
			

			if ($("#listOfscrolls > #" + nextID).length) {
				$("ul#scrollControls > li").removeClass("inactive");
				scrollVP.scrollTo("#" + nextID, 200);
				thisscrollID = nextID;
			}
			
			if (nextPos == arrscrollNav.length-1) {
				$("ul#scrollControls > li.scrollNext").addClass("inactive");
			} else if (nextPos == 0) {
				$("ul#scrollControls > li.scrollPrev").addClass("inactive");
			}
		}).bind("mousedown", function() {
			$(this).closest("li").addClass("click");
		}).bind("mouseup", function() {
			$(this).closest("li").removeClass("click")
		});
		
	});
	
	$(function() {
		$(".").each(function() {
			var thisUsername = $(this).attr("class").replace(" ", "");
			var scrollContainer = $(this);
			var loadscrolls = scrollContainer.find(".loadscrolls");
			var flushscrolls = $.getUrlVar("flushscrolls");
			if (flushscrolls != 1) {
				flushscrolls = 0;
			}
			
			$.getJSON("get-scrolls.cfm?username=" + thisUsername + "&flushscrolls=" + flushscrolls, function(data) {
				if (data.length && loadscrolls.length) {
					loadscrolls.remove();
					
					$.each(data, function(i,item) {
						if (scrollContainer.attr("id") == "listOfscrolls") {
							scrollContainer.append("<li class='scrollContainer' id='" + item.ID + "'>" + item.scroll + "<small class='darkGray'>" + item.DATE + "</small></li>");
						} else {
							scrollContainer.append("<p class='scrollContainer'>" + item.scroll + "</p><small class='darkGray'>" + item.DATE + "</small>");
							if (i == 1) return false;
						}
					});
					
					arrscrollNav = NEWS.scrollArray();
					thisscrollID = arrscrollNav[0];
				}
			});
		});
	});
	
});

