﻿var _iCurrentNav = 0;
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) + ")";
    $("#imgBanner" + _iNewImage).show();
    //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;
        $("#imgBanner" + _iCurrentImage).hide();
        $("#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);
    });
    
    $("a.aCategory").each(function()
    {
      var iTemp = $(this).attr("id").replace("aCategory", "");
      if ($("#ulCategory" + iTemp).length > 0)
      {
        $(this).mouseover(function()
        {
          var iTemp = $(this).attr("id").replace("aCategory", "");
          
          if (_iCurrentNav != 0 && _iCurrentNav != iTemp)
          {
            $("#ulCategory" + _iCurrentNav).slideUp();
          }
          
          $("#ulCategory" + iTemp).slideDown();
          
          _iCurrentNav = iTemp;
        });
      }
    });
});

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}
