﻿/* ===========================================================
	 Initialisation code
   ========================================================== */

$(document).ready(function() {

    // Open *rel* links in a new window
    $('a[rel="external"]').click(function() {
        var newWin = window.open(this.href, '');
        if (newWin) {
            newWin.focus();
        }
        return false;
    }).each(function(i) {
        var helpText = '(opens in a new window)';
        var currentTitle = $(this).attr('title');
        if (currentTitle && currentTitle.length > 0) {
            currentTitle += ' ';
        }

        $(this).attr('title', currentTitle + helpText);
    });

    // SITE WIDE:

    // search box copy
    $("#searchBox input.search").focus(
		function() {
		    if (this.value == "Search...") { this.value = ""; }
		}).blur(
		function() {
		    if (this.value == "") { this.value = "Search..."; }
		}
	);

    // background image hover on categories
    /* if ($("ul.product_categories").length) {
    $("ul.product_categories li").ahover();
    }; */

    // background hover colour on product groups and link them up
    $(".product_group, .product_category, .product_list")
		.hover(
			function() {
			    $(this).addClass("hover");
			    window.status = $(this).find(".product_title").attr("href");
			},
			function() {
			    $(this).removeClass("hover");
			});

    //  product groups need linking up
    $(".product_group").click(function() {
        location.href = $(this).find(".product_title").attr("href");
    });

    //  product categories need linking up
    $(".product_category, .price_info, .productContainer").click(function() {
        location.href = $(this).find("a").attr("href");
    });

    $(".Zinsser .product_category").click(function() {
        location.href = $(this).find(".view_range a").attr("href");
    });

    $(".Bollom .home_item").click(function() {
        location.href = $(this).find("a").attr("href");
    });

    attachLightbox();

    // RUSTOLEUM

    // panel switcher for v2 homepage
    if ($("#project_ideas_wrap").length) {
        $(".project_panel h3").click(function() {
            if (($(this)).parent().hasClass("behind")) {
                $(this).parent().removeClass("behind").addClass("panel_front");
                $(this).parent().siblings().addClass("behind").removeClass("panel_front");
            } else {
                $(this).parent().addClass("behind").removeClass("panel_front");
                $(this).parent().siblings().addClass("behind").removeClass("panel_front");
            }
        })
    }

    // rounded corners in panel switcher
    if ($("#project_ideas_wrap").length) {
        $("#your_projects_thumbs li, #project_idea_thumbs li")
     		.append('<span class="corner gold tl"></span><span class="corner gold tr"></span><span class="corner gold bl"></span><span class="corner gold br"></span>');
    }

    // link to open the technical documents radwindow on product pages
    $(".show_documents").click(function() {
        var data = $(this).attr('rel').split(',');
        return ShowDocuments(data[0], data[1]);
    });

    // fade the inspiration description
    $("#inspiration_main_info").css("opacity", "0.75");

    $("#inspiration_nav a")
	.hover(
		function() {
		    var id = $(this).attr("id").replace("_nav", "");
		    $("#inspiration_panels #" + id).animate({ opacity: 1.0 }, 250);
		},
		function() {
		    var id = $(this).attr("id").replace("_nav", "");
		    $("#inspiration_panels #" + id).animate({ opacity: 0.4 }, 250);
		}
	);

    $("#inspiration_main_thumbs input")
	.css("opacity", "0.4")
	.hover(
		function() {
		    $(this).animate({ opacity: 1.0 }, 250);
		},
		function() {
		    $(this).animate({ opacity: 0.4 }, 250);
		});

    if ($("body").hasClass("case_study_page")) {

        if ($("#inspiration_main_large img").length) {
            var img_file = $("#inspiration_main_large img").attr('src');
            img_file = img_file.substring(img_file.lastIndexOf('/') + 1);

            $("input[src$='" + img_file + "']")
			    .css("opacity", "1.0")
			    .addClass("selected")
			    .unbind('mouseenter')
			    .unbind('mouseleave');
        }
    };

    $(".browse_by")
		.hover(
			function() {
			    $(".browse_subnav").css("display", "block");
			},
			function() {
			    $(".browse_subnav").css("display", "none");
			});

    // change swatch class when swatch is clicked
    $("#ul_swatches li")
	    .click(function() {
	        $("#ul_swatches li").removeClass("current");
	        $(this).addClass("current");
	    });

    // take the border off the last product in the sub menu
    $("#sub_related li:last-child .sub_product").css("border", "none");

    // BLAGGING "CURRENT" STATUS

    // add a class of 'current' to the product tab when on product pages
    if ($.query.get('pgId') >= 1) {
        $(".Ecolibrium #TopMenu li:nth-child(2)").addClass("current");
        $(".Zinsser #TopMenu li:nth-child(3)").addClass("current");
    }

    // add a class of 'current' to News sub-pages
    $(".Bollom.news #TopMenu li:nth-child(2)").addClass("current");
    $(".Holdtite.news #TopMenu li:nth-child(5)").addClass("current");
    $(".Rustoleum.news #TopMenu li:nth-child(2)").addClass("current");
    $(".TorCoatings.news #TopMenu li:nth-child(2)").addClass("current");
    $(".Zinsser.news #TopMenu li:nth-child(2)").addClass("current");

    if ($.query.get('cId') == 68) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 70) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 71) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 72) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 74) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 75) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 77) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 80) { $(".TorCoatings #TopMenu li:nth-child(3)").addClass("current"); }

    // add a class of 'current' to the Blackfriar product tabs when on product pages
    if ($.query.get('cId') == 86) { $(".Zinsser #TopMenu li:nth-child(2)").addClass("current"); }
    if ($.query.get('cId') == 87) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 88) { $(".Zinsser #TopMenu li:nth-child(4)").addClass("current"); }
    if ($.query.get('cId') == 89) { $(".Zinsser #TopMenu li:nth-child(5)").addClass("current"); }
    if ($.query.get('cId') == 90) { $(".Zinsser #TopMenu li:nth-child(6)").addClass("current"); }

    // add a class of 'current' to the Holdtite product tabs when on product category pages
    if ($.query.get('cId') == 78) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 79) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 92) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 93) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 94) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 95) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 96) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 97) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 98) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 99) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 100) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 101) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 102) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 103) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 104) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 105) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 106) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 115) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 121) { $(".Holdtite #TopMenu li:nth-child(3)").addClass("current"); }

    // add a class of 'current' to the Zinsser product tabs when on product category pages
    if ($.query.get('cId') == 123) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 124) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 127) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 128) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 129) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 130) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 131) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 132) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 134) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 137) { $(".Zinsser #TopMenu li:nth-child(3)").addClass("current"); }

    // add a class of 'current' to the Bollom product tabs when on product category pages
    if ($.query.get('cId') == 142) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 143) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 144) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 145) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 146) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 147) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }
    if ($.query.get('cId') == 148) { $(".Bollom #TopMenu li:nth-child(3)").addClass("current"); }

});

