$(document).ready(function () {
    $("#imgHelp").click(function () {
        dialog("To add images to your listing please follow the steps below<br/><br/>1.  From the html editor click on the tree icon.<br/><br/>2. In the general tab click on the little box to the right of the URL field.<br/><br/>3. Click on upload.<br/><br/>4.  Upload images<br/><br/>5. Double click the image you want.<br/><br/>6. Enter Image Description and Title if needed and edit the options in the Appearence tab if you want to get perfect placment.<br/><br/> 7.  Click Insert<br/><br/>8.  WARNING - Images will be entered in the size you upload them.<br/>Please cut images down to size before uploading.<br/><br/>", "info");
        return false
    });
    $(".confirm").click(function () {
        if (confirm("Are you sure?")) {
            return true
        } else {
            return false
        }
    });
    $("input[type=file]").change(function () {
        $(this).parent("div").parent("div").find("input[type=text]").attr("value", $(this).val())
    });
    $("#subscribe").submit(function () {
        var a = trim($("#subscribe input").val());
        if (a == "enter.your@email.com") {
            dialog("Please enter your e-mail address", "err")
        } else {
            if (a == "") {
                dialog("Please enter your e-mail address", "err")
            } else {
                if (!isEmail(a)) {
                    dialog("Please enter a valid e-mail address", "err")
                } else {
                    return true
                }
            }
        }
        return false
    });
    var e = function () {
            var b = $(".adv");
            for (var a = 0; a < b.length; a++) {
                if (!b[a].checked) {
                    $(".checkall").removeAttr("checked");
                    $(".check input").removeAttr("checked");
                    return true
                } else {
                    if (a == b.length - 1 && b[a].checked) {
                        $(".checkall").attr("checked", true);
                        $(".check input").attr("checked", true)
                    }
                }
            }
        };
    $(".adv").click(e);
    $("form.catsListings").submit(function () {
        var b = $("input.adv");
        for (var a = 0; a < b.length; a++) {
            if (b[a].checked) {
                return true
            }
        }
        dialog("Please select a franchise to request an information pack on by ticking the checkbox next to their listing", "err");
        return false
    });
    $("#adverts:not(.home) td").attr("valign", "top");
    $("#adverts td").hover(function () {
        $(this).addClass("hover")
    }, function () {
        $(this).removeClass("hover")
    });
    $("#subscribe input").focus(function () {
        var a = trim($(this).val());
        if (a == "enter.your@email.com") {
            $(this).attr("value", "")
        }
    });
    $("#subscribe input").blur(function () {
        var a = trim($(this).val());
        if (a == "") {
            $(this).attr("value", "enter.your@email.com")
        }
    });
    $("#franInfoTbl tr").attr("valign", "top");
    $("select[name=comp_sel]").change(function () {
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "intro=" + $("select[name=comp_sel]").val(),
            success: function (a) {
                if (trim($("textarea[name=text2]").val()) != "") {
                    if (confirm("Do you want to change Text2?") == true) {
                        $("textarea[name=text2]").text(a)
                    }
                } else {
                    $("textarea[name=text2]").text(a)
                }
            }
        })
    });
    $(".titleHelp").click(function () {
        dialog("<img src='images/Adv-BrowserTitleHelp.gif' alt=''/>", "suc");
        return false
    });
    $(".metaDescHelp").click(function () {
        dialog("<img src='images/Adv-MetaDescHelp.jpg' alt=''/>", "suc");
        return false
    });
    $(".h1Help").click(function () {
        dialog("<img src='images/Adv-H1Help.png' alt=''/>", "suc");
        return false
    });
    $(".h2Help").click(function () {
        dialog("<img src='images/Adv-H2Help.jpg' alt=''/>", "suc");
        return false
    });
    $(".text1Help").click(function () {
        dialog("<img src='images/Adv-Text1Help.jpg' alt=''/>", "suc");
        return false
    });
    $(".text2Help").click(function () {
        dialog("<img src='images/Adv-Text2Help.jpg' alt=''/>", "suc");
        return false
    });
    $(".metaKwHelp").click(function () {
        dialog("Meta keywords are keywords designed for the search engine to judge the relevance of you page on.<br/>If you are a cleaning franchise you could enter something like:<br/><br/>cleaning franchises,domestic cleaning franchise,franchise for sale<br/><br/>Please enter commas between each keyword/phrase you enter", "suc");
        return false
    });
    $("#right .colapse").click(function () {
        var a = $(this).next("ul").css("display");
        if (a == "none") {
            $(this).next("ul").show("normal")
        } else {
            $(this).next("ul").hide("normal")
        }
        return false
    });
    $(".showTestimonials").click(function () {
        dialog($(".testimonials").html(), "");
        return false
    });
    $("#moreContacts").click(function () {
        $("#joinForm label.text:last").after('<label><input type="text" name="contacts[]"/>Contact (' + ($("#joinForm label.text").size() + 1) + "):</label>");
        return false
    });
    $("#userNewsletter").keyup(function () {
        var a = $(this).val().length;
        var b = 500;
        if (a > b) {
            $("#remainingChars").html("You cannot write more then " + b + " characters!");
            $("#userNewsletter").val($(this).val().substr(0, b));
            return false
        } else {
            $("#remainingChars").html("You have " + (b - a) + " characters left.");
            return true
        }
    });
    $("#adverts img").error(function () {
        this.src = "/images/blank.png";
        this.onerror = "";
        return true
    });
    createEditor("article");
    $(".add2Cart").click(function () {
        var a = $(this).attr("class").replace(/add2Cart id_/, "");
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "add2basket=" + a,
            success: function (b) {
                $("#basket .count").html(b);
                $("#basket small").html(b == 0 ? "You have not selected any companies" : "You have " + b + " Company selected")
            }
        });
        $("a.add2Cart.id_" + a).hide();
        $("a.removeFromCart.id_" + a).show();
        $("#basket").fadeIn("slow");
        return false
    });
    $(".removeFromCart").click(function () {
        var a = $(this).attr("class").replace(/removeFromCart id_/, "");
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "removeFromCart=" + a,
            success: function (b) {
                $("#basket .count").html(b);
                $("#basket small").html(b == 0 ? "You have not selected any companies" : "You have " + b + " Company selected");
                if (b == 0) {
                    $("#basket").fadeOut("slow")
                }
            }
        });
        $("a.removeFromCart.id_" + a).hide();
        $("a.add2Cart.id_" + a).show();
        if ($("#theList").html() != null) {
            $(this).parents("tr").prev().fadeOut("slow", function () {
                $(this).remove()
            });
            $(this).parents("tr").next().fadeOut("slow", function () {
                $(this).remove()
            });
            $(this).parents("tr").fadeOut("slow", function () {
                $(this).remove();
                if ($("#catItems").find("tr").size() == 0) {
                    $("#mid").html('<h2 id="theList">Request List</h2>There are no items in your request list')
                }
            })
        }
        return false
    });
    $("#basket .leftArrow").disableTextSelect();
    $("#basket .leftArrow").click(function () {
        if ($("#basket .current").prev().html() != null) {
            $("#basket .current").prev().removeClass("hidden");
            $("#basket .current").addClass("hidden");
            $("#basket .current").prev().addClass("current");
            $("#basket .hidden").removeClass("current")
        } else {
            $("#basket .current").addClass("hidden");
            $("#basket .current").removeClass("current");
            $("#basket .item:last").removeClass("hidden");
            $("#basket .item:last").addClass("current")
        }
    });
    $("#basket .rightArrow").click(function () {
        if ($("#basket .current").next().html() != null) {
            $("#basket .current").next().removeClass("hidden");
            $("#basket .current").addClass("hidden");
            $("#basket .current").next().addClass("current");
            $("#basket .hidden").removeClass("current")
        } else {
            $("#basket .current").addClass("hidden");
            $("#basket .current").removeClass("current");
            $("#basket .item:first").removeClass("hidden");
            $("#basket .item:first").addClass("current")
        }
    });
    $("#basket .rightArrow").disableTextSelect();
    $("#hearAboutUs").change(function () {
        if ($(this).val() == "Other") {
            $("label.hearOther").css("display", "block")
        } else {
            $("label.hearOther").css("display", "none")
        }
    });
    $("#joinForm input").keydown(function (a) {
        if (goBaby) {
            goBaby = false;
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "unSentForm=" + window.location,
                success: function (b) {}
            })
        }
    });
    if (!readCookie("popup")) {
        if (typeof (showMatchPointPopup) != "undefined") {
            createCookie("popup", 1, 7);
            tb_show("MatchPoint Franchise Finding Service", "http://" + document.domain + "/matchpoint.html?KeepThis=true&TB_iframe=true&height=580&width=445", false)
        }
    }
    if (typeof (showRecommendedPopup) != "undefined") {
        tb_show("Recommended", "http://" + document.domain + "/recommended.html?KeepThis=true&TB_iframe=true&height=450&width=545", false)
    }
    $("#cna-franchise-overview").click(function () {
        tb_show("Franchise overview", "http://" + document.domain + "/cna-franchise-overview.html?KeepThis=true&TB_iframe=true&height=300&width=545", false)
    });
    $("#cna-franchise-video-testimonial").click(function () {
        tb_show("Franchise Video Testimonial", "http://" + document.domain + "/cna-franchise-video-testimonial.html?KeepThis=true&TB_iframe=true&height=300&width=545", false)
    });
    $(".video-link").click(function () {
        var a = $(this).attr("href");
        tb_show("Video", a + "&KeepThis=true&TB_iframe=true&height=400&width=580", false);
        return false
    });
    $("#basket").draggable({
        stop: function (b, a) {
            createCookie("basketPosition", $("#basket").css("left") + ", " + $("#basket").css("top"), 30)
        }
    });
    var g = readCookie("basketPosition");
    if (g) {
        var h = g.split(",");
        if (h[0].replace("px", "") > $(window).width() || h[1].replace("px", "") > $(window).height()) {
            h[0] = $("#right").offset().left + "px";
            h[1] = 275 + "px"
        }
        $("#basket").css({
            left: h[0],
            top: h[1]
        })
    }
    $("#resultsAddAll").click(function () {
        var a = "";
        $("#adverts .add2Cart:visible").each(function () {
            var b = $(this).attr("class").replace(/add2Cart id_/, "");
            a += "&id[]=" + b;
            $("a.add2Cart.id_" + b).hide();
            $("a.removeFromCart.id_" + b).show()
        });
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "resultsAddAll=1" + a,
            success: function (b) {
                $("#basket .count").html(b);
                $("#basket small").html(b == 0 ? "You have not selected any companies" : "You have " + b + " Company selected");
                window.location = "request-list.html#form"
            }
        });
        return false
    });
    try {
        $(".advertsSlider").slider({
            animate: true,
            max: 12,
            slide: handleSliderSlide
        })
    } catch (f) {
        alert(f)
    }
    $(".slidingAdverts a").click(function () {
        var a = $(this).attr("id").replace(/id_/, "");
        $.ajax({
            type: "GET",
            url: "ajax.php",
            data: "advertClick=1&type=sliding_adverts&refferal=" + encodeURIComponent(window.location) + "&id=" + a
        })
    });
    $(".slidingAdverts a").tooltip({
        track: true,
        opacity: 1,
        bodyHandler: function () {
            return $(".slidingAdvertsDesc div").eq($(this).index()).html()
        },
        showURL: false
    });
    $("#toggleCommentForm").click(function () {
        if ($("#commentFormFields").hasClass("hidden")) {
            $("#commentFormFields").removeClass("hidden")
        } else {
            $("#commentFormFields").addClass("hidden")
        }
        return false
    });
    /*$("#rotatingMatchpointAdvert").cycle({
        fx: "fade",
        speed: 700,
        timeout: 4000,
        pause: true,
        pauseOnPagerHover: true
    });*/
    /*if (!readCookie("dvd_popup")) {
        createCookie("dvd_popup", 1, 7);
        tb_show("Free Franchise DVD", "http://" + document.domain + "/free-franchise-dvd.html?KeepThis=true&TB_iframe=true&height=460&width=950", false)
    }*/
		/*if (!readCookie("free_consultation")) {
        createCookie("free_consultation", 1, 7);
        tb_show("Free Consultation", "http://" + document.domain + "/free-consultation.html?KeepThis=true&TB_iframe=true&height=460&width=950", false)
    }*/
});

function handleSliderSlide(f, d) {
    try {
        $(".slidingAdverts a").filter(function (a) {
            return a < d.value
        }).hide();
        $(".slidingAdverts a").filter(function (a) {
            return a >= d.value
        }).show()
    } catch (e) {
        alert(e)
    }
}
var goBaby = true;

