function findValue(li){
    if (li == null) 
        return alert("No match!");
    
    // if coming from an AJAX call, let's use the CityId as the value
    if (!!li.extra) 
        var sValue = li.extra[0];
    
    // otherwise, let's just display the value in the text box
    else 
        var sValue = li.selectValue;
    
}

function selectItem(li){
    findValue(li);
}

function formatItem(row){

    return row[0];
    
}

function limitText(limitField, limitCount, limitNum){
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
    else {
        limitCount.value = limitNum - limitField.value.length;
    }
}

function getMoc(){

    jQuery("#search_input, #search_input2").autocomplete("/include/aj/aj_find_mocs.php", {
        delay: 200,
        //minChars: 2,
        matchContains: 1,
        width: "280px",
        scroll: false,
        cacheLength: 30,
        onItemSelect: selectItem,
        onFindValue: findValue,
        formatItem: formatItem,
        autoFill: false,
        selectFirst: false
    }).result(function(event, data, formatted){
    
        var page = "representative.php?" + data[1];
        
        window.location = page;
        
    });
    
    
    
    
}


function getMocRank(){

    jQuery("#rank_input, #rank_input").autocomplete("/include/aj/aj_rank_mocs.php", {
        delay: 200,
       // minChars: 2,
        matchContains: 1,
        width: "280px",
        scroll: false,
        cacheLength: 30,
        onItemSelect: selectItem,
        onFindValue: findValue,
        formatItem: formatItem,
        autoFill: false
    }).result(function(event, data, formatted){
    
        var moc_position = data[1];
        $('.scroll_highlight').removeClass('scroll_highlight');
        $('.scroll_link').css('color', '#731012').removeClass('scroll_link').find('a').css('color', '#731012');;
		
		
        $('#' + moc_position).parent('td').parent('tr').addClass('scroll_highlight');
        $('#' + moc_position).parent('td').parent('tr').find('td').css('color', '#fff').addClass('scroll_link').find('a').css('color', '#fff');
       
        $.scrollTo('#' + moc_position, 2000);
        
        
    });
    
    
    
}


function getTopMoc(){


    jQuery("#top_rank").autocomplete("/include/aj/aj_rank_mocs.php", {
        delay: 200,
       // minChars: 2,
        matchContains: 1,
        width: "280px",
        scroll: false,
        cacheLength: 30,
        onItemSelect: selectItem,
        onFindValue: findValue,
        formatItem: formatItem,
        autoFill: false
    }).result(function(event, data, formatted){
    
        var moc_position = data[1];
        var page = "top_rankings.php#" + data[1];
        
        window.location = page;
        
        
    });
    
    
}


function searchMocs(){
    var search_term = $("#search_input").val()
    if (search_term == "SEARCH BOX" || search_term.length < 2) {
    
        // SOME ERROR HANDLING OF SOME SORT - TIMANI
        $.facebox("<h2> Please enter a search term.</h2>");
    }
    else {
        window.location = "search_results.php?search_input=" + escape(search_term);
    }
}

function modalPrompt(page_name){
    $.facebox(function(){
        $.get(page_name, function(data){
            $.facebox(data);
        });
    });
}


function showNews(news_id){
	 $.facebox(function(){
        $.get("/include/aj/aj_news.php",{news_id:news_id,action:'show'}, function(data){
            $.facebox(data);
        });
    });
}


$(function(){
    getTopMoc();
    getMoc();
    getMocRank();
    
    
    swapValues = [];
    jQuery("#top_rank").each(function(i){
        swapValues[i] = jQuery(this).val();
        jQuery(this).focus(function(){
            if (jQuery(this).val() == swapValues[i]) {
                jQuery(this).val("");
            }
        }).blur(function(){
            if (jQuery.trim(jQuery(this).val()) == "") {
                jQuery(this).val(swapValues[i]);
            }
        });
    });
    
    swapValues2 = [];
    jQuery("#rank_input").each(function(i){
        swapValues2[i] = jQuery(this).val();
        jQuery(this).focus(function(){
            if (jQuery(this).val() == swapValues2[i]) {
                jQuery(this).val("");
            }
        }).blur(function(){
            if (jQuery.trim(jQuery(this).val()) == "") {
                jQuery(this).val(swapValues2[i]);
            }
        });
    });
    
    swapValues3 = [];
    jQuery(".ac_input").each(function(i){
        swapValues3[i] = jQuery(this).val();
        jQuery(this).focus(function(){
            if (jQuery(this).val() == swapValues3[i]) {
                jQuery(this).val("");
            }
        }).blur(function(){
            if (jQuery.trim(jQuery(this).val()) == "") {
                jQuery(this).val(swapValues3[i]);
            }
        });
    });
    
    
    $('a[rel*=report]').facebox({
    
        loadingImage: 'images/facebox/loading.gif',
        closeImage: 'images/facebox/closelabel.gif'
    });
    
});

