// Nav selection

$(document).ready(function(){
	var path = location.pathname.substring(1).split('/');
	if (path) {
		if(path != '') {
			$('#nav a[href^="/'+path[0]+'"]').parent().addClass('selected');
		} else {
			$('#nav a[href$="/'+path[0]+'"]').parent().addClass('selected');
		}
		$('#page_nav a[href$="/'+path[0]+'/'+path[1]+'"]').parent().addClass('selected');

		// Special cases
		if (path[0] == 'products' ||
			path[0] == 'bundles.php' ||
			path[0] == 'single-disc-collections.php' ||
			path[0] == 'sports.php') {
			$('#nav a[href="/bundles.php"]').parent().addClass('selected');
		} else if (location.pathname == '/faq.php') {
			$('#nav a[href^="/company"]').parent().addClass('selected');
			$('#page_nav a[href="/faq.php"]').parent().addClass('selected');
		}
		
		if(path[0] == 'singles.php' || path[0] == 'collections.php'){
			$('.view_all').css({
				'display' : 'none'
			});
		}
	}
});

// Light Box

$(document).ready(function() {
	var lightBoxLink = $('a[rel^="prettyPhoto"]');
	// This flag is used to disable the checkboxes and buy 2 get 1 free behavior
	// Add an item with id of noCheckboxes to stop them from appearing
	var showCheckboxes = !$('#noCheckboxes').length;
	lightBoxLink.prettyPhoto({animationSpeed:'fast', showTitle: false, theme:'facebook' });
	lightBoxLink.not('.hide-button').has('img').prepend('<span class="enlarge">+</span>');
	$('.slideshow .enlarge').text('enlarge');
	lightBoxLink.has('img').css({
		'position': 'relative',
		'text-decoration': 'none',
		'display' : 'inline-block'
	});
	$('span', lightBoxLink).css({
		'position' : 'absolute',
		'top' : '5px',
		'right' : '5px'
	});
	
	// Within the item link
	$('div.item > a:not([rel^="prettyPhoto"])').append('<span class="button">View details</span> ');
	// Outside the item link
	$('div.item').each(function() {
		// Skip non-products (those that have the rel attribute)
		if ($(this).children('[rel]').length > 0) {
			return; // continue
		}
		var itemId = $(this).attr('id');
		var labelId = itemId + '-label';
		$(this).append('<a class="button-download" href="http://secure.ultracart.com/cgi-bin/UCEditor?merchantId=8233C&amp;ADD=' +itemId+ '">Buy Now <span id="' +itemId+ '-price"></span></a>');
		if (showCheckboxes) {
			$(this).append('<br><input id="' +labelId+ '" class="itemCheckbox" type="checkbox"><label style="font-size: 11px" for="' +labelId+ '">Add this collection to your bundle</label>');
		}
		$('#' +itemId+ '-price').load('/get-price.php?itemId=' +itemId);
	});

	$('input.itemCheckbox').click(function(e) {
		updateSelectedItems(e);
	});
	$('.item a span.button').css({
		'padding-top' : '2px',
		'padding-right' : '5px',
		'padding-bottom' : '2px',
		'padding-left' : '5px',
		'font-size' : '9px'
	});
	$('.item a h2').css({
		'margin-top' : '0.5em',
		'margin-bottom' : '0.5em'
	});
});



//Slideshow

$(document).ready(function() {
	slideshow('div.slideshow', 3000);
});

function slideshow(tag, delay){
	
	// Variables
	var slideshow = $(tag);
	var slider;
	var content;
	var slides = slideshow.children();
	var nav;
	var li ="";
		
	var intervalId;
	var currentSlide = 0;
	var totalSlides = slides.length;
		
	// Build Slider and Nav
	
	slides.wrapAll('<div class="slideshow-slider" />');
	slider = $('div.slideshow-slider', slideshow);
	
	slides.wrapAll('<div class="slideshow-content" />');
	content = $('div.slideshow-content', slideshow);
	
	if(totalSlides > 1){
		slideshow.append('<ul class="slideshow-nav" />');
		nav = $('ul.slideshow-nav', slideshow);
		slides.each(
			function(i){
				li += '<li><a class="slideshow-button" href="#">' + (i+1) + '</a></li>';
			}
		);
		nav.hide();
		nav.append(li);
		setNavState();
	}
	slideshow.after("<div class='instructions'><span>Enlargement Instructions</span><ol><li>Click on the &quot;Enlarge&quot; button, on the contact sheet image above, to Enter the &quot;Lightbox&quot;. (You may also click directly on the image, to Enter the Lightbox.)</li><li>Use the arrow buttons at the lower left corner of the Lightbox window, to view images. (If there are no arrows, there is only one image in the Lightbox.)</li><li>When you're finished, click the &quot;X&quot;, in the lower right corner, to return to the product page.</li></ol></div>");
	$('div.instructions ol').hide();
	$('div.instructions span').hover(function(){
		$('div.instructions ol').stop(true, true).slideDown('slow');
	},function(){
		$('div.instructions ol').stop(true, true).slideUp('slow');
	});
	// CSS
	
	slides.css({
		'display' : 'inline',
		'float' : 'left'
	});
	
	slideshow.css({
		'width' : slides.width()		  
	});
	
	slider.css({
		'overflow' : 'hidden',
		'width' : slides.width(),
		'position': 'relative'
	});
	
	content.css({
		'width' : (slideshow.width() * totalSlides)			
	});
	
	// Mouse Events
	slideshow.hover(
		function() { pause(); nav.fadeIn();},
		function() { play(); nav.fadeOut();}
	);
	
	$(window).load(function() {		
		if(totalSlides > 1){
			$('a', nav).click(function(e) {
				slideTo($(e.target).parent().index());
				e.preventDefault();
				this.blur();
			});
			play();
		}
	});
		
	
	// Functions
		
	function slideTo(number) {
		currentSlide = number;
		slider.stop(true).animate({ scrollLeft: number * slides.width() }, 'slow');
		setNavState();
	}
	
	function play() {
		clearInterval(intervalId);
		intervalId = setInterval(nextSlide, delay);
	}
	
	function pause() {
		clearInterval(intervalId);
	}
		
	function nextSlide() {			
		currentSlide ++;
		if(currentSlide == totalSlides) {
			currentSlide = 0;
		}
		slideTo(currentSlide);
	}
	
	function setNavState() {
		$('li', nav).removeClass('selected');
		$('li:eq('+currentSlide+')', nav).addClass('selected');
	}
}

