var YahooApiKey = "BW3CFMfV34HIgkHiJsQGuaJDFfdFoQ60n3.RkJuaUvn2h3uF_50HUhSxUYPgTLaFhA--"
var YahooServices = {
    'images': 'http://search.yahooapis.com/ImageSearchService/V1/imageSearch',
    'local': 'http://local.yahooapis.com/LocalSearchService/V1/localSearch',
    'news': 'http://search.yahooapis.com/NewsSearchService/V1/newsSearch',
    'video': 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch',
    'web': 'http://search.yahooapis.com/WebSearchService/V1/webSearch'
};

jQuery.yahooSearch = function(options){
    defOptions = {
        q: "",
        type: "web",
        start: 0,
        success: function(){
        }
    }
    opt = $.extend({}, defOptions, options);
    $.jsonp({
        "callback": "yahooSearchC",
        "url": YahooServices[opt.type] + "?callback=yahooSearchC",
        "data": {
            "query": opt.q,
            "appid": YahooApiKey,
            "results": "8",
            "region": "us",
            "start": opt.start,
            "adult_ok": "1",
            "output": "json"
        },
        "success": function(data){
			onSuccess(data, opt.type, opt.start)
		},
        "error": function(d, msg){
        }
    });
    
    function onSuccess(data, st, start){
        var arr = []
        var results = data.ResultSet.Result
        $.each(results, function(i, val){
            var obj = {};
            obj["title"] = val.Title;
            obj["url"] = val.ClickUrl;
			obj["titleNoFormatting"] = val.Title; 
            //
            if (st == "web") {
                obj["content"] = val.Summary;
                obj["visibleUrl"] = val.DisplayUrl;
                //obj["cacheUrl"] = val.Cache.Url;
                obj["content"] = val.Summary;
            }
            if (st == "images") {
                obj["url"] = val.RefererUrl;
                obj["content"] = val.Summary;
                obj["imageUrl"] = val.ClickUrl;
                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.NewsSource;
                obj["date"] = val.PublishDate;
                obj["content"] = val.Summary;
            }
            //
			obj.n = i;			
            arr.push(obj);
            
        })
        if (opt.success) 
            opt.success({				
                search: "yahoo",
				start: start,
				totalResults: data.ResultSet.totalResultsAvailable,
				type: st,
                results: arr
            })
    }
}
