(function($) {

	// ------------------------------------------------------
	// Events to load on "document.ready"
	$(document).ready(function() {
	
		var defaultPopupHtml;
				
		$("form.hide_submit").each(function() {
			var thisForm = $(this);
			$(this).children('button').hide();
			$(this).children('select').change(function() {
				thisForm.submit();
			});
		});
		
		$("#product_nav li.last a").click(function() {
			$("#full_collection_list").toggle();
		});
		
		$("#show_exclusive_collections").toggle(
			function() { 
				$(this).html("Show all collections");
				$("#full_collection_list ul").addClass('exclusive_filter');
			},
			function() { 
				$(this).html("Show exclusive collections");
				$("#full_collection_list ul").removeClass('exclusive_filter');
			}
		);
		
		
		// Add link to imgs on product boxes
		$(".product_list li img").wrap(function() {
			var prod_img_link = $(this).siblings('a').attr('href');
			return '<a href="' + prod_img_link + '" />';
		});
		
		// Fake hover on product boxes
		$(".product_list li").hover(
			function() { $(this).children('a').addClass('hover'); },
			function() { $(this).children('a').removeClass('hover'); }
		);	
		
		
		// Add link to imgs on homepage featureboxes
		$("#home3box li").each(function() {
			var href = $("h4 a", this).attr('href');
			//$("h4 a", this).contents().unwrap();
			$(this).click(function() {
				window.location = href;
			});
		});
		
		// Fake hover on homepage featureboxes
		$("#home3box li").hover(
			function() { 
				$(this).children('h4').children('a').addClass('hover'); 
				$(this).children('img').addClass('hover');
			},
			function() { 
				$(this).children('h4').children('a').removeClass('hover');
				$(this).children('img').removeClass('hover');
			}
		);
		

		$("#header #nav ul").hover(
			function() { $(this).siblings('a').addClass('here'); },
			function() { $(this).siblings('a').removeClass('here'); }
		);

		// Tab swapper
		$(function () {
			var tabContainers = $('div#panelSwapper > div');
	
			$('div#panelSwapper ul#panelCtrl a').click(function () {
				tabContainers.hide().filter(this.hash).show();

				$('div#panelSwapper ul#panelCtrl a').removeClass('switchActive');
				$(this).addClass('switchActive');

				return false;
			}).filter(':first').click();
		});
		
		// Image swapper
		$(function () {
			var imgContainers = $('div#imgSwapper > div');
			
			$('div#imgSwapper ul#imgCtrl a').click(function () {
				imgContainers.hide().filter(this.hash).show();

				//$('div#imgSwapper ul#imgCtrl a').removeClass('switchActive');
				//$(this).addClass('switchActive');

				return false;
			}).filter(':first').click();
		    
		});
		
		// Remove search term onfocus
		$.fn.search = function() {
			return this.focus(function() {
				if( this.value == this.defaultValue ) {
					this.value = "";
				}
			}).blur(function() {
				if( !this.value.length ) {
					this.value = this.defaultValue;
				}
			});
		};
		$("form#search input").search();
		$("#email_updates_form input").search();
		$("#list_friends_email").search();
		
		

		/* modal popup on request brochure */
		if($('.pdf_lister_link').length > 0 )
		{
			var triggers = $(".pdf_lister_link").overlay({
				mask: {
					color: '#333333',
					loadSpeed: 200,
					opacity: 0.8
				},
				onBeforeLoad : function() {
					$("#innerPopupHtml .error").hide();
					$("#innerPopupHtml .brochureLoadingBar").hide();
					$("#innerPopupHtml .brochureThanks").hide();
				},
				onLoad: function() {
					$("#brochure_popup_download").submit(function() {
						return doBrochureFormAjax();	
			
					});
				},
				closeOnClick: false,
				onClose: function() {
					$('#innerPopupHtml').html(defaultPopupHtml);
				}
			}); 
	
		
			$(".pdf_lister_link").click(
				function() {
					$url = $(this).attr('href');
					$title = $(this).attr('title');
					$divhtml = $('#innerPopupHtml').html();
					defaultPopupHtml = $divhtml;
					
					$divhtml = $divhtml.replace(new RegExp("dynamic_name", 'g'), $title);
					$divhtml = $divhtml.replace(new RegExp("dynamic_url", 'g'), $url);
	
					$('#innerPopupHtml').html($divhtml);
				}
			);
		}

		
		//$("#brochure_popup_download_submit").click(function(){
		function doBrochureFormAjax()
		{
			//Define the form object
			var theForm = $("#brochure_popup_download");
			var data = theForm.serialize();
			var action = theForm.attr('action');
			
			theForm.hide();
			$(".brochureLoadingBar").show();
			
			$.ajax({
				url: action,
				data: (data),
				success: function(response) {
					switch(response)
					{
						case "success":
							$(".brochureLoadingBar").hide();
							$("#innerPopupHtml .error").hide();
							$(".brochureThanks").show();
						break;
						case "error_no_how_heard":
							$(".brochureLoadingBar").hide();
							$("#brochure_popup_download").show();
							$("#innerPopupHtml .error").show().text("Please tell us how you heard about C.P.Hart");
						break;
						case "error_no_name":
							$(".brochureLoadingBar").hide();
							$("#brochure_popup_download").show();
							$("#innerPopupHtml .error").show().text("Please enter your email");
						break;
					}
				}
			});
			return false;
		};
		
		
	});
})(jQuery);
