﻿
var hoverstockdiv;
var hoverstockelement;
var hoverstocktimeout;


window.onload = function() {
    try {
        $(".tablesorterlite").tablesorter();
    }
    catch (Error) { }


    $(".hoverstock").hover(
      function() {
          var qheight = 370;
          var url = $(this).attr("href");

          if (url.substr(0, 12) == "/companies/0") return;

          hoverstockelement = $(this);

          var left = ($(this).offset().left + $(this).width() + 5);
          if ((left + 505) > $(window).width()) left = $(this).offset().left - 530;


          var scrolly = getScrollXY()[1];

          var top = $(this).offset().top - 35;
          if ((top + qheight) > $(window).height() + scrolly)
              top = $(window).height() + scrolly - qheight - 30; //$(this).offset().top-300;
          if (top - scrolly < 0)
              top = scrolly + 10;

          if (left < 0)
              left = left * -1;

          hoverstockdiv = $("<div style='width: 525px; height: " + qheight + "px; background-color: white; z-index: 65000; position: absolute; left: " + left + "px; top: " + top + "px;'></div>");

          hoverstockdiv.html("<iframe frameborder=0 scrolling=no width='525px' height='" + (qheight + 25) + "px' src='/companies/hover.aspx?q=" + url + "'></iframe>");

          hoverstocktimeout = window.setTimeout(hoverstock_go, 800);

      },
      function() {
          try {

              window.clearTimeout(hoverstocktimeout);
              hoverstockelement = null;
              hoverstockdiv.remove();
          }
          catch (err) { }
      }
    );

}

function hoverstock_go() {
    try {
        if (hoverstockelement == null) return;
        hoverstockdiv.appendTo("body");
    }
    catch (err) { }
}





function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}



function ScrollToElement(theElement) {
    // http://radio.javaranch.com/pascarello/2005/01/09/1105293729000.html
    var selectedPosX = 0;
    var selectedPosY = 0;

    while (theElement != null) {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    window.scrollTo(selectedPosX, selectedPosY);

}


//function CallGeoSearch(e)
//{
//    alert('start geo');
//    var code = (e.keyCode ? e.keyCode : e.which);
//    if(code == 13) { //Enter keycode
//        //Do something
//        alert('Enter in geo');
//    }
//}


////This code will add the trim functionality into the javascript.
//if(typeof String.prototype.trim !== 'function') {
//  String.prototype.trim = function() {
//    return this.replace(/^\s+|\s+$/g, ''); 
//  }
//}


function SubmitGeoSearch() {
    var key = $.trim($(".search_input").val());
    alert(key);
    if (key != '') {
        if (window.location.hostname == 'localhost') {
            window.location.href = "http://" + window.location.host + "/GeoInvesting/Search.aspx?search=" + key + "&x=0&y=0";
        }
        else {
            window.location.href = "http://" + window.location.host + "/Search.aspx?search=" + key + "&x=0&y=0";
        }

        return false;
    }
}

function KeyDownHandler(e, btn) {
    // process only the Enter key
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 13) {
        document.getElementById(btn).click();
    }
}

function followUnfollow(myId, userId, obj) {


    $('#imgFollowLoading').css({ "display": "block" });
    // Call method through ajax web service
    AjaxWebService.LoadControls.followUnfollow(myId, userId, function() {
        //hideModal('modalPage');
        var labelText = $('#ctl00_ltFollowedBy').text();
        //alert(labelText);
        var lblfollowby = parseInt(labelText);
        //alert(lblfollowby);
        if ($('#' + obj).html() == ' Follow ') {
            $('#' + obj).html(' UnFollow ');
            lblfollowby = lblfollowby + 1;

            $('#ctl00_ltFollowedBy').html(lblfollowby);


        }

        else if (lblfollowby > 0) {
            $('#' + obj).html(' Follow ');
            //$('#ctl00_ltFollowedBy').html("0");
            lblfollowby = lblfollowby - 1;
            var NewVal = lblfollowby;

            $('#ctl00_ltFollowedBy').html("" + NewVal + "");
            //alert($('#ctl00_ltFollowedBy'));

        }

        $('#imgFollowLoading').css({ "display": "none" });
    }



    , OnfollowUnfollowFailed);


}


function OnfollowUnfollowFailed(result) {
    $('#imgFollowLoading').css({ "display": "none" });
    alert("Error in processing.");
}

function SetUnsetFavBoardNonAlert(userId, boardId, IsMember, Boardpath) {

    // Call method through ajax web service
    //alert(Boardpath);
    if (userId == 0) {
        location.href = "../signin.aspx?ReturnUrl=" + Boardpath;
    }
    else if (IsMember == "True") {
        var txt = $("#aSetAsFav").text();
        $("#aSetAsFav").text('Processing....');

        AjaxWebService.LoadControls.SetUnsetFavBoardNonAlert(userId, boardId, function() {

            if (txt == 'Set As Favorite') {
                $("#aSetAsFav").text('Remove As Favorite');
            }
            else {
                $("#aSetAsFav").text('Set As Favorite')
            }
        }
        );
    }
    else {
        //$.prompt("Only Member Users are allowed to Subscribe the Board");
        location.href = "../payment/default.aspx";
    }
}

function stripHTMLandNBSP(str) {
    return $.trim(str.replace(/<\/?[^>]+>/gi, '').replace(/&[^;]+;/g, ''));
}



function clearSearch(vid) {
    var obj = "#" + vid;
    var tool = $(obj).attr('title');
    var txt = $(obj).val();
    if (tool != txt)
        $(obj).val('');
}
