// THEATOM GROUP // Ocean Spray // common.js //

var signInRedirect = null;

jQuery(function ($) {

    if (!IsAuthenticated) {
        // user is not logged in. if they click "submit a recipe" then we'll catch it.
        $("a[href*=Submit-a-Recipe.aspx]").live("click", function (e) {
            e.preventDefault();
            signInRedirect = $(e.target).attr("href");
            $(".top-sign-in-button").click();
        });
    }

    // Cranberry Club Module
    var canHover = true,
        tweetIframe = $("div.share .twitter iframe"),
        getOpenGraphTitle = function () {
            var titleEl = $("meta[property=og:title]");
            if (titleEl.length) {
                return $(titleEl).attr("content");
            } else {
                return "";
            }
        };

    $("#cran-club").hover(
		function () {
		    if (canHover) {
		        $("#cran-club").stop(true).animate({
		            height: '177px', top: '-112px'
		        }, 500, function () { canHover = false; });
		    }
		},
		function () {
		    $("#cran-club").stop(true).animate({
		        height: '65px', top: '0'
		    }, 500, function () { canHover = true; })
		});


    // sign in triggers
    $(".show-sign-in").live("click", function (e) {
        signInRedirect = null;
        $(".top-sign-in-button").click();
    });

    // Primary Nav Mega Menu
    $("#primaryNav > li").hover(function () {
        $(this).children(".mega-menu").stop().css("opacity", 0).show().animate({ "opacity": 1.0 }, 150);
    },
        function () {
            $(this).children(".mega-menu").stop().animate({ "opacity": 0.0 }, 150, function () { $(this).hide(); });
        }
    );

    // Toggle Value Call
    $("#side-nav .search .searchBar").toggleValue("Search all recipes");
    // global search
    var global_search = $("#search");
    global_search.find(".txt").toggleValue("Search site");
    global_search.find(".btn").live("click", function (e) {
        var val = global_search.find(".txt")[0].value;
        if (val === "Search site" || val === "") {
            e.preventDefault();
        }
    });

    // Horizontal Slideshow Hover Effect
    $(".horizontal-slideshow .controls li .photo-wide-md").hover(
		function () {
		    this.className = "photo-md-hover";
		    $(this).children(".recipe-info").show();
		},
		function () {
		    this.className = "photo-wide-md";
		    $(this).children(".recipe-info").hide();
		}
    );

    // Google analytics event tracking
    $(".ga-track-event").live("click", function () {
        if ($(this).hasClass("print")) {
            _gaq.push(['_trackEvent', 'Recipe-Engagement', 'Print-Recipe', getOpenGraphTitle()]);
        } else if ($(this).hasClass("add-recipe-review")) {
            _gaq.push(['_trackEvent', 'Recipe-Engagement', 'Write-a-Review', getOpenGraphTitle()]);
        } else if ($(this).hasClass("add-to-recipe-box")) {
            _gaq.push(['_trackEvent', 'Recipe-Engagement', 'Add-to-Recipe-Box', getOpenGraphTitle()]);
        } else if ($(this).hasClass("email-submit")) {
            _gaq.push(['_trackEvent', 'Recipe-Engagement', 'Email-Recipe', getOpenGraphTitle()]);
        } else if ($(this).hasClass("site-invite")) {
            _gaq.push(['_trackEvent', 'Post-Sign-up-Engagement', 'Invite-a-Contact']);
        }


    });

    if (tweetIframe.length) {
        IframeOnClick.track($(tweetIframe).get(0), function () {
            _gaq.push(['_trackEvent', 'Recipe-Engagement', 'Tweet-Recipe', getOpenGraphTitle()]);
        });
    }

    // this root element is required by the Facebook API
    $('body').append('<div id="fb-root"></div>');

    $.getScript(document.location.protocol +
        '//connect.facebook.net/en_US/all.js');

    // this function executes once Facebook's API has finished loading
    window.fbAsyncInit = function () {
        FB.init({
            appId: 218957904798591,
            xfbml: true
        });

        // Like button functionality
        FB.Event.subscribe('edge.create', function (href, widget) {
            var buttonID = $(widget.dom).attr('id'),
                itemName = getOpenGraphTitle();

            // Track engagement with google analytics
            if (buttonID === "brand-like") {
                _gaq.push(['_trackEvent', 'Brand-Engagement', 'Facebook-Like', 'Brand']);
            } else if (buttonID === "product-like") {
                _gaq.push(['_trackEvent', 'Product-Engagement', 'Facebook-Like', itemName]);
            } else if (buttonID === "share-like") {
                _gaq.push(['_trackEvent', 'Recipe-Engagement', 'Facebook-Like', itemName]);
            }

        });
    };
});

// Toggle Value
(function ($) {
    $.fn.toggleValue = function(text){
        var self = this;
        return self.each(function(){
            self.attr("value", text);
            self.focus(function () { if (this.value === text) $(this).attr("value", ""); });
            self.blur(function () { if (this.value === text || this.value === "") $(this).attr("value", text); });
        });
    };
} (this.jQuery));
