// (jQuery) loveliness
$(document).ready(function(){

	// Sort order display
	// Hide the list of display orders
	$('#sortOrder ul').hide();

	// Toggle view of display order list
	$('#sortOrder p').click(function() {
		$('#sortOrder ul').toggle('fast');
	});
	
	
	
	
	

	// Bind facebox links
	$('a[rel*=facebox]').facebox() 
	
	// Hide products from navigation
	$("#navigation .product").hide();

	// Hide non-active categories from navigation
	$("#navigation .closedCategory").hide();

	// Open / Close help panel 
	$(".helpButton").click(function() {
		$("#content").addClass("helpPanel")
		$('#helpPanel').show('slow')
		return false;
	});

	$("#helpPanelTitle").click(function() {
		$('#helpPanel').hide('slow');
		$("#content").removeClass("helpPanel")
		return false;		
	});		


	// Product Zoom (jQuery)
	$("#productZoom").click(function() {

		$('#productImage').remove();
		$('#productImageDiv').html('<img src="" id="productImage">');

		// Set new image src
		var imageSrc = $("#productZoom").attr("href");
		$("#productImage").attr('src', imageSrc);	

		// Run the imagetool plugin on the image
		$(function() {
			$("#productImage").imagetool({
				viewportWidth: 300,
				viewportHeight: 300,
				topX: 150,
				topY: 150,
				bottomX: 450,
				bottomY: 450,
				loading: '/img/elements/loading.gif'
			});
		});
		
		$("#productImageDragtext").fadeIn();
		
		return false;
	});

	// 'Unzoom' a product image
	$(".closeZoom").click(function() {
	
		// Set smaller image src (from the ID... is that messy?)
		var altSmallImage = $(this).attr("id");

		// Call function to replace image
		altImage(altSmallImage);

		return false;		
    });								   


	// Alternative product photos (jQuery)
	$(".altPhoto").click(function() {

		// Set new image src
		var altImageSrc = $(this).attr("href");

		// Set new image Zoom link (from the ID... is that messy?)
		var altZoomLink = $(this).attr("id");

		// Call function to replace image
		altImage(altImageSrc, altZoomLink);

		return false;	
	});
	
	// Function: Show alternative product image
	function altImage(src, zoomLink) {

		// Fade out the zooming instructions if they're visable
		$("#productImageDragtext").fadeOut();

		// Remove the Zoom viewport div if it exists
		$('#productImageDiv div.viewport').remove();
		
		// Remove the current image
		$('#productImage').remove();

		// Set the new image Zoom link
		$("#productZoom").attr('href', zoomLink);

		// Set the new image 'Close Zoom' link
		$(".closeZoom").attr('id', src);

		// Load new image
		var img = new Image();
        $(img).load(function () {
			$(this).hide();
            $('#productImageDiv').append(this);
            $(this).fadeIn();
        }).attr({
			src: src,
			id: "productImage"
			});
	 }


	// Set up tabs on product page (jQuery UI Tabs widget)
	$("#productTabs > ul").tabs();


	// Front page Industry fader
	// Check the container exists on this page
	if ( $("#industries").length > 0 ) { 	
		// Slideshow - Front page industry panels
		$(function() {
			$('#industries').cycle({
				timeout: 8000,
				speed: 	1000,
				fit:	1
			});
		});	
		
		// As the cycle plugin messes up the rendering of the industry panels, add a top margin to the next element
		$("#industryLinks").css({'margin-top' : '38em'});
	}


	// Homepage Advanced Search
	// First, check that the advanced search exists on this page
	if ( $(".homepage #advancedSearch").length > 0 ) { 
		// Then hide the 'features' section of the search
		$("#advancedSearchFeatures").hide();
		
		// When an industry is selected, show the product features list
		$("input[@name='iProductIndustryID']").click(function() {
			$("#advancedSearchFeatures").slideDown();
		})
	}

});



// Limit text in textarea
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
