﻿var ct, st, si, ms = 5000, hintField, links = new Array(), urls = new Array();

$(document).ready(function() {
    // init the tabbed hotlinks (if they exist).
    if ($("#outercontainer").length) {
        $("#outercontainer").hide();
        $("#container-1 > ul").tabs({ fx: [null, { opacity: 'show'}] });
        $("#outercontainer").show();
    }

    // init the menu rollover.
    $('.main-nav ul li').hover(
      function() {
          if ($(this).attr('name')) {
              $('.main-nav ul li a').removeClass('hover');
              $('a', this).addClass('hover');
              var sub = '#subnav_' + $(this).attr('name');
              $('.sub-nav').hide();
              $(sub).show();
          }
      },
      function() { }
    )

    $('.sub-nav').hover(
      function() {
          //$('#subnav_'+$('.active').attr('name')).show();
      },
      function() {
          $('.main-nav ul li a').removeClass('hover');
          $('.sub-nav').hide();
          $('#subnav_' + $('.active').attr('name')).show();
      }
    )

    // capture when focus moves.
    $('input').focus(function() {
        $('#__LASTFOCUS').val(this.id);
    });

    $("form").submit(function() {
        if ($('.scrollTop').length > 0) {
            $('.scrollTop').val($(window).scrollTop());
        }
    });

    if ($('.scrollTop').length > 0) {
        // set the scrolling position.
        $(window).scrollTop($('.scrollTop').val());
    }

    /*
    // set up the thickbox login.
    if($('a.login').length)
    {
    var href = $('a.login').attr('href').replace('.html','-ajax.html?KeepThis=true&TB_iframe=true&height=325&width=500');   
    $('a.login').attr('href', href).addClass('thickbox');
    }
    */

    $(".trhover").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    var autoClick = true;

    $(".trhover").click(function() {
        if (autoClick == true) {
            document.location = jQuery(this).find("a:last").attr('href');
        }
    });

    // cancel the autoclick when a actual link is clicked.
    $(".trhover a").click(function() { autoClick = false; });


    // init search hints.
    hintField = $('.searchfield');
    if (hintField.length) {
        var submitID = hintField.attr('submitID');
        var hc = hintField.attr('hintcount');
        var kw = "";
        if (hc && hc != "0") {
            hintField.after("<div class=\"searchWrap\"><div class=\"autocomplete\"></div></div>");

            hintField.keyup(function(e) {
                if (ct) { clearTimeout(ct); }
                if (!e) e = window.event;
                if (e.keyCode == 40) { setSelected(si + 1); }
                else if (e.keyCode == 38) { setSelected(si - 1); }
                else if ((e.keyCode < 37 || e.keyCode > 40) && e.keyCode != 13) {
                    if (kw != hintField.val()) {
                        kw = hintField.val();
                        if (st) { clearTimeout(st); }
                        st = setTimeout(function() { buildHints(hc) }, 100);
                    }
                    setSelected(-1)
                }
                else if (e.keyCode == 13) {
                    if (si != -1) { window.location = urls[si]; return false; }
                    else if (submitID && submitID != "") { __doPostBack(submitID, '') }
                }
                else if (e.keyCode > 36 && e.keyCode < 41 && links.length > 0) {
                    $('.autocomplete').show();
                    ct = setTimeout('$(\'.autocomplete\').hide();', ms);
                }
            });
        }
    }
});



function buildHints(hc)
{	
    var params = "{keyword:'"+hintField.val()+"',hintCount:"+hc+"}";
    $.ajax({
        type: "POST",
        url: '/pages/ajax/autohint.aspx/Hints', 
        data: params, 
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
            xhr.setRequestHeader("Content-length", params.length);},
        contentType: "application/json; charset=utf-8",
        dataType: "json", 
        success: function(msg, status) {            
            eval(msg.d);
            showHints();
        },
        error: function(xhr,msg,e){}
        });
             
    setSelected(-1);
}

function showHints()
{
    if(links.length==0) { $('.autocomplete').hide(); }
    else
    {
	    if(ct) { clearTimeout(ct); }
	    $('.autocomplete').show().css('zIndex', '9999');		
	    if(links.length>0 && links.length == urls.length)
	    {
	        var sR = "";
	        for (var i=0;i<links.length;i++) 
            {
                sR = sR + "<div class=\"hintNormal\" hint=\"" + i + "\" id=\"_hint" + i + "\"><a href=\"" + urls[i] + "\">" + links[i] + "</a></div>";
            }
            
            $('.autocomplete').html(sR);
            $('.hintNormal').hover(
	            function () 
	            { 
	                var sel = parseInt($(this).attr('hint'));
	                setSelected(sel);
	            },
	            function () {}	            
	        );
	    }
	    ct = setTimeout('$(\'.autocomplete\').hide();', ms);
    }
}

function setSelected(sel)
{
    if(ct) { clearTimeout(ct); }
    
    ct = setTimeout('$(\'.autocomplete\').hide();', ms);
    
    if(sel<-1) { sel = 0; }
    
    if(sel>=links.length) { sel = links.length-1; }
    
    if(sel!=si)
    {
        si = sel;
        $('.hintNormal').removeClass('hintSelected');
        $("#_hint"+si).addClass('hintSelected');
    }
}
