﻿var _iCurrentImage = 1;
var _iNewImage = 0;
var _oInterval;
var _bIsAnimating = false;
var _sPreviewBoxId = "_divPreviewBox";
var _oTimeout;

function getPreviewBox()
{
    var oElement;

    if (!$$(_sPreviewBoxId)) 
    {
        oElement = document.createElement("div");
        oElement.setAttribute("id", _sPreviewBoxId);
        oElement.setAttribute("class", "previewBox");
        document.body.appendChild(oElement);
    }
    else
    {
        oElement = $$(_sPreviewBoxId);
    }
  
    return oElement;
}

function hidePreview()
{
    _oTimeout = window.setTimeout(function() { $(getPreviewBox()).fadeOut("slow"); }, 500);
}

function showPreview()
{
    var oPreviewBox = getPreviewBox();
    var iCardId;
    
    iCardId = $(this).attr("class");
    iCardId = iCardId.substring(iCardId.indexOf(' ') + 1);
    $(oPreviewBox).get(0).innerHTML = "<img style='padding-top:5px' src='/images/t/" + iCardId + ".jpg'/>";
    $(oPreviewBox).css("left", ($(this).offset().left - 115) + "px");
    $(oPreviewBox).css("top", ($(this).offset().top - 70) + "px");
    $(oPreviewBox).fadeIn("slow");
    if(_oTimeout != null)
    {
        window.clearTimeout(_oTimeout);
        _oTimeout = null;
    }
}

function fadeTo(img, bCancel)
{
    if(_bIsAnimating)
    {
        return false;
    }
    
    if(bCancel)
    {
        window.clearInterval(_oInterval);
    }
    
    if(img == _iCurrentImage)
    {
        return false;
    }
    
    _iNewImage = img;
    
    $$("imgBanner" + _iNewImage).style.xOpacity = .99;
    $$("imgBanner" + _iNewImage).style.opacity = .99;
    $$("imgBanner" + _iNewImage).style.MozOpacity = .99;
    $$("imgBanner" + _iNewImage).style.zIndex = 1;
    $$("imgBanner" + _iNewImage).style.filter = "alpha(opacity=" + ($$("imgBanner" + _iNewImage).style.xOpacity * 100) + ")";
    show("imgBanner" + _iNewImage);
    $("#tdMastCell" + _iCurrentImage).attr("class", "");
    $("#tdMastCell" + _iNewImage).attr("class", "on");
    
    window.setTimeout(fadeIn, 50);
    _bIsAnimating = true;
    
    return false;
}

function fadeIn()
{
    var oOld = $$("imgBanner" + _iCurrentImage);
    var oNew = $$("imgBanner" + _iNewImage);
    
    if(_iCurrentImage == _iNewImage)
    {
        return;
    }
    
    oOld.xOpacity = oOld.xOpacity - .1;
    
    if(oOld.xOpacity > 0)
    {
        setOpacity(oOld);
        window.setTimeout(fadeIn, 50);
    }
    else
    {
        oOld.style.zIndex = 0;
        oOld.xOpacity = 0;
        hide("imgBanner" + _iCurrentImage);
        $("#tdMastCell" + _iCurrentImage).attr("class", "");
        $("#tdMastCell" + _iNewImage).attr("class", "on");
        _iCurrentImage = _iNewImage;
        
        oNew.style.zIndex = 2;
        oNew.xOpacity = 0.99;
        _bIsAnimating = false;
    }
}

function doRoll()
{
    if(_iCurrentImage == 4)
    {
        fadeTo(1);
    }
    else
    {
        fadeTo(_iCurrentImage+1);
    }
}

function setOpacity(obj) 
{
		if(obj.xOpacity>.99) 
		{
			  obj.xOpacity = .99;
			  return;
		}
		
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

function doSearch(sSearch)
{
    window.location = "/site/search.aspx?search=" + escape(sSearch);
}

function grayOut(bShow)
{
    var oElement;
    
    if(bShow == false)
    {
        $("#divGrayOut").hide();
    }
    else
    {
        if($("#divGrayOut").get(0) == undefined)
        {
            oElement = document.createElement("div");
            oElement.innerHTML = "<div id='divGrayOut' class='grayout'></div>";
            $("body").get(0).appendChild(oElement);
            $("#divGrayOut").show();
            $("#divGrayOut").css("height", $(document).height() + "px");
        }
        else
        {
            $("#divGrayOut").css("height", $(document).height() + "px");
            $("#divGrayOut").show();
        }
    }
}


$(document).ready(function()
{
    if($("#ctlBanner_divHoverBanner") != undefined && $("#ctlBanner_divHoverBanner").html() != null)
    {
        hide("imgBanner2");
        hide("imgBanner3");
        hide("imgBanner4");
        
        imgs = $$("ctlBanner_divHoverBanner").getElementsByTagName("img");
	      for(i=0;i<imgs.length;i++) 
	      {
	          imgs[i].xOpacity = 0;
	          imgs[i].zIndex = 0;
	      }
	      
	      imgs[0].style.zIndex = 2;
	      
	      imgs[0].xOpacity = .99;
	      
	      _oInterval = window.setInterval(doRoll, 5000);
    }
    
    if ($("#aSearchLink") != undefined)
    {
        $("#aSearchLink").click(function() { doSearch($("#txtSearchBox").val());});
        $("#txtSearchBox").keydown(
        function(event)
        {
            if (event.keyCode == 13)
            {
                doSearch($(this).val());
                return false;
            }
        });
    }
    
    $(".searchbox").each(function()
    {
        $(this).focus(function()
        {
            if (this.value=="Search...") 
            {
                this.value="";
            }
        });
        
        $(this).blur(function()
        {
            if (this.value=="") 
            {
                this.value="Search...";
            }
        });
        
        $(this).keydown(
        function(event)
        {
            if (event.keyCode == 13)
            {
                doSearch($(this).val());
                return false;
            }
        });
    });
    
    $(".hoverlink").each(function()
    {
        $(this).mouseover(showPreview);
        $(this).mouseout(hidePreview);
    });
});