/*
	r-picks.js	
*/

// =========================================================================================
// preloadNavImages()
// =========================================================================================
function preloadXmasCat() {
	
	$('.category-list-r-picks img').each(function() { 
		imgSrc = $(this).attr('src');
		imgSrc = imgSrc.replace('-a', '-b');
		$("<img>").attr("src", imgSrc);
	});
	return false;	
	
}
// =========================================================================================

$(document).ready(function() {
									
	// preload cat images
	preloadXmasCat();
									
	if ($('#wishlist p.fade').is(':visible')) {
		setTimeout(function() {
			$('#wishlist p.fade').fadeOut('slow');
		}, 5000);
	}

	// == trigger send to me =============================================================
	
	$('#wishlist .nav a').click(function(e) {
													 
		var sendme = false;
		var sendfriend = false;
		var sendsanta = false;
		var printpage = false;
		var form = $('#wishlist form');
	
		e.preventDefault();
		
		if ($(this).hasClass('active')) {
			$(this).removeClass('active');
			form.fadeOut('fast', function() {
				$(this).removeAttr('class');
			});
			return false;
		}
		
		if ($(this).hasClass('sendme')) {
			sendme = true;
			sendEmail('sendme');
		} else if ($(this).hasClass('sendfriend')) {
			sendfriend = true;
			sendEmail('sendfriend');			
		} else if ($(this).hasClass('sendsanta')) {
			sendsanta = true;
			sendEmail('sendsanta');
		} else {
			window.print();
		}
		
	});	
	
	var sendEmail = function (type) {
		
		var form = $('#wishlist form');

		if (form.is(':visible')) {
			form.fadeOut('fast', function() {
				form.removeAttr('class');
				showEmailForm(form, type);
			});
		} else {
			showEmailForm(form, type);			
		}
		
	}
	
	var showEmailForm = function (form, type) {
		
		if (type == 'sendme') {
			form.addClass('sendme');
			$("#id_email_type").val("sendme");
			// form.find('input[type="submit"]').val('Send to Me');
		} else if (type == 'sendsanta') {
			form.addClass('sendsanta');
			$("#id_email_type").val("sendsanta");
			// form.find('input[type="submit"]').val('Send to Santa');			
		} else {
			form.addClass('sendfriend');
		   $("#id_email_me").val( "False" );
		   $("#id_email_type").val("sendfriend");
			
			// form.find('input[type="submit"]').val('Send to a Friend');			
		}
		$('#wishlist .nav a').removeClass('active');
		$('#wishlist .nav a.' + type).addClass('active');
		form.fadeIn('normal', function() {
			$(this).find('input:first').focus();
		});	
		
	}
	
	// == place top 10 numbers============================================================
	var placeTop10Numbers = function() {
		
		var top10list = $('ul#r-picks li');
		var counter = 1;
		
		top10list.each(function() {
										
			$(this).prepend('<img src="/static/images/campaigns/2009/r-picks/img-top10-' + counter + '.png" width="47" height="46" alt="#' + counter++ + '" class="top10">');
										
		});

	}
	
	// if category listing
	if ($('ul#r-picks').length > 0) {
		
		placeTop10Numbers();
		
	}	
	

});