// JavaScript Document

curNavSection = "";
curSubNavSection = "";

var Contact = {
    onComplete: function(content) {
        var result = eval(content.get_response().get_object());

        var textNode = document.createTextNode(result.message);

        document.getElementById("result").appendChild(textNode);
    }
};

function validateAJAXForm(context) {
    return $(this).valid();
}

function navigateToLocationDetail() {
    window.open("#theDetail", "_self");
}


function preview(swfFile, dur) {
    $.fancybox('<p>Loading video...</p>', {
        'autoDimensions': false,
        'width': 720,
        'height': 480,
        'transitionIn': 'none',
        'transitionOut': 'none',

        'onComplete': function() {
            player = $f("fancybox-inner", { src: "/Content/swf/flowplayer/flowplayer-3.2.0.swf", wmode: 'opaque' }, {

                play: { opacity: 0 },
                //key: '#$flowplayerkeycode',

                plugins: {
                    lighttpd: {
                        url: '/Content/swf/flowplayer/flowplayer.pseudostreaming-3.2.0.swf'
                    }
                },
                clip: {
                    autoPlay: true,
                    url: 'http://vod01.netdna.com/vod/foabuildashow.ingramenterprise/demos/low/' + swfFile,
                    provider: 'lighttpd',
                    onStart: function(clip) {
                        var wrap = jQuery(this.getParent());
                        var clipwidth = clip.metaData.width;
                        var clipheight = clip.metaData.height;
                        var pos = $.fn.fancybox.getViewport();
                        $("#fancy_outer").css({ width: clipwidth + 20, height: clipheight + 20 });
                        $("#fancy_outer").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth - 36) / 2)));
                        $("#fancy_outer").css('top', ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50) / 2)));
                        $("#fancy_right, #fancy_left").css({ height: clipheight - 60, bottom: '70px' });

                    },
                    onFinish: function() {
                        $('#fancybox-close').trigger('click');
                    }
                }
            });


            player.load();


        }
    });
}

function inpageVideo(f) {
    $f("stream", "/Content/swf/flowplayer/flowplayer-3.2.0.swf", {

        clip: {
            autoPlay: true,
            url: 'http://vod01.netdna.com/vod/foabuildashow.ingramenterprise/'+f,
            provider: 'lighttpd'
        },

        plugins: {
            lighttpd: {
                url: '/Content/swf/flowplayer/flowplayer.pseudostreaming-3.2.0.swf'
            }
        }
    });
}







$(document).ready(function() {


    $("form").each(function() { $(this).validate({ errorClass: "input-validation-error" }); });
    $('#btnSearch').click(function() {
        $("#searchForm").submit();
        return false;
    });
    //Form field defaults
    $(".defaultText").focus(function(srcc) {
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".defaultText").blur(function() {
        if ($(this).val() == "") {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });

    $(".defaultText").blur();

    //Fade Rollover Code
    $(".ui-fade-rollover").each(function() {
        if ($(this).attr("ui-fade-rollover-effect_loaded") == "true") return;

        $(this).attr("ui-fade-rollover-effect_loaded", "true");

        if ($(this).attr("alt") != null && curNavSection != "" && $(this).attr("alt").indexOf(curNavSection) != -1)
            $(this).attr("src", $(this).attr("src").replace("_off", "_over"));

        if ($(this).attr("alt") != null && curSubNavSection != "" && $(this).attr("alt").indexOf(curSubNavSection) != -1)
            $(this).attr("src", $(this).attr("src").replace("_off", "_over"));

        var wrapper = "<div style='display: block; height: auto; width: auto; background-repeat: no-repeat; background-image: url(" + $(this).attr("src").replace("_off", "_over") + ")'></div>";
        //alert(wrapper);
        if ($(this).parent().get(0).tagName == "A")
            $(this).parent().wrap(wrapper);
        else
            $(this).wrap(wrapper);

        $(this).bind("mouseenter", function(e) {
            $(this).stop().animate({ opacity: 0 }, 300);
        });
        $(this).bind("mouseleave", function(e) {
            $(this).stop().animate({ opacity: 1 }, 300);
        });
    });



    //Category Image Rollover
    $.preload('#main img.swap', {
        find: '.png',
        replace: '_over.png'
        //onFinish: finish
    });

    $('#main img.swap').hover(function() {
        this.src = this.src.replace('.png', '_over.png');
    }, function() {
        this.src = this.src.replace('_over', '');
    });

    //    function finish(data) {
    //        alert(data.total);
    //    };



    //Locations show detail
    $('.locationL').click(function() {
        $(".detail").hide();
        var div = $(this).attr("id") + "Details";
        $("#" + div).fadeIn("slow");
    });






    //Login dropdown
    $("#login").toggle(
        function() {
            $(this).addClass("roundT");
            //$("#acctLogin").slideDown("medium");
            $("#acctLogin").show();
        },
        function() {
            //$("#acctLogin").slideUp("medium");
            $("#acctLogin").hide();
            $(this).removeClass("roundT");
        }
    );

    //FancyBox
    $("a.imageFB").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'titleShow': false,
        'padding': 20,
        'overlayOpacity': 0
    });

    //Add even class to tables
    $(".stripe tr:even, .stripe tr td:odd, .stripe tr th:odd").addClass("even");

    //Take margin off last upsell item
    $("#relatedProducts ul li:last").addClass("last");

});

