var BingApiKey = "EC033770735B6A1392A160118CE66663B9EBEFF1"

jQuery.bingSearch = function(options){
    defOptions = {
        q: "",
        type: "Web",
        start: 0,
        success: function(){
        }
    }
    //
    if (options.type == "images") 
        options.type = "Image";
    //
    opt = $.extend({}, defOptions, options);
    /*
     data = "&JsonCallback=bingSearchC" +
     "&JsonType=callback" +
     "&Query=" +
     opt.q +
     "&AppId=" +
     BingApiKey +
     "&Market=" +
     "en-us" +
     "&Web.Offset=" +
     opt.start +
     "&Adult=Off" +
     "&Options=EnableHighlighting" +
     "&Sources=" +
     opt.type;
     //
     url = "http://api.bing.net/json.aspx?" + opt.type + ".Count=8" + data + "&JsonCallback=?";
     $.getJSON(url, onSuccess);
     */
    /* */
	var data = {
            "JsonCallback": "bingSearchC",
            "JsonType": "callback",
            "Query": opt.q,
            "AppId": BingApiKey,
            "Market": "en-us",
            "Adult": "Off",
            "Options": "EnableHighlighting",
            "Sources": opt.type,
        }
	//
	data[opt.type + ".Offset"] = opt.start;
	//
    $.jsonp({
        "url": "http://api.bing.net/json.aspx?" + opt.type + ".Count=8&JsonCallback=?",
        "callback": "bingSearchC",
        "data": data,
        "success": function(data){
            onSuccess(data, opt.type, opt.start)
        },
        "error": function(d, msg){
        }
    });
    /* */
    function onSuccess(data, st, start){
        var arr = [];
		var sr;
		var results;
		if(st == "web")
		{
			sr = data.SearchResponse.Web;
		}else if (st == "images"){
			sr = data.SearchResponse.Image;
		}else if (st == "news"){
			sr = data.SearchResponse.News;
		}        
		if(sr)if(sr.Results)results = sr.Results;
		//	
        if(results)$.each(results, function(i, val){
            var obj = {
                title: highlightFormat(val.Title),
                url: val.Url,
                titleNoFormatting: stripFormat(val.Title),
                content: highlightFormat(val.Description)
            }
            if (st == "images") {
                obj.content = val.Title;
                obj.imageUrl = val.MediaUrl;
                obj.size = val.FileSize;
                obj.width = val.Width;
                obj.height = val.Height;
                obj.tbUrl = val.Thumbnail.Url;
                obj.tbWidth = val.Thumbnail.Width;
                obj.tbHeight = val.Thumbnail.Height;
            }
            if (st == "news") {
                obj.source = val.Source;
                obj.date = val.Date;
                obj.content = highlightFormat(val.Snippet);
            }
            obj.n = i;
            arr.push(obj)
        })
        if (opt.success) 
            opt.success({
                search: "bing",				
                start: start,
				totalResults: data.SearchResponse.Total,
                type: st,
                results: arr
            })
    }
    
    function stripFormat(sources){
        if (!sources) 
            return sources;
        sources = sources.replace(//g, "");
        sources = sources.replace(//g, "");
        return sources;
    }
    
    function highlightFormat(sources){
        if (!sources) 
            return sources;
        sources = sources.replace(//g, "<b>");
        sources = sources.replace(//g, "</b>");
        //sources.replace("/(\xEE\x80\x80)/uix", "<b>");
        //sources.replace("/(\xEE\x80\x81)/uix", "</b>");
        return sources;
    }
}