function attachLightbox() {
    // alert("Attaching lightbox");
    $("a[rel='lightbox']").lightBox();
}

// TOR COATINGS

function switchCmsTab(toDisplay, toHide) {
	if($("#" + toDisplay).attr("class") == "hidden") {
		$("#" + toDisplay).attr("class", "");
		$("#" + toDisplay + "Li").addClass("current");
		
		var toHides = toHide.split(",");
		
		for (var i = 0; i < toHides.length; i++) {
			$("#" + toHides[i]).attr("class", "hidden");
			$("#" + toHides[i] + "Li").attr("class", "");
		}
	}
}

// RUSTOLEUM

function ShowDocuments(cId, pgId) {
	window.radopen("ProductDocuments.aspx?cId=" + cId + "&pgId=" + pgId, "UserListDialog");
	return false;
}


// var images = ["Universal-Category-Banner.jpg", "PT web.jpg", "Metallic-web-size.jpg", "craftbanner.jpg", "Stone-web-size.jpg", "banner_special_purpose.jpg", "Primers_Category_banner.jpg", "Specialist_Applications_Category_banner.jpg"]

var images = ["front_panel_v2_01.jpg","front_panel_v2_02.jpg","front_panel_v2_03.jpg","front_panel_v2_04.jpg","front_panel_v2_05.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=\"" + "_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);
    });
}