// For IE hover flicker bug
try {
    document.execCommand('BackgroundImageCache', false, true);
} catch (e) { }

// Hello jQuery!
$j(document).ready(function() {
    // First, let's tell tag the page when javascript is on.
    $j("body").addClass("jsOn");
    inputClear("#search .input");
    inputClear("#shareResults .input");
    toggleState(".subject h3");

    $j(".searchBox").keydown(function(event) {
        if (event.keyCode == "13") {
            $j(".btnSearchSubmit").click();
            return false;
        }
    });
    $j(".searchBoxMain").keydown(function(event) {
        if (event.keyCode == "13") {
            $j(".searchBtnMain").click();
            return false;
        }
    });

    $j('#iFrameGoogle').load(function() {
        setTimeout("resizeIframe();", 500);
    });

    var alertHeight = $j("body#pageHome #alertWrapper").height();
    $j("body#pageHome.bodyAlert").css("background-position", "0 " + alertHeight + "px");


    if (!$j(".show_sportsList input:checked").length) {
        $j(".sportsList").hide();
    }
    $j(".show_sportsList input").click(function() {
        $j(".sportsList").show();
    })
    $j(".hide_sportsList input").click(function() {
        $j(".sportsList").hide();
    })

    //	Add classes to certain LIs in navSecondary
    $j("#navigation #navSecondary li ul").each(function() {
        $j(this).children("li:first").addClass("first");
        $j(this).children("li:last").addClass("last");
    })

    //	Wrap all submit buttons in a div (they must have class="submit")
    $j("input.submit").wrap("<div></div>");

    //	Zebra tables
    $j("table").each(function() {
        $j("tr:nth-child(odd)").addClass("odd");
    });

    function inputClear(target) {
        var target = target || "input";
        $j(target).each(function() {
            if ($j(this).attr("type") == "text") {
                var value = $j(this).val();

                $j(this).focus(function() {
                    if ($j(this).val() == value) {
                        $j(this).val("");
                    }
                });
                $j(this).blur(function() {
                    if ($j(this).val() == "") {
                        $j(this).val(value);
                    }
                });
            }
        });
    }

    //	Campus Tabs Layer Switcher
    var tabCounter = 1;
    $j("#campusTabs a").each(function() {
        var tabNum = "layer" + tabCounter;
        $j(this).click(function() {
            $j(".campusLayer").hide();
            $j("#" + tabNum).show();
            $j("#campusTabs li").removeClass("active");
            $j(this).parent().addClass("active");
            return false;
        })
        tabCounter++;
    });

    //	Homepage mouseover layer control
    $j("#navHome li").hover(
		function() {
		    $j(this).addClass("over");
		    var sub = $j(this).find('ul').length;
		    if (sub > 0) {
		        $j(this).find('ul').show().css('left', $j(this).parent().width());
		    }
		},
		function() {
		    $j(this).removeClass("over");
		    $j(this).find('ul').hide();
		}
	);

    //	Experts Guide subject toggle 
    function toggleState(target, className) {
        $j(target).toggle(
		function() {
		    $j(this).parent().toggleClass('open')
		},
	    function() {
	        $j(this).parent().toggleClass('open')
	    }
    );
    }


    //	Gallery Page
    //	Get the image & caption from the active li, add them to
    //	the main display div
    var initialImg = $j("#thumbs li.active a").attr("href");
    var initialCap = $j("#thumbs li.active .caption").html();
    $j("#imgContainer").html('<img src="' + initialImg + '" alt="" />');
    $j("#captionContainer").html(initialCap);

    //	When you click on a thumb, remove content from the main
    //	display div and replace them with new content
    $j("#thumbs a").click(function() {
        var filePath = $j(this).attr("href");
        var mediaContent = '<img src="' + filePath + '" alt="" />';
        var thisCaption = $j(this).next(".caption").html();
        $j("#thumbs li").removeClass("active");
        $j(this).parent().addClass("active");
        $j("#imgContainer, #captionContainer").empty();
        $j("#imgContainer").html(mediaContent);
        $j("#captionContainer").html(thisCaption);
        return false;
    });

    $j("#nextalink").click(function() {
        var nextSize = $j("#thumbs li.active").next("li").size();
        if (nextSize > 0) {
            //	Grab the content for the thumb after the active thumb and
            //	put it's content into the main display div
            var nextImg = $j("#thumbs li.active").next("li").children("a").attr("href");
            var mediaContent = '<img src="' + nextImg + '" alt="" />';
            $j("#thumbs li.active").removeClass("active").next().addClass("active");
            $j("#imgContainer, #captionContainer").empty();
            $j("#imgContainer").html(mediaContent);
            var thisCaption = $j("#thumbs li.active").find(".caption").html();
            $j("#captionContainer").html(thisCaption);
        } else {
            //	If the last thumb is active, do all the above with the first thumb
            var firstImg = $j("#thumbs li:first").children("a").attr("href");
            var mediaContent = '<img src="' + firstImg + '" alt="" />';
            var thisCaption = $j("#thumbs li:first").find(".caption").html();
            $j("#thumbs li.active").removeClass("active");
            $j("#thumbs li:first").addClass("active");
            $j("#imgContainer, #captionContainer").empty();
            $j("#imgContainer").html(mediaContent);
            $j("#captionContainer").html(thisCaption);
        }
    });

    $j("#prevalink").click(function() {
        var prevSize = $j("#thumbs li.active").prev("li").size();
        if (prevSize > 0) {
            //	Grab the content for the thumb before the active thumb and
            //	put it's content into the main display div
            var prevImg = $j("#thumbs li.active").prev("li").children("a").attr("href");
            var mediaContent = '<img src="' + prevImg + '" alt="" />';
            $j("#thumbs li.active").removeClass("active").prev().addClass("active");
            $j("#imgContainer, #captionContainer").empty();
            $j("#imgContainer").html(mediaContent);
            var thisCaption = $j("#thumbs li.active").find(".caption").html();
            $j("#captionContainer").html(thisCaption);
        } else {
            //	If the first thumb is active, do all the above with the last thumb
            var lastImg = $j("#thumbs li:last").children("a").attr("href");
            var mediaContent = '<img src="' + lastImg + '" alt="" />';
            var thisCaption = $j("#thumbs li:last").find(".caption").html();
            $j("#thumbs li.active").removeClass("active");
            $j("#thumbs li:last").addClass("active");
            $j("#imgContainer, #captionContainer").empty();
            $j("#imgContainer").html(mediaContent);
            $j("#captionContainer").html(thisCaption);
        }
    });


    //	Courses Layer
    //	Open the Courses layer & populate with iframe
    $j("#courseRequirements a.layerPop").click(function() {
        var iframeURL = $j(this).attr("href");
        $j("#overlay").show();
        $j("#courseBox").append('<iframe src="' + iframeURL + '" width="100%" height="100%" scrolling="no" frameborder="0"><p>Your browser does not support iframes.</p></iframe>');
        return false;
    })

    //	Generic Layer
    $j("a.layerPopup").click(function() {
        var iframeURL = $j(this).attr("href");
        if ($j("#overlay").length < 1) {
            $j("body").append('<div id="overlay"><div id="overlayBG"></div><div id="layerBox">' +
                '</div><a class="close" href="#">close</a></div>');
            $j("#overlay, #overlay .close").click(function() {
                closeOverlay();
            });
        }
        $j("#overlay").show();
        // TODO: add HTML
        $j("#layerBox").html('<div class="overlayTextWrapper">' +
            '<p>A portion of the revenue generated by Barnes & Noble from bookstore sales is shared with Long Island University.</p>' +
            '<p>Students are not obligated to purchase their textbooks or supplies from Barnes & Noble but are free to purchase their books and supplies from the vendor of their choice.</p>' +
            '<p>By proceeding, I acknowledge that I have read and understand the statement of this disclosure.</p>' +
            '<p><a href="' + $j(this).attr("href") + '" target="_blank">Click here to proceed to the Barnes & Noble Web site</a></p>' +
            '<p><a href="#" onclick="closeOverlay();">Click here to return to previous page</a></div>');
        return false;
    })

    //	Close the layer and remove the iframe content
    $j("#overlay, #overlay .close").click(function() {
        closeOverlay();
    });
    function closeOverlay() {
        $j("#overlay").hide();
        $j("#courseBox").html('');
        $j("#layerBox").html('');
        return false;
    }

    $j("#feature").homePageFeature();









});  // Bye-bye jQuery!

