/*
Author: Matt Mayers	http://mattmayers.com/
*/

function wowheadLinks(){
	wp_wowhead_jquery('.wowhead').each(function(){
		var el = wp_wowhead_jquery(this)
		wp_wowhead_jquery.ajax({
			type: "GET",
			url: wowhead_wrapper_uri+"?item="+el.attr('title'),
			dataType: "xml",
			success: function(data){
				el.attr('href',wp_wowhead_jquery('link',data).text())
				el.addClass('q'+wp_wowhead_jquery('quality',data).attr('id'))
			}
		})
	})
}

function insert_wowhead_link(myField, i, defaultValue){
	if(!defaultValue) {
		if(document.selection){
			defaultValue=document.selection.createRange().text
		}
		else{
			defaultValue=myField.value.substring(myField.selectionStart, myField.selectionEnd)
		}
	}
	if(!edCheckOpenTags(i)){
		var item_name = prompt("Enter the WoW item name",defaultValue)
		if(item_name){
			wp_wowhead_jquery.ajax({
				type: "GET",
				url: wowhead_wrapper_uri+"?item="+item_name.replace(/\s/g, '+'),
				dataType: "xml",
				success: function(data){
					var href = wp_wowhead_jquery('link',data).text()
					var qID = wp_wowhead_jquery('quality',data).attr('id')
					edButtons[i].tagStart = '<a href="'+href+'" class="q'+qID+'">'
					edInsertTag(myField, i)
				}
			})
		}
	}
	else{
		edInsertTag(myField, i)
	}
}
wp_wowhead_jquery(document).ready(wowheadLinks)