//faq

$(document).ready(function() {
	$('dl.faq dd').hide();
	$('dl.faq dt').click(function(){						  
		$(this).next('dd:visible').slideUp('slow');
		$(this).next('dd:hidden').slideDown('slow');
	});
	
});

// Email input field hints
$(document).ready(function() {
	addHint('ea');
	addHint('es');
});

function clearCheckboxes() {
	$('#content > .section > .item > input').attr('checked', false);
	selectedItems = new Array();
	$('#selectedItemsBar').html('');
	$('#selectedItemsBar').slideDown("slow");
	$('#footer').attr('style', 'margin-bottom: 0px');
}

// TODO Have 2 and 3 placeholder images to show they're missing them (encourage users to get the free item)
// TODO Add item titles as captions below the image
// TODO Allow page to scroll down to so navbar doesn't cover it up
// TODO Page is slow in IE - see what's causing the slowdown
// # => [name, img, itemId]
selectedItems = new Array();

function updateSelectedItems(e) {
	var name = e.target.parentNode.children[0].children[1].innerHTML;
	var img = e.target.parentNode.children[0].children[0].getAttribute('src');
	var itemId = e.target.parentNode.getAttribute('id');
	if (e.target.checked) {
		selectedItems.push(new Array(name, img, itemId));
	} else {
		// Find and remove the item
		for (var i = 0, n = selectedItems.length; i < n; i++) {
			if (name == selectedItems[i][0]) {
				selectedItems.remove(i);
				break;
			}
		}
	}

	// Display the items
	var msg = '';
	if (selectedItems.length == 1) {
		msg = 'Add another collection from this page and get the 3rd one free';
	} else if (selectedItems.length == 2) {
		msg = 'Add a third collection for FREE';
	} else if (selectedItems.length >= 3) {
		msg = "Add these to the shopping cart to complete your purchase";
	} 

	var content = '<b style="font-size: 12pt">' + msg + '</b><br>';
	var urlItems = '';
	for (var i = 0, n = selectedItems.length; i < n; i++) {
		content += "<img src='" + selectedItems[i][1] + "' title='"+ selectedItems[i][0] +"' width='150' height='100'> &nbsp;";
		urlItems += '&ADD=' + selectedItems[i][2];
	}
	content += '<br><span style="float:right;"><a href="http://secure.ultracart.com/cgi-bin/UCEditor?merchantId=8233C' + urlItems + '" class="button">Add to shopping cart</a>' +
	'<input style="margin-left: 10px" type="button" class="button" value="Clear" onclick="clearCheckboxes(); return false;">';
	if (selectedItems.length == 0) {
		$('#selectedItemsBar').slideUp("slow");
		$('#footer').attr('style', 'margin-bottom: 0px');
		return;
	}

	$('#selectedItemsBar').html(content);

	if (selectedItems.length > 0) {
		$('#footer').attr('style', 'margin-bottom: 200px');
		$('#selectedItemsBar').slideDown("slow");
	}
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
	  var rest = this.slice((to || from) + 1 || this.length);
	    this.length = from < 0 ? this.length + from : from;
		  return this.push.apply(this, rest);
};

var emailInputHint = 'Enter your email address';
// Add hints for email fields (only called when page loads)
function addHint(id) {
	$('#' + id).val(emailInputHint);
	$('#' + id).css('color', '#999');
}

// Remove hints for email fields (called onfocus)
function removeHint(obj) {
	if (obj.value == emailInputHint) {
		obj.value = '';
		obj.style.color = '#000';
	}
}

