/* __________________________ JQWHEREEY__________*/
jQuery(function($) {

    /* ------------------------------------------- Setup tabs */
    $("#tab-a").show();
    $("#tab-a > ul").tabs();

    $("#tab-b").show();
    $("#tab-b > ul").tabs();

    $("#tab-comments").show();
    $("#tab-comments > ul").tabs();

    $('#more-stories-face').html(jQuery('#more-stories').html());
    $('#related-stories-face').html(jQuery('#related-stories').html());

    /* ------------------------------------------------------- */


    /* Page pop info stuff */
    $(".pop-info").toolPop("Click to see info");
    /* Story page inline gallery */
    $().inlineGallery();

});
/* END PAGE FUNCTION */

/* _______________________ Inline Gallery ____________*/
jQuery.fn.inlineGallery = function() {
    jQuery.tes_namespace = {};
    jQuery.tes_namespace.currentIndex = 0;
    jQuery.tes_namespace.who = jQuery("#gallery-nav a:eq(0)");
    jQuery.tes_namespace.max = jQuery("#gallery-nav a").length - 1;
    // if(jQuery.tes_namespace.max == 0)jQuery("#gallery-nav").css({height: "0px", overflow: "hidden"});
    // how many in the gallery? is this not 0 based???
    // added the -1 to length to force 0 based count
    jQuery("#gallery-nav a").each(
    function(i) {
        if (jQuery.tes_namespace.max > 0) {
            this.innerHTML = " " + (i + 1) + " " + this.innerHTML;
            /* a load checker gallery-nav is hidden in the style sheet and
		should be turned back on here-- don't want the buttons clickable until they're finished */
            if (i == jQuery.tes_namespace.max) jQuery("#gallery-nav").css({
                visibility: "visible",
                height: "22px"
            });
        }
    });

    /* run the click on the numbered gallery buttons */
    jQuery("#gallery-nav a").click(
    function(e) {
        e.preventDefault();
        /* grab the index of who was clicked */
        jQuery.tes_namespace.currentIndex = jQuery("#gallery-nav a").index(this);
        jQuery.tes_namespace.who = jQuery(e.target);
        jQuery().injectPhoto("a");
    });

    /* run the click on gallery image to advance */
    jQuery("#full-image span").mouseover(
    function(e) {
        jQuery("#full-image span").css({
            cursor: "pointer"
        });
    });
    jQuery("#full-image span").click(
    function(e) {
        jQuery().injectPhoto("img");
    });

}

/*____________________________________ Process pulled image____*/
jQuery.fn.injectPhoto = function(origin) {
    if (origin == "img") {
        if (jQuery.tes_namespace.currentIndex < jQuery.tes_namespace.max) {
            jQuery.tes_namespace.currentIndex = jQuery.tes_namespace.currentIndex + 1;
            jQuery.tes_namespace.who = jQuery("#gallery-nav a:eq(" + jQuery.tes_namespace.currentIndex + ")");
        } else {
            jQuery.tes_namespace.currentIndex = 0;
            jQuery.tes_namespace.who = jQuery("#gallery-nav a:eq(0)");
        }
    }
    var who = jQuery.tes_namespace.who;
    jQuery().hiliteCurrent();
    // hilite the numbox for current image
    jQuery("#full-image span").hide();
    // hide the current photo
    /* grab info from template and inject it in gallery photo box */
    var image = "<img src=" + who.attr("href") + " height=" + who.attr("photoHeight") + " alt='photo'>"
    var byline = who.attr("byline");
    // These are here becuase IE
    var credit = who.attr("credit");
    // shows undefined if the photo
    if (!byline) byline = "";
    // comes across sans byline or
    if (!credit) credit = "";
    // credit.
    jQuery("#full-image img").replaceWith(image);
    jQuery("#full-text h6").replaceWith("<h6>" + byline + " " + credit + "</h6>");
    jQuery("#full-text p").replaceWith("<p>" + who.attr("caption") + "</p>");
    /* end injection */
    jQuery("#full-image span").fadeIn("slow");
    // fade in the next photo
}
// end injectPhoto
jQuery.fn.hiliteCurrent = function() {
    jQuery("#gallery-nav a").css(
    {
        backgroundImage: "url(http://images.chron.com/common/735/sandbox/storypage/images/btn_fadeBG2.jpg)"
    });
    jQuery("#gallery-nav a:eq(" + jQuery.tes_namespace.currentIndex + ")").css(
    {
        backgroundImage: "url(http://images.chron.com/common/735/sandbox/storypage/images/btn_fadeBG.jpg)"
    });
}

