function animateFeatured() {
	if('#featured') {
		$('#featured').animate({marginTop: -85, marginBottom: 35}, 1000);
	}
}

function initMenu() {
	var menuItems = $('#menu > ul > li');
	menuItems.hover(
		function() {
			$(this).find('ul').slideDown('fast');
		},
		function() {
			$(this).find('ul').slideUp('fast');
		}
	);
}
	
function initSlideshow() {
	
	var fadeSpeed = 1000;
	var fadeDelay = 5000;
	var slideshowItems = $('#slideshow img');
	var slideshowLength = slideshowItems.length;

	if(slideshowLength > 0) {

		function startFade() {
			//start with a random photo
			var fadeNum = Math.floor(Math.random() * slideshowLength);
			//fade first photo into view
			nextFade();
			if(slideshowLength > 1) {
				//start looping through
				setInterval(nextFade, fadeDelay);
			}
			function nextFade() {
				//fade out everything except target photo
				$(slideshowItems).filter(function(index) {
					return index != fadeNum;						 
				}).fadeOut(fadeSpeed);
				//fade in target photo
				$(slideshowItems[fadeNum]).fadeIn(fadeSpeed);
				//find next photo
				if (fadeNum < (slideshowLength-1)) {
					fadeNum++;
				} else {
					fadeNum = 0;
				}
			}
		}
		startFade();
	}
}

function initTabs() {
	var tabsets = $('.product_details');
	if(tabsets.length) {
		tabsets.each(function(index, tabset) {
			var tabs = $(tabset).find('.product_tabs').find('a');
			var sections = $(tabset).find('.product_section').hide();
			tabs.each(function(index, tab) {
				$(tab).click(function() {
					tabs.removeClass('active');
					$(this).addClass('active');
					sections.hide();
					$(sections[index]).fadeIn();
					return false;					
				});
			});
			tabs.first().click();
		});
	}
}

function initImageSwap() {
	$('a.swap').click(function() {
		var target = $(this).closest('.product_details').prev('.product_overview').find('.product_photo');
		var target_link = $(this).closest('.product_details').prev('.product_overview').find('.product_photo_link');
		var desc = $(this).closest('.product_details').prev('.product_overview').find('.photo_description');
		var src = $(this).attr('href');
		var title = $(this).attr('title');
		target.attr('src', src);
		target_link.attr('href', src).attr('title', title);
		desc.html('Shown: ' + title);
		return false;
	});
	var sets = $('ul.images').has('a.swap');
	sets.each(function() {
		$(this).find('a.swap').first().click();
	});
}
 
this.screenshotPreview = function(){	
		
	xOffset = 50;
	yOffset = 50;
	$("a.preview").click(function(){
		return false;
	});
	$("a.preview").hover(function(e){
		this.t = this.title;	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.href +"' alt='url preview' />"+ c +"</p>");	
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css({
				position: "absolute",
				padding: "5px",
				backgroundColor: "#e0ecfa",
				fontSize: "10px"
			})
			.fadeIn("fast");
    },
	function(){
		$("#screenshot").remove();
	});
	$("a.preview").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function initZoom() {
	var options = {
		zoomWidth: 250,
        zoomHeight: 250,
		title: false
    }
	$(".zoom").jqzoom(options);
}

$(document).ready(function(){
	animateFeatured();
	initMenu();
	initSlideshow();
	initTabs();
	initImageSwap();
	initZoom();
	screenshotPreview();
});

