var BingApiKey = "EC033770735B6A1392A160118CE66663B9EBEFF1"

jQuery.bingSearch = function(options){
    defOptions = {
        q: "",
        type: "Web",
        start: 0,
        success: function(){
        }
    }
	var q = opt.q;
	var ot = options.type;
    //
    if (options.type == "images") 
        options.type = "Image";
		
    if (options.type == "blogs") 
        options.type = "web";
		q = opt.q + " blog";

    if (options.type == "twitter") {
		options.type = "web";
		q = opt.q + " site:twitter.com";
	}		
    //
    opt = $.extend({}, defOptions, options);
	var data = {
            "JsonCallback": "bingSearchC",
            "JsonType": "callback",
            "Query": q,
            "AppId": BingApiKey,
            "Market": "en-us",
            "Adult": "Off",
            "Options": "EnableHighlighting",
            "Sources": opt.type
        }
	//
	if(opt.start)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, ot, options.start)
        },
        "error": function(d, msg){
        }
    });
    /* */
    function onSuccess(data, st, start){
        var arr = [];
		var sr;
		var results;
		if(st == "web" || st == "twitter" || st == "blogs")
		{
			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)
        })
		var totalResults = 8;
		if(sr)if(sr.Total )totalResults = sr.Total;
		if (opt.success) 
            opt.success({
                search: "bing",				
                start: start,
				totalResults: totalResults,
                type: st,
                results: arr
            })
    }
	function replaceHighlightingCharacters(text, beginStr, endStr)
    {
        // Replace all occurrences of U+E000 (begin highlighting) with
        // beginStr. Replace all occurrences of U+E001 (end highlighting)
        // with endStr.
        var regexBegin = new RegExp("\uE000", "g");
        var regexEnd = new RegExp("\uE001", "g");
              
        return text.replace(regexBegin, beginStr).replace(regexEnd, endStr);
    }
	
	
    function stripFormat(sources){
        if (!sources) 
            return sources;
		return replaceHighlightingCharacters(sources, "", "");
    }
    
    function highlightFormat(sources){
        if (!sources) 
            return sources;
		return replaceHighlightingCharacters(sources, "<b>", "</b>");
    }

}