/*____________________________ Tool Tip and Pop Info */
jQuery.fn.toolPop = function(tip) {
    this.mouseover(
    function(e) {
        var mouseX = jQuery().getMouseX(e);
        var mouseY = jQuery().getMouseY(e);
        if (!this.tooltipdiv) {
            var div = document.createElement("div");
            this.tooltipdiv = div;
            jQuery("body").append(div);
            jQuery(this.tooltipdiv).addClass("tool-tip");
        }

        jQuery(this).css({
            cursor: "pointer",
            backgroundColor: "#f1f1f1"
        });
        if (this.tooltipdiv) jQuery(this.tooltipdiv).html(tip);
        jQuery(this.tooltipdiv).show().css({
            left: mouseX + "px",
            top: mouseY + 3 + "px"
        });

    }
    ).mouseout(
    function() {
        if (this.tooltipdiv) {
            jQuery(this.tooltipdiv).hide();
            jQuery(this).css({
                backgroundColor: "#fff"
            });
        }
    }
    ).mousemove(
    function(e) {
        if (this.tooltipdiv) {
            var mouseX = jQuery().getMouseX(e);
            var mouseY = jQuery().getMouseY(e);
            jQuery(this.tooltipdiv).show().css({
                left: mouseX + "px",
                top: mouseY + 3 + "px"
            });
        }
    }
    ).click(
    function(e) {
        if (this.tooltipdiv) {
            jQuery(".info-box-x").hide();
            jQuery(".info-box").hide();
            jQuery(this.tooltipdiv).hide();
            jQuery(this).css({
                backgroundColor: "#fff"
            });
            if (!this.toolpopdiv) {
                var divpop = document.createElement("div");
                var divpop_x = document.createElement("div");
                this.toolpopdiv = divpop;
                this.toolpopdiv_x = divpop_x;
                jQuery(this.toolpopdiv).addClass("info-box");
                jQuery("body").append(divpop);
                jQuery(this.toolpopdiv_x).addClass("info-box-x");
                jQuery("body").append(divpop_x);
            }
            if (this.toolpopdiv) jQuery(this.toolpopdiv).html(
            "some kind of ajax pull<br> and populate into this box<br>Mama sing sing gotta jibboo, keep on drinkin jiboo."
            );
            // place the pop up box
            var mouseX = jQuery().getMouseX(e);
            var mouseY = jQuery().getMouseY(e);
            var popdivHeight = jQuery(this.toolpopdiv).height() + 30;
            var popdivWidth = jQuery(this.toolpopdiv).width();
            jQuery(this.toolpopdiv).show().css({
                left: mouseX + "px",
                top: mouseY - popdivHeight + "px"
            });
            // place the close button
            mouseX = mouseX + (popdivWidth - 7);
            mouseY = mouseY - (popdivHeight - 3);
            jQuery(this.toolpopdiv_x).show().css({
                left: mouseX + "px",
                top: mouseY + "px"
            });
        }
        /* end if */

        jQuery(this.toolpopdiv_x).click(
        function(e) {
            jQuery(".info-box-x").hide();
            jQuery(".info-box").hide();
        }).mouseover(
        function(e) {
            jQuery(this).css({
                cursor: "pointer"
            });
        });

        jQuery("body").click(
        function(e) {
            // an attempt at making the boxes close if you click anywhere else
            });

    });

    return this;
}
/* END SHELL FUNCTION toolPop */

/* _________________________________ get mouse coordinates  */
// based on code from http://www.quirksmode.org/js/events_properties.html
jQuery.fn.getMouseX = function(e) {
    var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft: 0);
    mouseX += 10;
    return mouseX;
}
jQuery.fn.getMouseY = function(e) {
    var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop: 0);
    mouseY += 10;
    return mouseY;
}
