﻿var CycleNI = {

    showDisclaimer: function() {
        alert("CYCLENI.COM DISCLAIMER\n\nEvery care has been taken to ensure accuracy of the information. We cannot accept responsibility for errors or omissions but where such are brought to our attention, the information will be amended accordingly.\n\nWhilst all the clubs, associations and activity operators listed on this website generally operate according to which is accepted as current best practice, it is the responsibility of the participant to ensure that they are credible and all appropriate safety standards are adhered to. The Countryside Access and Activities Network have, nor assume, any responsibility for the accuracy or the completeness of the information supplied or the service and level of care afforded by any of the clubs, associations and activity operators listed on this website.");
    },

    joinMailingList: function() {
        var email = Phizz.Tools.TrimString(Phizz.Dom.ByID("txtMailingList_Email").value);
        if (!Phizz.Validation.IsEmail(email)) {
            alert("Please enter a valid email address and try again.");
            Phizz.Dom.ByID("txtMailingList_Email").focus();
            return false;
        } else {
            return true;
        }
    },

    Competition: {
        validate: function() {
            return Phizz.Validation.Dropdown("ctl00_cphPage_selTitle")
		        && Phizz.Validation.Value("ctl00_cphPage_txtFirstName")
		        && Phizz.Validation.Value("ctl00_cphPage_txtSurname")
		        && Phizz.Validation.Email("ctl00_cphPage_txtEmailAddress", false)
		        && Phizz.Validation.Value("ctl00_cphPage_txtPhoneNumber", false)
		        && Phizz.Validation.Value("ctl00_cphPage_txtAddress")
		        && Phizz.Validation.Value("ctl00_cphPage_txtTown")
		        && Phizz.Validation.Value("ctl00_cphPage_txtCounty")
		        && Phizz.Validation.Value("ctl00_cphPage_txtPostcode");
        }
    },

    Rotator: {
        objImgBack: null,
        objImgFront: null,
        urls: null,
        interval: null,
        index: 0,
        opacity: 0,
        init: function(objImg, urls, interval, randomise) {
            this.interval = interval;
            if (randomise) {
                this.urls = new Array(urls.length);
                for (var i = 0; i < this.urls.length; i++) this.urls[i] = urls.splice(Math.floor(Math.random() * urls.length), 1)[0];
            } else {
                this.urls = urls;
            }
            for (var i = 0; i < this.urls.length; i++) {
                if (this.urls[i].substr(this.urls[i].lastIndexOf("/") + 1) == objImg.src.substr(objImg.src.lastIndexOf("/") + 1)) {
                    var s = this.urls.splice(i, 1)[0];
                    this.urls.push(s);
                    break;
                }
            }

            this.objImgBack = objImg;
            this.objImgFront = document.createElement("img");
            this.objImgFront.setAttribute("style", "position:absolute;visibility:hidden;left:0px;top:0px;opacity:0;-moz-opacity:0;-khtml-opacity:0;filter:alpha(opacity=0);width:".concat(objImg.offsetWidth, "px;height:", objImg.offsetHeight, "px;"));
            this.objImgFront.setAttribute("alt", "");
            this.objImgFront.setAttribute("src", this.urls[0]);
            /* IE doesn't use predefinied css string */this.objImgFront.style.filter = "alpha(opacity=0)";
            objImg.parentNode.appendChild(this.objImgFront);

            setTimeout(function() { CycleNI.Rotator.fadeInNext(); }, this.interval);
        },
        fadeInNext: function() {
            this.opacity = 0;
            this.objImgFront.style.visibility = "visible";
            setTimeout(function() { CycleNI.Rotator.fadeStep(); }, 40);
        },
        fadeStep: function() {
            if (this.objImgFront.style.opacity >= 1) {
                this.onAfterFade();
            } else {
                this.opacity += 0.06;
                this.objImgFront.style.opacity = this.objImgFront.style.MozOpacity = this.objImgFront.style.KhtmlOpacity = this.opacity.toString();
                this.objImgFront.style.filter = "alpha(opacity=" + (this.opacity * 100).toString() + ")";
                setTimeout(function() { CycleNI.Rotator.fadeStep(); }, 40);
            }
        },
        onAfterFade: function() {
            this.index = this.index == this.urls.length - 1 ? 0 : this.index + 1;
            this.objImgBack.src = this.objImgFront.src; // TODO: Is this sometimes causing flicker?
            this.objImgFront.style.visibility = "hidden";
            this.objImgFront.style.opacity = this.objImgFront.style.MozOpacity = this.objImgFront.style.KhtmlOpacity = "0";
            this.objImgFront.style.filter = "alpha(opacity=0)";
            this.objImgFront.src = this.urls[this.index];
            setTimeout(function() { CycleNI.Rotator.fadeInNext(); }, this.interval);
        }
    },

    KeywordSearch: {
        onFocus: function(obj) {
            if (Phizz.Tools.TrimString(obj.value) == "Route Search") {
                obj.value = "";
                obj.style.color = "#3a3a3a";
            }
        },
        onBlur: function(obj) {
            if (Phizz.Tools.TrimString(obj.value) == "") {
                obj.value = "Route Search";
                obj.style.color = "#999999";
            }
        },
        onKeyPress: function(obj, e) {
            if (!e) e = window.event;
            if (e.keyCode == 13) {
                Phizz.Navigation.Go("/search/?k=" + encodeURIComponent(Phizz.Tools.TrimString(obj.value)))
            }
        }
    },

    CountyMap: {
        setCounty: function(name) {
            Phizz.Dom.ByID("CountyMap").src = "/i/countymap/" + name + ".png";
            if (Phizz.Browser.IsIE6) Phizz.Tools.EnableIE6ImageTransparency(Phizz.Dom.ByID("CountyMap"));
        },
        reset: function() {
            Phizz.Dom.ByID("CountyMap").src = "/i/_.gif";
            if (Phizz.Browser.IsIE6) Phizz.Dom.ByID("CountyMap").style.filter = "";
        }
    },

    RouteList: {
        objSelected: null,
        disableOnMouseOver: false,

        Row: {
            onMouseOver: function(obj) {
                if (CycleNI.RouteList.disableOnMouseOver) {
                    CycleNI.RouteList.disableOnMouseOver = false;
                } else {
                    if (CycleNI.RouteList.objSelected != null) CycleNI.RouteList.objSelected.style.backgroundColor = "transparent";
                    CycleNI.RouteList.objSelected = obj;
                    CycleNI.RouteList.objSelected.style.backgroundColor = "#e7ebd3";
                    if (Phizz.Dom.ByID("RouteListCountyMap") != null) Phizz.Dom.ByID("RouteListCountyMap").focusPin(obj.id.substr(1));
                }
            },
            onClick: function(obj) {
                Phizz.Navigation.Go(obj.firstChild.firstChild.getAttribute("href"));
            }
        },
        Pin: {
            onMouseOver: function(id) {
                if (Phizz.Dom.ByID("RouteListRows") == null) { CycleNI.ProviderList.Pin.onMouseOver(id); return; }
                var currentScrollTop = Phizz.Dom.ByID("RouteListRows").scrollTop;
                var currentScrollBottom = currentScrollTop + Phizz.Dom.ByID("RouteListRows").offsetHeight;
                var targetScroll = Phizz.Dom.ByID("r" + id).offsetTop;
                if (targetScroll < currentScrollTop || targetScroll > currentScrollBottom - 20) {
                    CycleNI.RouteList.disableOnMouseOver = true;
                    Phizz.Dom.ByID("RouteListRows").scrollTop = targetScroll;
                }
                if (CycleNI.RouteList.objSelected != null) CycleNI.RouteList.objSelected.style.backgroundColor = "transparent";
                CycleNI.RouteList.objSelected = Phizz.Dom.ByID("r" + id);
                CycleNI.RouteList.objSelected.style.backgroundColor = "#e7ebd3";
            },
            onClick: function(id) {
                if (Phizz.Dom.ByID("RouteListRows") == null) { CycleNI.ProviderList.Pin.onClick(id); return; }
                Phizz.Navigation.Go(Phizz.Dom.ByID("r" + id).firstChild.firstChild.getAttribute("href"));
            }
        }
    },

    ProviderList: {
        objProviderList: null,
        objSelected: null,
        objProviderBox: null,
        disableOnMouseOver: false,

        Row: {
            onMouseOver: function(obj, mapID) {
                if (CycleNI.ProviderList.disableOnMouseOver) {
                    CycleNI.ProviderList.disableOnMouseOver = false;
                } else {
                    if (CycleNI.ProviderList.objSelected != null) CycleNI.ProviderList.objSelected.style.backgroundColor = "transparent";
                    CycleNI.ProviderList.objSelected = obj;
                    CycleNI.ProviderList.objSelected.style.backgroundColor = "#e7ebd3";
                    if (Phizz.Dom.ByID(mapID) != null) Phizz.Dom.ByID(mapID).focusPin(obj.id.substr(1));
                }
            },
            onClick: function(obj) {
                CycleNI.ProviderList.ProviderBox.show(obj.id.substr(1));
            }
        },
        Pin: {
            onMouseOver: function(id) {
                var myProviderList = $(CycleNI.ProviderList.objProviderList).find(".ProviderList").get(0);
                var currentScrollTop = myProviderList.scrollTop;
                var currentScrollBottom = currentScrollTop + myProviderList.offsetHeight;
                var targetScroll = Phizz.Dom.ByID("r" + id).offsetTop;
                if (targetScroll < currentScrollTop || targetScroll > currentScrollBottom - 20) {
                    CycleNI.ProviderList.disableOnMouseOver = true;
                    myProviderList.scrollTop = targetScroll;
                }
                if (CycleNI.ProviderList.objSelected != null) CycleNI.ProviderList.objSelected.style.backgroundColor = "transparent";
                CycleNI.ProviderList.objSelected = Phizz.Dom.ByID("r" + id);
                CycleNI.ProviderList.objSelected.style.backgroundColor = "#e7ebd3";
                if (CycleNI.ProviderList.objProviderBox != null) CycleNI.ProviderList.ProviderBox.show(id);
            },
            onClick: function(id) {
                CycleNI.ProviderList.ProviderBox.show(id);
            }
        },
        ProviderBox: {
            show: function(id) {
                if (CycleNI.ProviderList.objProviderBox != null) this.close();
                CycleNI.ProviderList.objProviderBox = Phizz.Dom.ByID("p" + id);
                CycleNI.ProviderList.objProviderBox.style.display = "block";
            },
            close: function() {
                if (CycleNI.ProviderList.objProviderBox != null) {
                    CycleNI.ProviderList.objProviderBox.style.display = "none";
                    CycleNI.ProviderList.objProviderBox = null;
                }
            }
        }
    },

    Events: {
        filter: function() {
            Phizz.Navigation.Go("/events/?v=" + Phizz.Dom.ByID("selView").value + (Phizz.Dom.ByID("selCounty").value == "0" ? "" : "&c=" + Phizz.Dom.ByID("selCounty").value));
        }
    },

    CyclingInNI: {
        objActive: null,
        showCounty: function(n) {
            if (this.objActive != null) this.objActive.style.visibility = "hidden";
            this.objActive = Phizz.Dom.ByID("cini" + n);
            this.objActive.style.visibility = "visible";
            Phizz.Dom.ByID("CyclingInNIClose").style.visibility = "visible";
        },
        hideCounty: function() {
            Phizz.Dom.ByID("CyclingInNIClose").style.visibility = "hidden";
            if (this.objActive != null) {
                this.objActive.style.visibility = "hidden";
                this.objActive = null;
            }
        }
    },

    NCN: {
        objActive: null,
        showRoute: function(n) {
            if (this.objActive != null) this.objActive.style.visibility = "hidden";
            this.objActive = Phizz.Dom.ByID("ncn" + n);
            this.objActive.style.visibility = "visible";
            Phizz.Dom.ByID("NCNClose").style.visibility = "visible";
        },
        hideRoute: function() {
            Phizz.Dom.ByID("NCNClose").style.visibility = "hidden";
            if (this.objActive != null) {
                this.objActive.style.visibility = "hidden";
                this.objActive = null;
            }
        }
    },

    Gallery: {
        entity: "",
        currentImage: -1,
        ids: new Array(),
        show: function(entity, id, image) {
            Phizz.Navigation.OpenWindow("/Gallery.aspx?entity=" + entity + "&id=" + id + "&image=" + (image == "" ? "0" : image), 900, 690, "winGallery", false);
        },
        displayImage: function(index, autoScroll) {
            if (this.currentImage != -1) {
                Phizz.Dom.ByID("i" + this.currentImage).style.border = "solid 1px #bbbbbb";
                Phizz.Dom.ByID("i" + this.currentImage).style.padding = "4px";
            }
            this.currentImage = index;
            Phizz.Dom.ByID("GalleryImage").innerHTML = "<img src=\"/Ti.ashx?p=" + this.entity + "%5c" + this.ids[index] + ".jpg&mw=680&mh=480\" />";
            Phizz.Dom.ByID("i" + this.currentImage).style.border = "solid 2px #000000";
            Phizz.Dom.ByID("i" + this.currentImage).style.padding = "3px";
            if (autoScroll) Phizz.Dom.ByID("i" + this.currentImage).scrollIntoView();
        },
        previous: function() {
            this.displayImage(this.currentImage == 0 ? this.ids.length - 1 : this.currentImage - 1, true);
        },
        next: function() {
            this.displayImage(this.currentImage == this.ids.length - 1 ? 0 : this.currentImage + 1, true);
        }
    }

}

