/**
 * @fileoverview Pagination
 * 
 */
var product = function(){

	// 
	var $body = null;
	
	
	/**
	 * The options passed through to this function
	 *
	 * @var Object
	 * @private
	 */
	var options = {

	};
	
	
	/**
	 * Initialise the functionality
	 * @param {Object} options The initialisation options
	 * @return void
	 * @public
	 */
	var init = function(initOptions) {
		
		// save any options sent through to the intialisation script, if set
		for (var option in options) {
			if (!!initOptions[option] || initOptions[option] === false) {
				options[option] = initOptions[option];
			}
			
			// error check, if no element is specified then stop
			if (!options[option] && options[option] !== false && options[option] !== 0) {
				throw('Required option not specified: ' + option);
				//return false;
			}
		}
		
		$body = $("body");
		
		
		// initialise product page tabs (right col)
		initTabs();
	
		
		// initialise scrolling thumbnails (below/right of main product image)
		initThumbScroll();
		
		
		// initialise the thumbnail updater, that changes the main image when clicked 
		initThumbUpdater();
		
		
		// initialise the product image zoom functionality
		initImageZoom();
		
		
		// remove title for IE (causes problems if not)
		if (LibMan.isMSIE && LibMan.vIE < 8) {
			$("#product-image-main img").attr('alt', '').attr('title', '');
		}
	};
	
	
	
	/*
	 *
	 */
	var initTabs = function() {
		// tabs
		$("div#tabs ul.menu").tabs("div#tab-container div.tab", {
			tabs: "li",
			current: "selected"
		});
		
	};
	
	
	
	/*
	 *
	 */
	var initThumbScroll = function() {
		// scrollable thumbs
		var thumbScrollable;
		if ($body.hasClass("jeans")) {
			thumbScrollable = $("#product-image-thumbs").scrollable({
				items: "#product-image-thumbs-clip",
				speed: 500,
				vertical: true,
				keyboard: false,
				next: "#nav-next",
				prev: "#nav-previous"
			});
		} else {
			thumbScrollable = $("#product-image-thumbs").scrollable({
				items: "#product-image-thumbs-clip",
				speed: 500,
				keyboard: false,
				next: "#nav-next",
				prev: "#nav-previous"
			});
		}
		var thumbScrollableApi = thumbScrollable.scrollable();

		// remove default events
		$("a#nav-next, a#nav-previous").bind('click', function(e){
			e.preventDefault();
			this.blur();
		});
	};
	
	
	
	/*
	 * When the thumbnail is clicked, change both the visible large image, and the rollover zoom image
	 */
	var initThumbUpdater = function() {
	
		// scrollable thumbs - change the main image on hover
		$("#product-image-thumbs a").bind('click', function(e) {
			e.preventDefault();
			this.blur();
		 	if ($(this).hasClass("active")) { return; }
		 	
			// change the images
			var url = $(this).attr("href");
		 	var largeImage = $("#product-image-main");
			
			// change image - fade for non-IE
			if (LibMan.isMSIE && LibMan.vIE < 8) {
			
				largeImage.find("img").attr("src", url).attr('alt', '').attr('title', '');
			
			} else {
				largeImage.animate({
					opacity : 0
				}, 250, function(){
					largeImage.find("img").attr("src", url);
					$(this).delay(100).animate({opacity:1}, 250);
				});
			}
			
			// change the larger 'zoom' image url here
			var zoomImage = url;
			zoomImage = zoomImage.replace(/imagepad.php\?w=[0-9]+&h=[0-9]+&f=/g, '/');
			largeImage.find("a").attr("href", zoomImage);
			// animate border
		 	$("#product-image-thumbs a").removeClass("active").animate({borderColor:"#ffffff"}, 250);
		 	$(this).addClass("active").stop().animate({borderColor:"#7a7a7a"}, 250);
            // PW re-inistialise image zoom
            initImageZoom();			

			// animate border
		 	$("#product-image-thumbs a").removeClass("active").animate({borderColor:"#ffffff"}, 250);
		 	$(this).addClass("active").stop().animate({borderColor:"#7a7a7a"}, 250);
		 });
		
		// make first thumb selected
		$("#product-image-thumbs a:first").css({border:"1px solid #7a7a7a"});

		
	};

	
	
	
	/*
	 *
	 */
	var initImageZoom = function() {
	
		$("#zoom-jeans").jqzoom({
		    zoomWidth: 622,
		    zoomHeight: 500,
            xOffset: 18,
            yOffset: 0,
			showEffect: "fadein",
			hideEffect: "fadeout",
            position: "right",
			title:false
		});
		
		
		$("#zoom").jqzoom({
		    zoomWidth: 540,
		    zoomHeight: 540,
            xOffset: 20,
            yOffset: 0,
			showEffect: "fadein",
			hideEffect: "fadeout",
            position: "right",
			title:false
		});
	};
	
	
	/*
	 * 
	 */
	return {
		init: init
	};
	
}();


