var $j = jQuery.noConflict();
$j(document).ready(function () {
    $j("#thumbDiv").mouseover(function () {
        $j("#venue").fadeOut("slow");
    });
    $j("#bigDiv").mouseout(function () {
        $j("#venue").fadeIn("slow");
    });
    $j("#startDate").DatePicker({
        format: "d/m/Y",
        date: $j("#startDate").val(),
        current: $j("#startDate").val(),
        starts: 1,
        position: "b",
        onBeforeShow: function () {
            $j("#startDate").DatePickerSetDate($j("#startDate").val(), true);
        },
        onChange: function (formated, dates) {
            $j("#startDate").val(formated);
            $j("#startDate").DatePickerHide();
        }
    });
    $j(".topButton").click(function () {
        $j("#startDate").DatePickerShow();
        return false;
    });
    $j("#homeNewsPodImage img").attr("src", $j(".homeNewsPodImageHidden1 img").attr("src"));
    $j("#homeNewsPodImage img").attr("title", $j(".homeNewsPodImageHidden1 img").attr("title"));
    $j("#homeNewsPodImage img").attr("alt", $j(".homeNewsPodImageHidden1 img").attr("alt"));
    $j(".article1").hover(function () {
        $j("#homeNewsPodImage img").attr("src", $j(".homeNewsPodImageHidden1 img").attr("src"));
        $j("#homeNewsPodImage img").attr("title", $j(".homeNewsPodImageHidden1 img").attr("title"));
        $j("#homeNewsPodImage img").attr("alt", $j(".homeNewsPodImageHidden1 img").attr("alt"));
    });
    $j(".article2").hover(function () {
        $j("#homeNewsPodImage img").attr("src", $j(".homeNewsPodImageHidden2 img").attr("src"));
        $j("#homeNewsPodImage img").attr("title", $j(".homeNewsPodImageHidden2 img").attr("title"));
        $j("#homeNewsPodImage img").attr("alt", $j(".homeNewsPodImageHidden2 img").attr("alt"));
    });
    $j(".article3").hover(function () {
        $j("#homeNewsPodImage img").attr("src", $j(".homeNewsPodImageHidden3 img").attr("src"));
        $j("#homeNewsPodImage img").attr("title", $j(".homeNewsPodImageHidden3 img").attr("title"));
        $j("#homeNewsPodImage img").attr("alt", $j(".homeNewsPodImageHidden3 img").attr("alt"));
    });
    jQuery.fn.fadeIn = function (speed, callback) {
        return this.animate({
            opacity: "show"
        }, speed, function () {
            if (jQuery.browser.msie) {
                this.style.removeAttribute("filter");
            }
            if (jQuery.isFunction(callback)) {
                callback();
            }
        });
    };
    jQuery.fn.fadeOut = function (speed, callback) {
        return this.animate({
            opacity: "hide"
        }, speed, function () {
            if (jQuery.browser.msie) {
                this.style.removeAttribute("filter");
            }
            if (jQuery.isFunction(callback)) {
                callback();
            }
        });
    };
    $j("#eventType").change(function () {
        $j("#eventVenues ul li").fadeOut("fast");
        $j("#eventVenues ul").hide("normal", function () {
            $j("#eventVenues ul li, #eventVenues ul").empty();
            $j.getJSON("/index.php?c=venue-finder&a=get-locations&id=" + $j("#eventType").val(), function (locations) {
                $j("#eventVenues ul").html("");
                $j.each(locations, function (i, item) {
                    $j("#eventVenues ul").removeAttr("opacity").append('<li><a href="/venues/' + item.urlTitle + '" title="' + item.location + " - " + item.name + '">' + item.location + " - " + item.name + "</a></li>").fadeIn("fast");
                });
                $j("#eventVenues ul li").show("fast");
            });
        });
    });
    $j("#terms-acceptance").submit(function () {
        if (!$j("#terms-acceptance-field").val() == "1") {
            return false;
        } else {
            return true;
        }
    });
    $j(".videoLink").click(function () {
        $j(".videoHolder").ajaxStart(function () {
            $j(this).empty().html('<img src="/images/ajax-loader.gif" align="center" style="padding-top: 90px; padding-left: 200px;" />');
            return false;
        });
        $j("#videoTitle").html($j(this).attr("title")).fadeIn();
        $j.ajax({
            type: "POST",
            url: "/index.php?c=videos&a=find-video&id=" + $j(this).attr("rel") + "&videoType=" + $j(this).attr("rel"),
            dataType: "json",
            success: function (videos) {
                $j(".videoHolder").html(videos).fadeIn();
            }
        });
        return true;
    });
    $j("#email_address").blur(function () {
        $j("a#signUpLink").attr("href", "/sign-up/modal?email_address=" + $j("#email_address").val());
        $j("a#signUpLink").attr("rel", "shadowbox; width=550; height=580;");
        Shadowbox.setup();
        return false;
    });
    $j(".closeShadowBox").click(function () {
        window.parent.Shadowbox.close();
        return false;
    });
    $j(".reloadParent").click(function () {
        parent.location.reload();
        window.parent.Shadowbox.close();
        return false;
    });
    $j(".popup").click(function () {
        window.open($j(this).attr("href"));
        return false;
    });
    $j(".back").click(function () {
        history.back();
        return false;
    });
    $j(".printCurrentWindow").click(function () {
        window.print();
        return false;
    });

    //Send action request to Google and Clicky for outbound links
    $j("a[href*='http://']:not([href*='" + window.location.hostname + "'])").bind("click", function (e) {
        _gaq.push(["_trackEvent", "Outbound", jQuery(this).context.hostname, jQuery(this).attr("href")]);
        clicky.log($j(this).attr("href"), $j(this).attr("title"), "outbound");
    });

    //Send an action request to google for which search button was pressed.
    $j('.searchButton').click(function () {
        _gaq.push(["_trackEvent", "Search Form", "Search Form Used", $j(this).attr('id')]);
    });

    //Send action request to google for video plays
    $j("a[href*=.flv]").flowplayer("/swf/flowplayer-3.2.1.swf", {
        key: "#$7def0ea747ae46b1052",
        plugins: {
            controls: {
                url: "/swf/flowplayer.controls-3.2.0.swf",
                buttonColor: "rgba(0, 0, 0, 0.9)",
                buttonOverColor: "#000000",
                backgroundColor: "#A6A8AB",
                backgroundGradient: "medium",
                sliderColor: "#FFFFFF",
                sliderBorder: "1px solid #808080",
                volumeSliderColor: "#FFFFFF",
                volumeBorder: "1px solid #808080",
                timeColor: "#000000",
                durationColor: "#535353"
            }
        },
        clip: {
            autoPlay: true
        },
        canvas: {
            backgroundColor: '#987290;',
            backgroundGradient: 'medium'
        },
        onLoad: function() {
           _gaq.push(["_trackEvent", "Video Play", "Play", $j("a[href*=.flv]").attr('title')]);
            clicky.log($j("a[href*=.flv]").attr('href'), $j("a[href*=.flv]").attr('title'), "download");
        },
        onBeforeFinish: function () {
            $j(this).unload();
        }
    });

    //Set up validation for top form
    //Sets the messages that appear on the form labels to nowt
    jQuery.validator.messages.required = "";

    //
    // front end validation of form submission
    // submitHandler: fires when successful form sent through
    //
    $j('#headerAvailability').validate({
        rules: {
            delegates: "required"
        },
        success: function() {
            _gaq.push(['_trackPageview', '/top-search-form']);
        },
        errorElement: "em"
     });
});