$().ready(function() {

    function findValueCallback(event, data, formatted) {
        $("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
    }

    function formatItems(row) {
        return row[0] + " (<strong>id: " + row[1] + "</strong>)";
    }

    function formatItem(row) {  	
        return row[0] + " (id: " + row[1] + ")";  
    }

    function formatResult(row) {
        return row[0].replace(/(<.+?>)/gi, '');
    }

});

/*top search for sites, categories, tags etc*/
function bind_multi_option_search(id){
    //datasource is a url
    $("#" + id).autocomplete(base_url + "autocomplete", {
        scrollHeight:260,
            selectFirst: false,
            cacheLength:0,

            formatItem: function(item) {
                //alert(item[0]);
                var tempTable="";
                tempTable+="<table cellspacing='0' cellpadding='0' width='100%'>";
                tempTable+="<tr>";

                tempTable+="<td width='1%' align='left'>&nbsp;</td>";
                tempTable+="<td width='9%' align='left'>";
                if (item[2] != "") {
                    tempTable+= item[2];
                } else {
                    tempTable+="&nbsp;";
                }
                tempTable+="</td>";
                tempTable+="<td width='60%' align='left'>";
                tempTable+=item[0];
                tempTable+="</td>";
                tempTable+="<td width='30%' align='left'>";
                tempTable+=item[3];
                tempTable+="</td>";
                tempTable+="</tr>";
                tempTable+="</table>";
                return tempTable;
            }
    }).result(function(event, item) {
        location.href = item[1];
    });


    $("#" + id).result(function(event, data, formatted) {
        if (data) {
            $(this).parent().next().find("input").val(data[1]);
        }
    });
} 


/*common variables for adding site to list*/
var default_text="";
var loading_image="";

/*common bind function for sites search starts */
function bind_sites_search(id,postfix){
    //datasource is a url
    $("#" + id).autocomplete(base_url + "autocomplete/get_sites", {
            scrollHeight:260,
            selectFirst: false,
            cacheLength:0,
            formatItem: function(item) {
                var tempTable="";
                tempTable+="<table cellspacing='0' cellpadding='0' width='100%'>";
                tempTable+="<tr>";

                tempTable+="<td width='1%' align='left'>&nbsp;</td>";
                tempTable+="<td width='10%' align='left'>";
                if (item[2] != "") {
                    tempTable+= item[2];
                } else {
                    tempTable+="&nbsp;";
                }
                tempTable+="</td>";
                tempTable+="<td width='89%' align='left'>";
                tempTable+=item[0];
                tempTable+="</td>";
                tempTable+="</tr>";
                tempTable+="</table>";
                return tempTable;
            }
    }).result(function(event, item) {
        //location.href = item[1];
        if(item[0] == "We don't have that site yet; do you think we should add it?"){
            location.href = item[1];
            return;
        }
        handle_submit_common(postfix, item[3]);
    });


    $("#" +id ).result(function(event, data, formatted) {
        if (data) {
            $(this).parent().next().find("input").val(data[1]);
        }
    });


}
/*common bind function for sites search ends */

/*common site add from suggestion list starts */
/*
*postfix is used with followings
*div containing list of site logos
*form
*searchbox
*/
function handle_submit_common(postfix){
    var frm,search_term;

    frm=document.getElementById("frm_" + postfix);
    search_term=frm["txtSiteSearch_" + postfix].value;;

    if(search_term =="" || search_term ==default_text){
        alert("Please enter site to add");
        frm["txtSiteSearch_" + postfix].focus();
        return false;
    }

    $("#message_" + postfix).removeClass("flash");
    $("#message_" + postfix).show("slow").html(loading_image);		
    
    handle_submit_add(postfix);
    return false;
    
}

function process(data,postfix){
    var frm;
    frm=document.getElementById("frm_" + postfix);
    
    if(data=='Site added to compare list'){
        handle_refresh(postfix)
        return;        
    }
    fadeInSpeed = 1000;
	$("#txtSiteSearch_" + postfix).val(default_text);
    $("#txtSiteSearch_" + postfix).css("color","silver");
    
    $("#txtSiteSearch_" + postfix).blur();
    
    $("#message_" + postfix).addClass("flash");
    $("#message_" + postfix).hide().html(data).fadeIn(fadeInSpeed);
    
    if(data == 'Site added to list'){                    
        handle_refresh(postfix)
    }
}

function handle_search_autocomplete(default_text,txtId){
	if(document.getElementById(txtId).value != "" && document.getElementById(txtId).value != default_text){
		location.href=base_url + "search/" + document.getElementById(txtId).value;
	}
	return false;
}


