// change jQuery to use $j so as to not conflict with prototype
var $j = jQuery.noConflict();

$j(document).ready(function() {

    // focus on the first form element
    $j("input[type='text']:first", document.forms[0]).focus();

    //$j(".insighttools").hide();
    $j("#lc_form").hide();
    $j("#fdebug").hide();

    // clicking on "Show Insight Tools"
    $j(".itools")
    .click(
        function() {
            var insightid = $j(this).attr("insightid");
            var tooldiv = "#insighttools" + insightid;
            var insightdiv = "#insight" + $j(this).attr("insightid");

            $j(tooldiv).toggle();
            $j(insightdiv).show();

            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // rate an insight. (old way.. the new rating stuff is in rating-jquery.js)
    $j(".rs")
    .click(
        function() {
            var insightid = $j(this).attr("insightid");
            var rating = $j(this).attr("rating");

            // no star logic
            if (!isNumeric(rating)) {
                rating = 0;
            }

            var url = "houston_xml.php?op=rateinsight&insightid=" + insightid + "&rating=" + rating;

            // make the rating html
            var k=0;
            var ratinghtml = "";
            for (k=0;k<rating;k++) {
                ratinghtml += "<img src='images/star.gif' />";
            }
            for (k;k<5;k++) {
                ratinghtml += "<img src='images/star_off.gif' />";
            }

            $j("#insightrating" + insightid).html(ratinghtml);

            //alert ("Rated Insight " + insightid + ": " + rating + " stars.");
            $j.get(url);

            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // the "Add your comment on this insight" link
    $j(".lc").click(
        function() {

            var insightid = $j(this).attr("insightid");  // get the insight id to apply this comment to.

            var url = window.location.pathname + window.location.search; // can't just use window.location cuz then we get the hash
            url += "#lc" + insightid; // get this url to pass to comment_submit

            // get the topOffset
            var popTopOffset = $j(this).offset().top + 15;
            var popLeftOffset = $j(this).offset().left - 350;

            var pophtml = "<form name='commentform' action='comment_submit.php' method='post'><input type='hidden' name='_done' value='" + url + "' /><input type='hidden' name='comment_insightid' id='comment_insightid' value='" + insightid + "' />    <label for='comment'>Your Comment</label>    <br />    <p>    <textarea name='postercomment' rows='10' cols='60'></textarea>    <br /><br /><input type='submit' name='mode' value='Submit Comment' /></p></form>";

            // build the form
            $j("#lc_form_inside")
            .html(pophtml)
            ;

            // show the popover
            $j("#lc_form")
            .show()
            .css({"background": "#eeeeee","border": "2px solid #aec6e0", "margin":"10px", "padding":"10px", "width":"500px"})
            .css({"top":popTopOffset})
            .css({"left":popLeftOffset})
            ;

            this.blur();
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // "add your comment" if you're not logged in
    $j(".lc2").click(
        function() {

            var insightid = $j(this).attr("insightid");  // get the insight id to apply this comment to.

            var url = window.location.pathname + window.location.search; // can't just use window.location cuz then we get the hash
            url += "#lc" + insightid; // get this url to pass to comment_submit

            // get the topOffset
            var popTopOffset = $j(this).offset().top + 15;
            var popLeftOffset = $j(this).offset().left - 350;

            var pophtml = "<p style='padding: 20px 10px;'>Please <a href='login.php'>Sign In</a> to add your comment. <br/><br/> Or, if you are not yet a member of the Insight Community, <a href='newaccount.php'>Join Now</a>!</p>";

            // build the form
            $j("#lc_form_inside")
            .html(pophtml)
            ;

            // show the popover
            $j("#lc_form")
            .show()
            .css({"background": "#eeeeee","border": "2px solid #aec6e0", "margin":"10px", "padding":"10px", "width":"500px"})
            .css({"top":popTopOffset})
            .css({"left":popLeftOffset})
            ;

            this.blur();
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // insight comment form close click.
    $j("#lc_close").click(
        function() {
            $j("#lc_form").hide();
            this.blur();
            return false;
        }
    );

    // collapse/expand ALL insights
    $j("#c_insights").toggle(
        function() {
            $j(".insight").slideUp();
            $j(".h_insight").children().attr("src","images/arrow-orange-rt.gif");
            return false;
        }
        ,
        function() {
            $j(".insight").slideDown();
            $j(".h_insight").children().attr("src","images/arrow-orange-dn.gif");
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // collapse/expand ALL insight tools
    $j("#c_insighttools").click(
        function() {
            $j(".insighttools").slideToggle();
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // toggle insight when you click on the header
    $j(".h_insight").click(
        function() {
            var insightid = "#insight" + $j(this).attr("insightid");
            $j(insightid).slideToggle();

            if ($j(this).children().attr("src") == "images/arrow-orange-dn.gif") {
                $j(this).children().attr("src","images/arrow-orange-rt.gif");
            } else {
                $j(this).children().attr("src","images/arrow-orange-dn.gif");
            }
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // edit controls for insight status
    $j(".s_insight").click(
        function () {

            var op = $j(this).attr("value");
            var insightid = $j(this).attr("insightid");
            var url = "";

            //alert (op + insightid);
            switch (op) {
                case "delete":
                    if (confirm ("Are you sure you want to delete Insight " + insightid + "?")) {
                        url = ("houston_xml.php?op=deleteinsight&insightid=" + insightid);
                    }
                    break;
                case "reject":
                    url = "houston_xml.php?op=insightupdatestatus&insightid=" + insightid + "&action=" + op;
                    $j(this).attr("value", "unreject");
                break;
                case "unreject":
                    url = "houston_xml.php?op=insightupdatestatus&insightid=" + insightid + "&action=" + op;
                    $j(this).attr("value", "reject");
                break;
            }

            if (url != '') {
                $j.get(url);
            }

            return false;
        }
    )
    ;

    // click on the insights tab
    $j(".t_insights").click(
        function () {
            $j("#insights").show();
            $j("#insightformdiv").hide();
            $j("#drafts").hide();
            $j("#claim").hide();

            $j(".t_insights").addClass("on");
            $j(".t_addinsight").removeClass("on");
            $j(".t_drafts").removeClass("on");
            $j(".t_claim").removeClass("on");

            $j("#claimfocushack").focus();
            $j("#claimfocushack").hide();

            return false;
        }
    )
    ;

    // click on the create insight tab
    $j(".t_addinsight").click(
        function () {
            $j("#insights").hide();
            $j("#insightformdiv").show();
            $j("#drafts").hide();
            $j("#claim").hide();

            $j(".t_insights").removeClass("on");
            $j(".t_addinsight").addClass("on");
            $j(".t_drafts").removeClass("on");
            $j(".t_claim").removeClass("on");

            $j("#focushack").show();
            $j("#focushack").focus();
            $j("#focushack").hide();

            $j("#insighttitle").focus();

            return false;
        }
    )
    ;

    // click on the insights tab
    $j(".t_drafts").click(
        function () {
            $j("#insights").hide();
            $j("#insightformdiv").hide();
            $j("#drafts").show();
            $j("#claim").hide();

            $j(".t_insights").removeClass("on");
            $j(".t_addinsight").removeClass("on");
            $j(".t_drafts").addClass("on");
            $j(".t_claim").removeClass("on");

            return false;
        }
    )
    ;

    // click on the claims tab
    $j(".t_claim").click(
        function () {
            $j("#insights").hide();
            $j("#insightformdiv").hide();
            $j("#drafts").hide();
            $j("#claim").show();

            $j(".t_insights").removeClass("on");
            $j(".t_addinsight").removeClass("on");
            $j(".t_drafts").removeClass("on");
            $j(".t_claim").addClass("on");

            $j("#focushack").show();
            $j("#focushack").focus();
            $j("#focushack").hide();

            return false;
        }
    )
    ;

    // new insight button, just confirms.
    $j("#c_newinsight").click(
        function () {
            clearForm = confirm ("Are you sure you want to create a new insight?  Any unsaved changes will be lost.");
            if (!clearForm) {
                return false;
            }
        }
    )
    ;

    // delete insight button
    $j(".d_insight").click(
        function () {
            var insightid = $j(this).attr("insightid");
            var url = "houston_xml.php?op=deleteinsight&insightid=" + insightid;

            if (confirm ("Are you sure you want to delete this insight?")) {
                $j.get(url);
                $j("#il_" + insightid).fadeOut();
                var draftcount = $j("#draftcount").text(); // decrement the draft count by 1
                $j("#draftcount").text(draftcount-1);
            }
        }
    )
    ;

    // undelete insight button
    $j(".u_insight").click(
        function () {
            var insightid = $j(this).attr("insightid");
            var url = "houston_xml.php?op=undeleteinsight&insightid=" + insightid;

            if (confirm ("Are you sure you want to undelete this insight?")) {
                $j.get(url);
                $j("#il_" + insightid).fadeOut();
            }
        }
    )
    ;

    // toggle the feedback debug
    $j("#h_fdebug").click(
        function () {
            $j("#fdebug").slideToggle();
        }
    )
    .css({cursor:"pointer"})
    ;

    // limit the minibio to 250 characters
    $j('#minibio').keyup(
        function() {
            var text = $j('#minibio').val();
            var textlength = text.length;

            if(textlength > 250) {
                $j('#minibio').val(text.substr(0, 250));
                $j('#minibio').scrollTop(48);
            }
        }
    )
    ;

 });

