// change jQuery to use $j so as to not conflict with prototype
var $j = jQuery.noConflict();


function hideInsight(insightId) {
    //alert(insightId);
    $j("#iid" + insightId).hide();
    $j("#piid" + insightId).show();
}

$j(document).ready(function() {


    // hide insight texts
    //$j(".insighttext").hide();

    // hide the popover
    $j("#popover").hide();

    $j(".foop").click(function() {
        alert("HI!");
    })
    .css({cursor:"pointer"})
    ;

    $j(".loadpop").click(function() {

        //get the url
        var thisUrl = $j(this).attr('href');

        // get the topOffset
        var popTopOffset = $j(this).offset().top + 5;
        var popLeftOffset = $j(this).offset().left;

        // get the JSON data
        $j.getJSON("ajax/json-" + thisUrl,
            function(data){

              var pophtml = "<div style='float:right;cursor:pointer;'>[x]</div>";
              pophtml += data.pophtml;
              popwidth = data.width;
              // show the popover
              $j("#popover").html(pophtml)
              .css({"background": "#eeeeee","border": "2px solid #aec6e0", "margin":"10px", "padding":"10px"})
              .css({"top":popTopOffset})
              .css({"left":popLeftOffset})
              .css({"width":popwidth})
              ;



            });

        // toggle the popover
        $j("#popover")
        .toggle()
        ;

        this.blur();
        return false;
    })
    .css({cursor:"pointer"})
    ;

    // if they click on the popover itself, it will toggle
    $j("#popover").click(function() {
        $j("#popover").toggle();
    })
    ;

    // 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"})
    ;

    // 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"})
    ;

    // "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 icurl = $j(this).attr("href");

            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: 15px 10px;font-size:18px;'>Please <a href='" + icurl + "'>visit the Insight Community</a> to add your comment.</p>";

            // build the form
            $j("#popover")
            .html(pophtml)
            ;

            // show the popover
            $j("#popover")
            .show()
            .css({"background": "#ffffbb","border": "2px solid #aec6e0", "margin":"10px", "padding":"10px", "width":"500px"})
            .css({"top":popTopOffset})
            .css({"left":popLeftOffset})
            ;

            this.blur();
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // profile tabs
    $j("#t_comments").click(
        function() {
            $j("#t_comments").addClass("on");
            $j("#t_submitted").removeClass("on");
            $j("#t_written").removeClass("on");

            $j("#p_comments").show();
            $j("#p_submitted").hide();
            $j("#p_written").hide();            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    $j("#t_submitted").click(
        function() {
            $j("#t_comments").removeClass("on");
            $j("#t_submitted").addClass("on");
            $j("#t_written").removeClass("on");

            $j("#p_comments").hide();
            $j("#p_submitted").show();
            $j("#p_written").hide();

            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    $j("#t_written").click(
        function() {
            $j("#t_comments").removeClass("on");
            $j("#t_submitted").removeClass("on");
            $j("#t_written").addClass("on");

            $j("#p_comments").hide();
            $j("#p_submitted").hide();
            $j("#p_written").show();
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;

    // tiers check for size selected
     $j("#tiersform").submit(function() {
          if ($j("#tiersize").val() == "") {
            alert ("Please choose a Size before clicking on the Buy button.");
            return false;
          }
          return true;
    });

 });

