﻿$(document).ready(function() {

	if ($("body").hasClass("suppliers")) {
		$('img[usemap]').maphilight();
		$("map *").tooltip({
			delay: 100,
			showURL: false,
			top: -25,
			left: 10
		});
	}
	
	// initialise lightbox
	if ($("#case_thumbs, #news_images").length) {
		$("a.lightbox").lightbox();
	}

	// bring on the, uhh, embiggener!
	embiggener_init();

	// fade out site messages
	message_init();    

	// drop down Range menu
	$("#nav_site .rmSlide li.rmItem")
	.click( function() {
		location.href = $(this).find("a").attr("href");
	})
	.hover( function() {
		$(this).animate({ backgroundColor: "#5a9629" }, 250).dequeue();
		},  function() {
		$(this).animate({ backgroundColor: "#7ac042" }, 250).dequeue();
	});
	
	if ($(".prod_thumbs li").length > 0) {

		// Setup initial image statuses
		$("#product_images li:first img").css({ opacity: "1.0", display: "block" }).addClass("current").parent().addClass("current");
		$("#product_images li:gt(0) img").css({ opacity: "0.0", display: "block" });
		$(".prod_thumbs li:first").addClass("current");

		// change images when thumbnail is clicked
		$(".prod_thumbs li")
			.click(carousel_click);
	}
	
	// product catalogue background image fade
	//$(".prod_cat #content").prepend('<div id="prod_cat_fade"></div>');
	$("#prod_cat_fade").fadeTo(1000,1 /*, function() {
		$(".prod_cat #content").css({
			'background': 'url(../_Assets/images/Coffins/prod_back_paulowina.gif) no-repeat top right'
			})
		}*/).fadeTo(750,0.5);

	// front page image cycle
	if ($("#content_home_sub").length) {
		$("#front_panels").cycle('fade');
	}
	
});

$.fn.maphilight.defaults = {
	fade: true,
	fill: true,
	fillColor: 'ffffff',
	fillOpacity: 0.6,
	stroke: false
}

function embiggener_init() {
    if ($(".embiggener").length) {
        // We have the embiggener
        $.getScript("http://js.if-fma.com/jquery.jqzoom.pack.1.0.1.js", function() {
            $(".embiggener").jqzoom({
                imageOpacity: 0.2,
                fadeinSpeed: 'medium',
                fadeoutSpeed: 'medium',
                hideEffect: 'fadeout',
                position: 'right',
                showEffect: 'fadein',
                title: false,
                xOffset: 220,
                yOffset: 10,
                zoomHeight: 280,
                zoomWidth: 398
            });
        });
    }
}

function message_init() {
    if($(".message").length) {
        $(".message")
	        .fadeTo(3000, 1);
	        
	    if($(".message").hasClass("error") == false || $(".message").hasClass("exclamation") == false) {
	        $(".message").animate({ opacity: "0.0" }, 2500, function() {
	            $(this).remove();
	        });
	    }
    }
}

function carousel_click() {
	var prod_img = $(this).find("img").attr("id").replace("ctl00_cphMainContent_thumb", "large"); 			// Image ID in the ul_imagery array
	change_image("#product_images img.current", "#" + prod_img);
	// add current class to selected image
	$(this).siblings(".current").removeClass("current");
	$(this).addClass("current");
	return false;
}

function change_image(old_image, new_image, callback) {	
	if($(old_image).attr("id") != $(new_image).attr("id")) {
		$(old_image)
			.fadeTo(750, 0.0)
			.removeClass("current")
			.parent()
				.removeClass("current");

		$(new_image)
			.fadeTo(500, 1.0, callback)
			.addClass("current")
			.parent()
				.addClass("current");
	}
}

// front page image switcher


	/*
		var images = ["front_hearse.jpg", "front_reflections.jpg", "front_treehands.jpg", "front_woolcoffin.jpg"];

		$(window).load(function() {
			if ($("#category_banners").length) {
				$.shuffle(images);

				$.getScript("http://js.if-fma.com/jquery.preload.1-0-7.js", function() {
					$.preload(images, {
						base: '_assets/library/',
						onComplete: preload_complete,
						onFinish: preload_finish
					});
				});
			}
		});

		function preload_complete(data) {
			if(data.done == 1) {
				$("#cat_banner")
					.attr("src", appRoot + "_assets/library/" + images[0])
					.fadeIn('slow');
			}
		}

		function preload_finish(data) {

			$.getScript("http://js.if-fma.com/jquery.cycle.all.pack.js", function() {
				for (var i = 1; i < images.length; i++) {
					$("#category_banners")
						.append("<img src=\"" + appRoot +  "_assets/library/" + images[i] + "\" />");
				}

				$("#category_banners")
					.removeClass("loading") 
					.cycle({ fx: 'fade', speed: 1000, timeout: 5000 });
			});    
		}

		// Add indexOf for browsers that don't have it implemented
		if (!Array.prototype.indexOf) {
			Array.prototype.indexOf = function(elt /*, from //\/) {
				var len = this.length;

				var from = Number(arguments[1]) || 0;
				from = (from < 0)
				 ? Math.ceil(from)
				 : Math.floor(from);
				if (from < 0)
					from += len;

				for (; from < len; from++) {
					if (from in this &&
				  this[from] === elt)
						return from;
				}
				return -1;
			};
		}

		// jQuery Plugin
		$.fn.image = function(src, f) {
			return this.each(function() {
				var i = new Image();
				i.src = src; 
				//this.appendChild(i);
			});
		}
	*/