// events on page load
$(function() {

});

function slideShow() {
	// HOME PAGE SLIDESHOW CODE //
	// set first slide as selected
	$(".slideshow ul li:first").addClass("selected");
	$(".slide-nav a:first").addClass("selected");
	
	
	// generate slide nav buttons
	$(".slideshow ul:first > li").each(function(index) {
		$(".slide-nav").append('<a href=""></a><div class="preview"><img src="/images/slides/preview/' + (index + 1) + '.jpg" alt=""/></div>');
	});
	$(".slideshow-wrapper .slide-nav a:first").addClass("selected");
	
	
	// show paging buttons on mouseover
	$(".slideshow").mouseover(function() {
		$(".slideshow > a.next").stop(true, true).fadeIn(250);
		$(".slideshow > a.prev").stop(true, true).fadeIn(250);
	});
	$(".slideshow").mouseleave(function() {
		$(".slideshow > a.next").stop(true, true).fadeOut(250);
		$(".slideshow > a.prev").stop(true, true).fadeOut(250);
	});
	
	
	// show preview when hover over slide nav
	$(".slide-nav a").mouseover(function() {
		position = $(this).position();
		$(this).next().css("left", (position.left - 67) + "px").show();
	});
	$(".slide-nav a").mouseleave(function() {
		$(this).next().hide();
	});

	
	// AUTO CHANGE SLIDE
	var t;
	var timer_is_on = 0;
	var getTimer;
	var isFirstTime = true;
	
	// starts the auto changer
	doTimer();
	
	function autoChangeSlide() {
		if (!isFirstTime) {
			var slideLength = $(".slideshow ul:first > li").length;
	
			// determine the index of the selected slide
			$(".slideshow ul:first > li").each(function(index) {
				if ($(this).hasClass("selected")) {
					thisSlide = index;
				}
			});
			
			if (thisSlide + 1 > slideLength - 1)
				goToSlide(0);
			else
				goToSlide(thisSlide + 1);
		}
		
		isFirstTime = false;
			
		t = setTimeout(function() {
			autoChangeSlide();
		}, 10000);
	}

	// starts the javascript timer
	function doTimer() {
		if (!timer_is_on) {
			timer_is_on = 1;
			autoChangeSlide();
		}
	}

	// stops the javascript timer
	function stopCount() {
		clearTimeout(t);
		timer_is_on = 0;
	}

	
	// EVENT HANDLERS
	// click next
	$(".slideshow > a.next").click(function() {
		stopCount(); //stop auto change slides
		var slideLength = $(".slideshow ul:first > li").length;
	
		// determine the index of the selected slide
		$(".slideshow ul:first > li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		if (thisSlide + 1 > slideLength - 1)
			goToSlide(0);
		else
			goToSlide(thisSlide + 1);
	});
	
	
	// click prev
	$(".slideshow > a.prev").click(function() {
		stopCount(); //stop auto change slides
		var slideLength = $(".slideshow ul:first > li").length;
	
		// determine the index of the selected slide
		$(".slideshow ul:first > li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		if (thisSlide - 1 < 0)
			goToSlide(slideLength - 1);
		else
			goToSlide(thisSlide - 1);
	});
	
	
	// slide nav click
	$(".slide-nav > a").click(function() {
		stopCount(); //stop auto change slides
		var slideIndex = $(".slide-nav > a").index(this);
		goToSlide(slideIndex);
		return false;
	});
					

	// HELPER FUNCTIONS
	// function to go to a specific slide index
	function goToSlide(index) {
		var thisSlide = 0;
		
		// determine the index of the selected slide
		$(".slideshow ul:first > li").each(function(index) {
			if ($(this).hasClass("selected")) {
				thisSlide = index;
			}
		});
		
		// remove selected class from current slide.
		$(".slideshow ul:first > li").eq(thisSlide).removeClass("selected");
		$(".slide-nav a").eq(thisSlide).removeClass("selected");
		
		// add selected class to next slide.
		$(".slideshow ul:first > li").eq(index).addClass("selected");
		$(".slide-nav a").eq(index).addClass("selected");
		
		// adds a custom trigger event which is then bound to each slide
		$(".slideshow ul:first > li").trigger("loadSlide", [index]);
		
		//alert("index: " + index + "\nthisSlide: " + thisSlide);
		var moveDistance = index * -1000;				
		$(".slideshow ul:first").animate({left: moveDistance}, 500);
	}
}




function workGallery(photoPath, firstImage) {
	// PHOTO GALLERY	
	// Arguments: 
	// "photoPath" is the path to the folder where the images are. Make sure to end the path with a forward slash.
	// "firstImage" is the index of first selected image
	// This gallery uses three photos to run; a thumbnail image, a medium size image for the viewer, and a full size image for popup. Name all three photos the same and append -thumb and -full to the corresponding filenames.
	// example: workGallery("/images/work/wpl/", 0);
	

	// path to the folder where the images are. make sure to end the path with a forward slash
	var imgPath = photoPath;
	
	// index of first selected image
	var imgIndex = firstImage;
	
	// create array of thumbnails
	var thumbnails = $(".thumbnails > a");
	
	
	// hide thumbnails if there is only 1 photo
	if (thumbnails.length <= 1) {
		$(".thumbnails, .sidebar .next, .sidebar .prev").hide();
	}
	
	
	// display first image in the viewer
	var firstImg = $(".thumbnails > a:first").attr("href");
	var imgTitle = $(".thumbnails > a:first").attr("title");
	displayImg(imgPath + firstImg, imgTitle);
	
	
	// enable fancybox on viewer
	$("a.view").fancybox({
		'transitionIn' : 'fade',
		'transitionOut' : 'fade',
		'speedIn' : 400, 
		'speedOut' : 200, 
		'overlayOpacity' : 0.7,
		'overlayColor' : '#333',
		'autoScale' : false,
		'hideOnContentClick' : true,
		'titleShow' : true,
		'titlePosition' : 'outside'
	});
		

	// generate thumbnails
	$(".thumbnails > a").each(function() {
		imgName = $(this).attr("href");
		imgExt = imgName.substring(imgName.lastIndexOf("."), imgName.lastIndexOf(".") + 4);
		imgName = imgName.replace(imgExt, "");
		
		imgThumb = new Image();
		$(imgThumb).attr("src", imgPath + imgName + "-thumb" + imgExt).attr("alt", "");
		$(this).html(imgThumb);
	});
	
	
	// set first thumbnail as selected
	$(".thumbnails > a:first").addClass("selected"); 
	
	
	// EVENT HANDLERS //
	// viewer click event
	$(".view").click(function() {
		return false; // stops href from executing
	});
	
	
	// next thumbnail click
	$(".sidebar > a.next").click(function() {
		if (($(".thumbnails > a.selected").index() + 1) < $(".thumbnails > a").size()) {
			imgName = $(".thumbnails > a.selected").next().attr("href");
			imgIndex = $(".thumbnails > a.selected").next().index();
			imgTitle = $(".thumbnails > a.selected").next().attr("title");
		}
		else {
			imgName = $(".thumbnails > a:first").attr("href");
			imgIndex = $(".thumbnails > a:first").index();
			imgTitle = $(".thumbnails > a:first").attr("title");
		}
		
		displayImg(imgPath + imgName, imgTitle);
		return false; // stops href from executing
	});
	
	$(".sidebar > a.prev").click(function() {
		if (($(".thumbnails > a.selected").index() - 1) >= 0) {
			imgName = $(".thumbnails > a.selected").prev().attr("href");
			imgIndex = $(".thumbnails > a.selected").prev().index();
			imgTitle = $(".thumbnails > a.selected").prev().attr("title");
		}
		else {
			imgName = $(".thumbnails > a:last").attr("href");
			imgIndex = $(".thumbnails > a:last").index();
			imgTitle = $(".thumbnails > a:last").attr("title");
		}
		
		displayImg(imgPath + imgName, imgTitle);
		return false; // stops href from executing
	});
				
	
	// thumbnail click event
	$(".thumbnails > a").click(function() {
		imgIndex = $(this).index();
		imgName = $(this).attr("href");
		imgTitle = $(this).attr("title");
		displayImg(imgPath + imgName, imgTitle);
		return false; // stops href from executing
	});
				
	
	// FUNCTIONS //
	// displays an image in the viewer
	function displayImg(imgURL, imgTitle) {
		// show spinner and load image
		$("a.view > img").fadeTo(0, 0.4);
		$(".overlay").fadeTo(0, 0.2);
		var img = new Image();
		$(img).load(function() {
			$(img).attr("alt", imgTitle);
			imgName = $(".thumbnails > a.selected").attr("href");
			imgExt = imgName.substring(imgName.lastIndexOf("."), imgName.lastIndexOf(".") + 4);
			imgName = imgName.replace(imgExt, "");
			$("a.view").attr("href", imgPath + imgName + "-full" + imgExt);
			$("a.view").attr("title", imgTitle);						
			$("a.view > img").remove();
			$("a.view").append(img);
			$(".overlay").hide();
			$("a.view img").fadeTo(0, 1);				
		}).attr("src", imgURL);
		
		
		// change which thumbnail is selected
		$(".thumbnails > a").removeClass("selected");
		$(".thumbnails > a").eq(imgIndex).addClass("selected");
		
		// update 'next' photo button
		if (($(".thumbnails > a.selected").index() + 1) < $(".thumbnails > a").size())
			imgName = $(".thumbnails > a.selected").next().attr("href");
		else
			imgName = $(".thumbnails a:first").attr("href");			
		imgExt = imgName.substring(imgName.lastIndexOf("."), imgName.lastIndexOf(".") + 4);
		imgName = imgName.replace(imgExt, "");
		$("a.next > img").attr("src", imgPath + imgName + "-thumb" + imgExt);
	}
}




function workThumbnails() {
	// fade in subnav
	$("ul.subnav").show();

	// subnav fade in/fade out effect
	$("ul.subnav li").mouseenter(function() {
		$(this).find("a").fadeIn(200);
	})
	.find("a").mouseout(function() {
		if (!$(this).hasClass("selected")) {
			$(this).stop(false, true);
			$(this).fadeOut(100);
		}
	});
	
	
	// subnav filtering
	var currentTag = "all";
	var noOfPieces = $(".piece").size();
	$(".content").css("height", Math.ceil(noOfPieces / 3) * 230);
	
	$("ul.subnav li a").click(function() {
		var tag = $(this).parent("li").attr("class");
		if (!$(this).hasClass("selected")) {
			$("ul.subnav li a.selected").fadeOut(100);
			$("ul.subnav li a").removeClass("selected");
			$(this).addClass("selected");
		}
		
		switch(tag) {
			case "subnav-all":
				filterWork("all");
				currentTag = "all";
				break;
			
			case "subnav-websites":
				filterWork("website");
				currentTag = "website";
				break;
				
			case "subnav-print":
				filterWork("print");
				currentTag = "print";
				break;
				
			case "subnav-logos":
				filterWork("logo");
				currentTag = "logo";
				break;
			
			default:
				filterWork("all");
				currentTag = "all";
		}
	});
	
	function filterWork(tag) {
		if (tag != currentTag) {
			if (tag == "all") {
				$(".piece").fadeOut(200);
				noOfPieces = $(".piece").size();
				$(".content").delay(200).animate({height: (Math.ceil(noOfPieces / 3) * 230) + "px"}, 200);
				$(".piece").delay(200).fadeIn(200);
			}
			else {
				$(".piece").fadeOut(200);
				noOfPieces = $(".piece." + tag).size();
				$(".content").delay(200).animate({height: (Math.ceil(noOfPieces / 3) * 230) + "px"}, 200);
				$(".piece." + tag).delay(200).fadeIn(200);
			}
		}
	}
	
	
	// work piece hover effects
	$(".piece .overlay").mouseover(function() {
		$(this).prev().animate({top: '-18px'}, 500, "easeOutElastic");
	});
	$(".piece .overlay").mouseleave(function() {
		$(this).prev().stop(true); // this fixes a small delay before firing the mouseleave animation
		$(this).prev().animate({top: '0px'}, 100, "linear");
	});
}


function removeSpaces(s) {
	var string = s;
	string = string.replace(/ /g, "");
	return string;
}

function validateContact() {
	var isValid = true;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	var txtName = $("[name=txtName]");
	var txtPhone = $("[name=txtPhone]");
	var txtEmail = $("[name=txtEmail]");
	var txtMessage = $("[name=txtMessage]");
	
	var fields = new Array(txtName, txtEmail, txtMessage);
	
	// run validation
	for (i = 0; i < fields.length; i++) {
		var string = removeSpaces(fields[i].val());
		
		if (string.length < 1 || string == null) {
			fields[i].addClass("invalid")
			if (fields[i].next().is(":visible"))
				fields[i].next().effect("highlight", {color: "#E87575"}, 1500);
			else
				fields[i].next().slideDown(500);
			isValid = false;
		}
		else if (fields[i].attr("name") == "txtEmail" && !emailReg.test(string)) {
			if (fields[i].next().is(":visible"))
				fields[i].next().effect("highlight", {color: "#E87575"}, 1500);
			else
				fields[i].next().slideDown(500);
			isValid = false;
		}
		else {
			fields[i].removeClass("invalid").next().hide();
		}
	}
	
	return isValid;
}



// makes content and sidebar the same height
function equalizeContent() {
	mainHeight = $(".wrapper > div.main").height();
	sidebarHeight = $(".wrapper > div.sidebar").height();
	
	if (mainHeight > sidebarHeight) {
		padding = parseInt($(".wrapper > div.sidebar").css("padding-top")) + parseInt($(".wrapper > div.sidebar").css("padding-bottom"));
		$(".wrapper > div.sidebar").height(mainHeight - padding);
		//alert("mainHeight > sidebarHeight\nmainHeight: " + mainHeight + "\nsidebarHeight: " + sidebarHeight);
	}
		
	if (sidebarHeight > mainHeight) {
		padding = parseInt($(".wrapper > div.main").css("padding-top")) + parseInt($(".wrapper > div.main").css("padding-bottom"));
		$(".wrapper > div.main").height(sidebarHeight - padding);
		//alert("sidebarHeight > mainHeight\nmainHeight: " + mainHeight + "\nsidebarHeight: " + sidebarHeight);
	}
}



// twitter feed
function loadTwitter() {
	new TWTR.Widget({
		version: 2,
		type: 'profile',
		rpp: 1,
		interval: 6000,
		width: 805,
		height: 40,
		theme: {
			shell: {
				background: 'transparent',
				color: '#CCC'
			},
			tweets: {
				background: 'transparent',
				color: '#CCC',
				links: '#FFF'
			}
		},
		features: {
			scrollbar: false,
			loop: false,
			live: false,
			hashtags: false,
			timestamp: false,
			avatars: false,
			behavior: 'all'
		}
	}).render().setUser('vxfusion').start();
}
