// add class to show js enabled (all javascript ENABLED css styles can be prepended with html.js, all javascript DISABLED css styles can be prepended with html.no-js)
$('html').removeClass('no-js').addClass('js');

$(function(){


	// find all the input elements with title attributes (jquery.hint)
	$('input[title!=""]').hint();


	// input focus class helper for css styling hooks
	$(":text, :password, textarea")
		.focus(function() {
			$(this).addClass('focus');
		})
		.blur(function() {
			$(this).removeClass('focus');
		});


	// input focus class helper for css styling hooks
	$("button, .button")
		.hover(
			function () {
				$(this).addClass('hover');
			}, 
			function () {
				$(this).removeClass('hover');
			}
		);


	// open rel="external" links in new window (instead of target="_blank")
	$("a[rel='external']").addClass("external").click( function() { window.open( $(this).attr('href') ); return false; } );


	// carousel
	$('#footerCarousel').jcarousel({
		// Configuration goes here
	});


	// terms popup
	$("#terms")
		.live('click', function(e) {
			e.preventDefault();
			var link = $(this).attr('href');
			$('#content').hide();
			$('<div id="popHolder" />').appendTo('#content-wrapper');
			$("#popHolder").load(link + ' #termsContent', function() {
				$('<a id="close" href="#">Hide Terms &amp; Conditions</a>').appendTo('#content-wrapper').bind('click', function() {
					$('#content').show();
					$('#popHolder, #close').remove();
				});
			});
		});


	// press glitz
	$("#press_content h5").css('opacity','0.8');
	$("#press_content a,.display-items a.item-img")
		.hover(
			function () {
				$('img',this).css('opacity','0.8');
				$(this).addClass('hover');
			}, 
			function () {
				$('img',this).css('opacity','1');
				$(this).removeClass('hover');
			}
		);
	// fancbox press display
	
	$("a#display-img-gallery").fancybox();
	$("a#display-txt-gallery").fancybox();

	/*$("a[rel=display-img-gallery]").fancybox({
		'transitionIn'		:	'none',
		'transitionOut'		:	'none',
		'titlePosition'		:	'over',
		'overlayOpacity'	:	'0.9',
		'overlayColor'		:	'#000',
		'titleFormat'		:	function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=display-txt-gallery]").fancybox({
		'transitionIn'		:	'none',
		'transitionOut'		:	'none',
		'overlayOpacity'	:	'0.9',
		'overlayColor'		:	'#000',
		'titlePosition'		:	'over',
		'titleFormat'		:	function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});*/


	// Homepage
	var height;
	if ($.browser.msie) {
		$("#navwrapper h5 a").css('opacity','0.6');
		height = 146;
	} else {
		height = 145;
	}
	var $navwrapper=$("#navwrapper");
	$navwrapper
		.animate({bottom:'-'+height}, 1000, function() {})
		.hover(
			function () {
				$(this)
					.stop()
					.animate({bottom:'0'}, 1000);
			},
			function () {
				$(this)
					.stop()
					.animate({bottom:'-'+height}, 1000);
			}
		)
		.find("h2")
		.each(function(index) {
			var thisclass=$(this).attr("class");
			$(this)
				.hover(
					function () {
						$("h2").removeClass("sel");
						$(this).addClass("sel");
						if ($.browser.msie) {
							$("h2 a").css('opacity','0.6');
							$("a", this).css('opacity','0.9');
						}
						$(".navoptions")
							.css("display","none")
							.filter("#"+thisclass)
							.css("display","block");
					}, 
					function () {}
				);
		});


	// product popup form
	$(".viewenquire")
		.live('click', function(e) {
			e.preventDefault();
			var link = $(this).attr('href');
			$("#prodInfo #closeenquire").removeClass("show");
			$("#prodInfo div.blurb,#prodInfo p.size,.viewenquire").hide();
			$("#prodInfo").append("<div id=\"form-pop-up\" class=\"loading\"></div>");
			$("#form-pop-up").load(
				link + " #formwrapper",
				function(){
					$("#form-pop-up").removeClass("loading");
					$("#enquireForm button").live('click', function(e) {
						e.preventDefault();
						var error=false;
						$(".text,select", $("#enquireForm")).each(function(index) {
							if ($(this).val().length < 2) {
								error=true;
								$(this).addClass("error");
							} else {
								$(this).removeClass("error");
							}
						});
						if (!error) {
							var inputs = [];
							$(':input',$("#enquireForm")).each(function() {
								inputs.push(this.name + '=' + escape(this.value));
							});
							jQuery.ajax({
								data: inputs.join('&'),
								url: $("#enquireForm").attr("action"),
								timeout: 2000,
								type: "POST",
								error: function(r) {
									//console.log("Failed to submit");
								},
								success: function(r) {
									//$("#cuffreg-top").append($("#thankMessage"));
									//$("#thankMessage").css("display","block");
									//$("#register_cufflinks_form, #introMessage").remove();
									//$("ul.footLinks").css("display","block"); // weird IE bug fix for layout
									$("#enquireForm, #closeenquire").remove();
									$("#formwrapper").text("Thank you for your enquiry. We will be in contact shortly.");
									function showandhide() {
										$("#formwrapper, #form-pop-up").remove();
										$(".blurb, #view").show();
									}
									window.setTimeout(showandhide, 5000);
								}
							});
						}
					});
				}
			);

		});
		
	$("#closeenquire").click(function(e){
		e.preventDefault();
		$("#prodInfo #closeenquire").addClass("show");
		$("#prodInfo div.blurb,#prodInfo p.size,.viewenquire").show();
		$("#prodInfo #form-pop-up").remove();
	});
	
	// Custom press image pop up
	$("#fancybox-img").live("click", function() {
		window.open( $(this).attr('src') );
	});

});
