﻿function initSearch()
{
    var server = "/WebServices/AutoComplete.aspx";  
    var schema = ["result", "value"]; 
    var dataSource = new YAHOO.widget.DS_XHR(server, schema);
     	
    dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_XML; 
    dataSource.scriptQueryParam = "search";

    var autoComp = new YAHOO.widget.AutoComplete("tbxSearch","autoCompleteContainer", dataSource); 
    autoComp.typeAhead = false;
    autoComp.useShadow = true;
    autoComp.forceSelection = false;          
    autoComp.autoHighlight = true; 
    
    
    var itemSelectHandler = function(sType, aArgs) { 
         searchEvents();
	}; 
	
	autoComp.itemSelectEvent.subscribe(itemSelectHandler); 
 }
 
 
function searchEvents()
{
    var tbxSearch = document.getElementById("tbxSearch");
    if(trim(tbxSearch.value) != "")
        window.location = "/SearchTickets.aspx?searchterm=" + tbxSearch.value
}
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
function trim( value ) {
    return LTrim(RTrim(value));
}

//this function allows any link that has a rel attriute set to external to open in a new window
//this is done to allow for a link to open in a new window and to keep the page XHTML strict complient
//you can't use the target attribute on an XHTML strict complient page
function externalLinks()
{
    if (!document.getElementsByTagName) return;
    
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) 
    {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
} 
function hookKeyDown()
{
    document.onkeydown = alertkey;    
}
function alertkey(e) 
{
    if(!e) 
    {
        if( window.event ) //Internet Explorer
          e = window.event;
        else 
          return;
    }
    if( typeof( e.keyCode ) == 'number') //DOM
        e = e.keyCode;
    else if( typeof( e.which ) == 'number') //NS 4 compatible
        e = e.which;
    else if( typeof( e.charCode ) == 'number') //also NS 6+, Mozilla 0.9+
        e = e.charCode;
    else 
        return;
        
    if(e==13) 
        return searchEvents();
 }

var iCurrentDisplayIndex = 0;
var _bAutoRotate = true;
function startContentRotate(iContentCount)
{
    iCurrentDisplayIndex++;
    if(iCurrentDisplayIndex == iContentCount)
        iCurrentDisplayIndex = 0;
    
    if(_bAutoRotate)
        setTimeout ('rotateContent(' + iCurrentDisplayIndex + ', ' + iContentCount+ ',false);startContentRotate(' + iContentCount+ ')', 5000);
}

function rotateContent(iDisplayIndex, iContentCount, bStopAutoRotate)
{
    //if the global switch has been thrown and the bStopAutoRotate is not true then this is a left over request
    if(!_bAutoRotate && !bStopAutoRotate) 
        return;
        
    iCurrentDisplayIndex = iDisplayIndex;
    for(var i = 0; i < iContentCount; i++)
    {
        document.getElementById("item_" + i).style.display = 'none';
        if(i == 0 || i + 1 == iContentCount)
            document.getElementById("link_" + i).className = 'hiddenItemLast';
        else 
            document.getElementById("link_" + i).className = 'hiddenItem';
    }
    document.getElementById("item_" + iDisplayIndex).style.display = 'block';
    document.getElementById("link_" + iDisplayIndex).className = 'displayItem';
    
    if(bStopAutoRotate == true)
        _bAutoRotate = false
}


function newComment(iPostID) {
    var newCommentWindow = window.open("/blogNewComment.aspx?postid=" + iPostID, 'newComment', 'location=0,status=1,scrollbars=1,menubar=0,resizable=1,width=850,height=550');
    newCommentWindow.focus();
}

