
/* ----------------------------------------------------------------
                  K W I A T   V I N T A G E
---------------------------------------------------------------- */

/* Perform at DOM-Ready */

$(function() {
	$('ul#navigation').dropdownMenu();

	initializePage();

	// Initiliaze page-specific scripts
	initializeCollection();
	if ($('#spotlight_page').length != 0) {
		initializeSpotlight();		
	}
});


/* ----------------------------------------------------------------
   P A G E - S P E C I F I C   F U N C T I O N S
---------------------------------------------------------------- */

/* Function tointialize page elements which
	will need to be called after Ajax scripts */

function initializePage() {
	$('a[rel^=popup]').map(function() {
		$(this).optPopup($(this).attr('href'), $(this).metadata());
	});
}


/* Collection Page */

function initializeCollection() {
	$('#email_page').slidedownBox($('#email_link'), "email.jsp");
	$('#concierge_page').slidedownBox($('#concierge_link'), "contact.jsp");
	
	initializeCollectionAjax();
}

function initializeCollectionAjax() {
	$('#image_grid a').each(function() {		
		$(this).click(function() {
			prodId = $(this).attr('title');
			swapProd('prod' + prodId);

			$('#concierge_page input[name=product]').val(prodId);
			$('#email_page input[name=product]').val(prodId);
			
			var imageLink = "/popup.jsp?content=cat1Dim&comp=" + $('#email_page input[name=comp]').val() + "&product=" + $('#email_page input[name=product]').val();
			var printLink = "/print.jsp?content=print&product=" + $('#email_page input[name=product]').val() + "&login=" + $('#email_page input[name=login]').val() + "&comp=" + $('#email_page input[name=comp]').val();
			
			// Sets the category name for the email which gets sent out
			$('#email_page input[name=category]').val($('div#prod' + $('#email_page input[name=product]').val()).find('dt').text());
			
			$('a.image_link').map(function() {
				$(this).attr('href', imageLink);
			})
			
			$('a.print_link').attr('href', printLink);
			
			initializePage();
			
			return false;
		})
	});
	
	// $('ul.pagination li a').map(function() {
	// 	$(this).ajaxLoad({ selection: '#catalog_right'});
	// });
}

/* In the Spotlight */

function initializeSpotlight() {
	$('#spotlight_right div:last').addClass('last');	
}


/* ----------------------------------------------------------------
   P L U G I N S   A N D   G E N E R A L   F U N C T I O N S
---------------------------------------------------------------- */

/* Plugin for the drop-down page menus */

(function($) {
	$.fn.dropdownMenu = function() {
		var $$ = this;
		
		$$.find('li').hover(function() {
			$(this).children('ul:hidden').show();
		}, function() {
			$(this).children('ul:visible').hide();
		});
		
		// $$.find('ul').mouseout(function() {
		// 	$(this).hide();
		// });
		
		// Fixed problem with z-index on dropdown
		if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
			$('ul#navigation ul li a').each(function() {
				$(this).prepend('<span></span>').css('float', 'left');
			});
		}
		
		return this;
	};
})(jQuery);


/* Plugin for the box which slides
   down to display the email and contact pages */

(function($) {
	$.fn.slidedownBox = function(link, postUrl) {
		var box = this;
		var boxForm = this.find('form');
		
		box.hide();

		box.prepend('<div class="arrow"></div>')
		box.prepend('<div class="message"></div>');

		box.find('.message').hide();
		
		box.find(':text').addClass('text');
		box.find(':checkbox').addClass('checkbox');
		box.find(':submit').addClass('submit');
		
		link.click(function() {
			if (box.is(':visible')) {
				box.slideUp('slow');
			}
			else {
				$('.slidedown:visible').slideUp('slow');
				box.find('.message').hide();
				box.find('form').show();

				box.slideDown('slow', function() {
					$('html:not(:animated), body:not(:animated)').animate({ scrollTop: $(document).height() - $(window).height() }, 700);
					// $('html:not(:animated), body:not(:animated)').animate({ scrollTop: $('html').height() - $(window).height() }, 700);
					box.find('input:first').focus();
				});			
			}
			return false;
		});
				
		boxForm.validate({
			submitHandler: function(form) {
				if (postUrl.substr(0, 1) != "/") {
					postUrl = "/" + postUrl;
				}
				
				$.ajax({
					type: "POST",
					url: postUrl,
					data: boxForm.serialize(),
					beforeSend: function() {
						boxForm.find('input').attr('disabled', true).addClass('disabled');
						boxForm.find('textarea').attr('disabled', true).addClass('disabled');
					},
					success: function(data) {
						box.find('.message').html(data).slideDown('slow');
						boxForm.slideUp('slow');
		
						setTimeout(function() {
							box.find('input').removeAttr('disabled');
							box.find('textarea').removeAttr('disabled');
							box.slideUp('slow') }, 3000);
						}
					});
				return false;
			}
		});
		
		return this;
	};
})(jQuery);


/*  Ajax Load function  */

(function($) {

	$.fn.ajaxLoad = function(settings) {			
		settings = jQuery.extend({
			url: $(this).attr('href'),
			selection: '',
			container: ''
		}, settings || {});
		
		if (settings.container == '') {
			settings.container = settings.selection;
		}
						
		function loadSelection() {			
			// var loading = $('<div class="loadig"></div>').width($(settings.container).outerWidth()).height($(settings.container).outerHeight());
			
			$(settings.container).addClass('loading').find('*').css('opacity', .8);
			
			// loading.prependTo(settings.container).css('opacity', 1).fadeIn('slow');
			
			var temp = $('<div/>');

			temp.load(settings.url + " " + settings.selection, function() {
				$(settings.container).replaceWith(temp.html());

				initializeCollectionAjax();
			});
			
			return false;
		}

		return this.unbind('click').click(loadSelection);
	};
})(jQuery);