function r1(obj) { CycleNI.RouteList.Row.onMouseOver(obj); }
function r2(obj) { CycleNI.RouteList.Row.onClick(obj); }
function p1(obj, mapID) { CycleNI.ProviderList.Row.onMouseOver(obj, mapID); }
function p2(obj) { CycleNI.ProviderList.Row.onClick(obj); }

Phizz.Dom.RegisterDOMReadyEvent(function() {
    if (Phizz != null && Phizz.SWFText != null) {
        Phizz.SWFText.minHeight = 35;
        Phizz.SWFText.embedByTagAndClass("H1", "SWFText", "/i/HelveticaLight.swf", "32", "000000", "left");
    }

    if (Phizz.Browser.IsIE6) {
        var obj, objs;

        obj = Phizz.Dom.ByID("MapLabel");
        if (obj != null) Phizz.Tools.EnableIE6ImageTransparency(obj.firstChild);

        obj = Phizz.Dom.ByID("SectionMenu");
        if (obj != null) for (var i = 0; i < obj.childNodes.length; i++) Phizz.Tools.EnableIE6BackgroundTransparency(obj.childNodes[i], "/i/SectionMenu-BG" + (i % 3) + ".png");

        obj = Phizz.Dom.ByID("RouteOptions");
        if (obj != null) {
            obj = obj.firstChild; if (obj.nodeName == "TBODY") obj = obj.firstChild;
            for (var i = 0; i < obj.childNodes.length; i++) Phizz.Tools.EnableIE6BackgroundTransparency(obj.childNodes[i].firstChild, "/i/RouteOption-BG" + (obj.childNodes[i].firstChild.className == "Alt" ? "2" : "") + ".png");
        }

        objs = Phizz.Dom.ByClass("WeatherIcon", "DIV");
        for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6ImageTransparency(objs[i].firstChild);

        objs = Phizz.Dom.ByClass("NewsListingImage", "DIV");
        for (var i = 0; i < objs.length; i++) if (objs[i].firstChild.src.indexOf("_.gif") < 0) Phizz.Tools.EnableIE6BackgroundTransparency(objs[i].firstChild, "/i/NewsListingImage-BG.png");

        objs = Phizz.Dom.ByClass("NewsArticleImages", "DIV");
        for (var i = 0; i < objs.length; i++) for (var j = 0; j < objs[i].childNodes.length; j++) if (objs[i].childNodes[j].firstChild.src.indexOf("_.gif") < 0) Phizz.Tools.EnableIE6BackgroundTransparency(objs[i].childNodes[j].firstChild, "/i/NewsListingImage-BG.png");

        objs = Phizz.Dom.ByClass("PanelImage", "IMG");
        for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6BackgroundTransparency(objs[i], "/i/PanelImage-BG.png");

        objs = Phizz.Dom.ByClass("RouteImage", "IMG");
        for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6BackgroundTransparency(objs[i], "/i/RouteImage-BG.png");

        objs = Phizz.Dom.ByClass("Button", "IMG");
        for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6ImageTransparency(objs[i]);

        objs = Phizz.Dom.ByClass("Button", "INPUT");
        for (var i = 0; i < objs.length; i++) Phizz.Tools.EnableIE6ImageTransparency(objs[i]);

        obj = Phizz.Dom.ByID("NCNClose");
        if (obj != null) Phizz.Tools.EnableIE6ImageTransparency(obj.firstChild.firstChild);

        obj = Phizz.Dom.ByID("CyclingInNIClose");
        if (obj != null) Phizz.Tools.EnableIE6ImageTransparency(obj.firstChild.firstChild);

        //var obj = Phizz.Dom.ByID("Help_Top");
        //if (obj != null) Phizz.Tools.EnableIE6BackgroundTransparency(obj, "/i/suggest/HelpBubble-BG.png");

        obj = Phizz.Dom.ByID("Help_Base");
        if (obj != null) Phizz.Tools.EnableIE6ImageTransparency(obj);
    }
});