(function($) {

    //Home page feature
    $.fn.homePageFeature = function(options) {
        var opts = $.extend({
            closeButton: '#close', // selector that will trigger close state
            activeClassName: "active", // class to add to object when active/visible
            nav: "#featureNav", //selector of the nav ul
            animationSpeed: 750
        }, options);

        var animationSpeed = opts.animationSpeed;

        var $panels;
        var $activePanel;


        return this.each(function() {
            var obj = $(this);

            var activeClassName = opts.activeClassName;
            var $nav = obj.find(opts.nav);
            var $rotatingContent = obj.find(".rotatingContent").children();

            rotateContent($rotatingContent);

            $panels = obj.find(".panel");
            $activePanel = $panels.eq(0);


            $panels.hide();
            $activePanel.show();

            $nav.parent().append("<a href='#' id='close'>close x</a>");
            var $closeButton = obj.find(opts.closeButton);
            $closeButton.hide();


            $nav.children("li").each(function() {
                var panelNumber = $(this).prevAll().length + 1;

                var $panel = $panels.eq(panelNumber);

                var vidContainer = $panel.find(".video");
                var vidId = $.trim(vidContainer.text());

                $(this).click(function() {
                    changePanel($panel, vidId);

                    $(this).addClass(activeClassName);
                    $(this).siblings().removeClass(activeClassName);

                    $closeButton.show();

                    return false;
                });
            });

            $closeButton.click(function() {
                $closeButton.hide();
                $nav.children("li").removeClass(activeClassName);
                changePanel($panels.eq(0));

                return false;
            });
        });

        function changePanel($newPanel, vidId) {

            if ($newPanel.get(0) !== $activePanel.get(0)) {
                $videoContainer = $newPanel.find(".video");
                $oldVideoContainer = $activePanel.find(".video");

                $videoContainer.empty();



                videoObject = " \
	<object id='myExperience744588013001' class='BrightcoveExperience'> \
 	  <param name='bgcolor' value='#FFFFFF' /> \
	  <param name='width' value='320' /> \
	  <param name='height' value='240' /> \
	  <param name='playerID' value='740643207001' /> \
	  <param name='playerKey' value='AQ~~,AAAABdF1lwk~,vIGs4I8aVlAdAfLJU5xYlYNGBMGQxVWb' /> \
	  <param name='isVid' value='true' /> \
	  <param name='isUI' value='true' /> \
	  <param name='dynamicStreaming' value='true' /> \
	  <param name='wmode' value='transparent' /> \
	  <param name='@videoPlayer' value='" + vidId + "' /> \
	</object>";




                $newPanel.fadeIn(animationSpeed);
                $activePanel.fadeOut(animationSpeed);

                $activePanel = $newPanel;
                $oldVideoContainer.empty();
                $videoContainer.hide().animate({ opacity: 0.1 }, 1).show().animate({ opacity: 0.1 }, 1500).animate({ opacity: 1.0 }, 500);
                $videoContainer.append(videoObject);

                brightcove.createExperiences();
            }
        }

        function rotateContent($images) {

            var totalImages = $images.length - 1;

            if (totalImages > 0) {
                var currentImage = 0;

                $images.hide();
                $images.eq(currentImage).show();

                window.setInterval(function() {
                    var previousImage = currentImage;

                    if (currentImage == totalImages) {
                        currentImage = 0;
                    }
                    else {
                        currentImage++;
                    }

                    $images.eq(currentImage).fadeIn(2000);
                    $images.eq(previousImage).fadeOut(2000);
                }, 6000);
            }

        }
    };
})(jQuery);

function clickButton(element) {
    if (element.click) {
        element.click();
    } else if ("fireEvent" in element) {
        element.fireEvent("onclick");
    } else {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        element.dispatchEvent(evt);
    }
}

