/*
		Copyright (c) 2008-2009 JEON - James Eunson Online
		All Rights Reserved
		http://www.jameseunson.com
*/

(function($){
	var Simplywines = {	
		searchString: 'Enter search terms',
		newsletterString: 'Enter your email address',
		usernameString: 'Enter your username',
		passwordString: 'password',
		init: function() {
			var self = this;
			
			// Bind auto clear to text fields
			self.bindEmpty($('#nav_search input[type="text"], #search input[type="text"]'), self.searchString);
			self.bindEmpty($('#bit.newsletter input[type="text"]'), self.newsletterString);
			self.bindEmpty($('#bit.account input[type="text"], #login input[type="text"]'), self.usernameString);
			self.bindEmpty($('#bit.account input[type="password"], #login input[type="password"]'), self.passwordString);
			
			// Buy and add buttons for specials
			$('button.add').each(function(i){
				$(this).bind('click', function(e) {
					var id = $(this).parent().children().filter('.id').val();
					window.location = 'http://' + window.location.hostname + '/cart/add/' + id;
				});
			});
			$('button.info').each(function(i){
				$(this).bind('click', function(e) {
					var id = $(this).parent().children().filter('.id').val();
					window.location = 'http://' + window.location.hostname + '/browse/bottle/' + id;
				});
			});
			
			// Purchase button on checkout page
			$('button.purchase').each(function(i){
				$(this).bind('click', function(e) {
					window.location = 'http://' + window.location.hostname + '/purchase';
				});
			});
			
			// Add newsletter sidebar widget - jquery form override
			$('#bit.newsletter form').bind('submit', function(e){
				e.preventDefault();
				$(this).ajaxSubmit({
					dataType: 'json',
					success: function(json) {
						if(json.added == 1) {
							$('#bit.newsletter form .error').hide();
							$('#bit.newsletter form .success').append('Thanks for signing up! We have sent you a confirmation email.').fadeIn('fast');
							$('#bit.newsletter form input[type="text"]').attr('disabled','true');
						} else {
							$('#bit.newsletter form .success').hide();
							$('#bit.newsletter form .error').empty().append(json.error).fadeIn('fast');
						}
					}
				});
			});
			

			
			// Category browser
			var cat_links = $('ul#cat_list li a');
			var cat_links_array = []; // li a elements
			var browser_windows = []; // div.browser elements
			var selected_index = null; // currently selected button/browser
			
			// Initialize
			$.each(cat_links, function(i){
				cat_links_array.push($(this));
			});
			
			$(cat_links).each(function(i){ // Establish initially selected element
				$(this).parent().attr('class') == 'selected' ? selected_index = i : '';
			});
			
			$('.browser').each(function(i){
				i > 0 ? $(this).css('display', 'none') : ''; // Set i=0 visible, i > 0 hidden
				browser_windows.push($(this));
			});
			
			// Create bindings
			$(cat_links).each(function(j) {
				$(this)
					.bind('click', function(e){
						e.preventDefault();
						// Change browser view
						$(cat_links_array[selected_index]).parent().removeClass('selected');
						$(browser_windows[selected_index]).fadeOut('fast');
						selected_index = j;
						$(cat_links_array[selected_index]).parent().addClass('selected').removeClass('over');
						$(browser_windows[selected_index]).fadeIn('fast');

					}).bind('mouseover', function(e) {
						$.each(cat_links, function(i) {
							$(this).parent().attr('class') == 'over' ? $(this).parent().removeClass('over') : '';
						});
						j != selected_index ? $(this).parent().addClass('over') : '';
					});
			});
			$('ul#cat_list').bind('mouseleave', function(e){ // Return selected to correct one on mouseleave
				$.each(cat_links, function(i) {
					$(this).parent().removeClass('over');
				})
			});
		},
		bindEmpty: function(field, string) { // Generic function for clearing text fields
			$(field)
			.attr('value',string)
			.attr('title',string)
			.addClass('inactive')	
			.bind('click', function(e) {
				if($(this).val() == $(this).attr('title')) { 
					$(this).val('').removeClass('inactive').addClass('active');
				}
			}).bind('blur', function(e){
				if($(this).val() == '') {
					$(this).val($(this).attr('title')).removeClass('active').addClass('inactive');
				}
			});
		}


	};
	
	$(function() {
		Simplywines.init();
	});

})(jQuery);
