﻿var lsrc, csrc;
$("#srch").bind("keypress", function (e)
{ 
    if ((e.keyCode||e.which) == 13)
    { 
        return false;
    }
}
); 
$("#srch").bind("keyup", function (e)
{ 
    if ((e.keyCode||e.which) == 13)
    { 
        return false;
    }
    else
    { 
        var lstext = $("#srch").val(); 
        if(lstext.length >= 2 )
        { 
            $("#spop").html("<div style='padding:15px;' ><img src='/images/ajax_loading_small.gif' alt='loading...' /> Searching</div>"); 
            $("#spop").slideDown('fast');
            csrc = lstext; 
            setTimeout("sendsearch()",1500); 
        }
        else
        { 
            $("#spop").slideUp('slow');
        }
    }
}); 

$("#srch").bind("focus", function(e)
{ 
    if($(this).val() == 'Search' )
        $(this).val('');
}).bind("blur", function(e)
{ 
    if($(this).val() == '' ) $(this).val('Search');
}); 

function sendsearch()
{ 
    if( csrc != lsrc )
    { 
        lsrc = csrc; 
        
        jax_xsend(6, $("#srchtown").val() + "|" + csrc, function (r)
        {
            $("#spop").hide();
            var lstext = $("#srch").val(); 
            if(lstext.length >= 2 )
            { 
               $("#spop").html(r);
               $("#spop").slideDown('fast');
            }
            
        }
        );

    }
}

function setacttab(jqthis, acts)
{
    $(".act-nt").removeClass('active');
    jqthis.addClass('active');
    if( acts != -1 )
    {
        $(".comment").hide();
        if( acts == 2 )
        {
            $(".comment").filter(".act5").fadeIn('fast');
        }
        else if( acts == 1 )
        {
            $(".comment").filter(".act2, .act4, .act6, .act7").fadeIn('fast');
        }
        else if( acts == 0 )
        {   
            $(".comment").filter(".act0, .act3, .act6, .act7").fadeIn('fast');
        }
        else if (acts == 3 )
        {
            $(".comment").filter(".act1, .act2, .act3, .act7").fadeIn('fast');
        }
        
    }
    else
    {
       $(".comment").fadeIn('fast');
    }
    
    return false;
}

//get ajax object
function jax_get() 
{
    var httpRequest;
    if (window.XMLHttpRequest) 
    { 
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) 
        {
            httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) 
    { 
        try 
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!httpRequest) 
    {
        alert('AJAX init failed saving throw');
        return null;
    }
    return httpRequest;
}

function wspublicsend( httpRequest, xmlmessage )
{
    httpRequest.open('POST', "/publicws.asmx", true);
    var message = wsheader() + xmlmessage + wsfooter();
    httpRequest.setRequestHeader("Content-Type", "text/xml");
    httpRequest.send(message);
} 

//xsend - compressed webservice send method
function jax_xsend(id, data, callback)
{
    var ob = jax_get();
    ob.onreadystatechange = function() 
    { 
       jax_pcb( ob, callback );
    } 
    
    ob.open('POST', "/x.asmx", true);
    ob.setRequestHeader("Content-Type", "text/xml"); 
    ob.send(jax_h() + "<op xmlns=\"http://townrenowned.com/x\"><id>" + id + "</id><data>" + data + "</data></op>" + jax_f());
}

function jax_pcb( ob, callback )
{
    if( ob.readyState == 4 && ob.status == 200)
    {
        var xmldoc = ob.responseXML; 
        try{xmldoc.normalize();}catch(e){} 
        var root_node = xmldoc.getElementsByTagName('opResult').item(0).firstChild.data; 
        
        callback(root_node);
    }
}

function jax_h()
{   
    return '<?xml version="1.0" encoding="utf-16"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>';
}
function jax_f()
{
    return '</soap:Body></soap:Envelope>';
}


function ddpick(id, upid )
{
    
    if( $("#" + id).css('display') == "none" )
    {
        $(upid).slideUp();
        $("#" + id).slideDown('fast');
    }
    else
    {
        $("#" + id).slideUp('fast');
    }
    
    return false;
}

function imgpreload(clsid)
{
    $("." + clsid).each(function (i) 
    {
        var imgdiv = $(this);
        var img = new Image();
        $(img).load(function() 
        {     
            $(this).hide();          
            imgdiv.removeClass('imgloading').append(this);
            $(this).fadeIn();    
        }
        ).attr('src',imgdiv.attr('title'));
        imgdiv.attr('title','');
    }
    );
}

$(document).ready(function() 
{
    imgpreload('imgload');
}); 