Phizz.Dom.RegisterWindowLoadEvent(function() {
    Phizz.Tools.PreloadImage("/i/countymap/Antrim.png", 326, 264);
    Phizz.Tools.PreloadImage("/i/countymap/Armagh.png", 326, 264);
    Phizz.Tools.PreloadImage("/i/countymap/Londonderry.png", 326, 264);
    Phizz.Tools.PreloadImage("/i/countymap/Down.png", 326, 264);
    Phizz.Tools.PreloadImage("/i/countymap/Fermanagh.png", 326, 264);
    Phizz.Tools.PreloadImage("/i/countymap/Tyrone.png", 326, 264);
});


$(function() {

    ratings = ["Very Poor", "Poor", "OK", "Good", "Very Good"];

    $("#Rating td.stars").mousemove(function(e) {
        var rating = Math.min(Math.floor((e.pageX - $(this).offset().left) / 23) + 1, 5);
        $(this).css("background-position", "0px " + (-115 + rating * 23) + "px");
        $("#Rating td.label").html(ratings[rating]);
    }).mouseleave(function() {
        var rating = $("input[name=Rating]").val();
        if (rating.length > 0) {
            $(this).css("background-position", "0px " + (-115 + rating * 23) + "px");
             $("#Rating td.label").html(ratings[rating]);
        } else {
            $(this).css("background-position", "0px -115px");
             $("#Rating td.label").html("<img src=\"/i/click-to-rate.png\" alt=\"click to rate\" />");
        }
    }).click(function(e) {
        $("input[name=Rating]").val(Math.min(Math.floor((e.pageX - $(this).offset().left) / 23) + 1, 5));
    });

});