/*global grunticon:true*/
(function(grunticon, window){
	"use strict";
	var document = window.document;

	// x-domain get (with cors if available)
	var ajaxGet = function( url, cb ) {
		var xhr = new window.XMLHttpRequest();
		if ( "withCredentials" in xhr ) {
			xhr.open( "GET", url, true );
		} else if ( typeof window.XDomainRequest !== "undefined" ) { //IE
			xhr = new window.XDomainRequest();
			xhr.open( "GET", url );
		}
		if( cb ){
			xhr.onload = cb;
		}
		xhr.send();
		return xhr;
	};

	var svgLoadedCORSCallback = function(callback){
		if( grunticon.method !== "svg" ){
			return;
		}
		grunticon.ready(function(){
			ajaxGet( grunticon.href, function() {
				// check to see if we've already created a style block for this href
				var existingStyle = window
					.document
					.querySelector( 'style[data-href$="'+ grunticon.href +'"]' );
				var ref = grunticon.getCSS( grunticon.href );

				if( existingStyle ){
					// NOTE this will do nothing at all if the embeds are already there
					// and the embed attribute has been removed. The only purpose for this
					// is if the markup has been "rerendered" in the page after the
					// initial embed at which point the embed attributes should exist and
					// the existing embeds should be gone
					grunticon.embedIcons( grunticon.getIcons( existingStyle ) );
				} else {
					var style = document.createElement( "style" );
					style.innerHTML = this.responseText;
					style.setAttribute("data-href", grunticon.href);
					ref.parentNode.insertBefore( style, ref );
					ref.parentNode.removeChild( ref );
					grunticon.embedIcons( grunticon.getIcons( style ) );
				}

				// only call the callback if the href gets something out of the DOM
				// TODO this functions should probably throw an exception when the href
				// is now found in the page
				if( (existingStyle || ref) && typeof callback === "function" ){
					callback();
				}
			} );
		});
	};

	grunticon.ajaxGet = ajaxGet;
	grunticon.svgLoadedCORSCallback = svgLoadedCORSCallback; //TODO: Deprecated
	grunticon.embedSVGCors = svgLoadedCORSCallback;

}(grunticon, this));

@@ -593,16 +593,17 @@
 rn;%0A%09%09%7D%0A
+%0A
 %09%09grunti
@@ -628,123 +628,8 @@
 ()%7B%0A
-%09%09%09ajaxGet( grunticon.href, function() %7B%0A%09%09%09%09// check to see if we've already created a style block for this href%0A%09
 %09%09%09v
@@ -658,17 +658,16 @@
 dow%0A%09%09%09%09
-%09
 .documen
@@ -668,17 +668,16 @@
 ocument%0A
-%09
 %09%09%09%09.que
@@ -734,17 +734,17 @@
 '%22%5D' );%0A
-%09
+%0A
 %09%09%09var r
@@ -784,18 +784,57 @@
 href );%0A
-%0A
 %09
+%09%09callback = callback %7C%7C function()%7B%7D;%0A%0A
 %09%09%09if( e
@@ -845,25 +845,24 @@
 ingStyle )%7B%0A
-%09
 %09%09%09%09// NOTE 
@@ -921,17 +921,16 @@
 y there%0A
-%09
 %09%09%09%09// a
@@ -996,25 +996,24 @@
 or this%0A%09%09%09%09
-%09
 // is if the
@@ -1064,17 +1064,16 @@
 ter the%0A
-%09
 %09%09%09%09// i
@@ -1142,17 +1142,16 @@
 and%0A%09%09%09%09
-%09
 // the e
@@ -1180,17 +1180,16 @@
 be gone%0A
-%09
 %09%09%09%09grun
@@ -1249,22 +1249,90 @@
  );%0A%09%09%09%09
-%7D else
+callback();%0A%09%09%09%7D else if ( ref ) %7B%0A%09%09%09%09ajaxGet( grunticon.href, function()
  %7B%0A%09%09%09%09%09
@@ -1624,263 +1624,8 @@
  );%0A
-%09%09%09%09%7D%0A%0A%09%09%09%09// only call the callback if the href gets something out of the DOM%0A%09%09%09%09// TODO this functions should probably throw an exception when the href%0A%09%09%09%09// is now found in the page%0A%09%09%09%09if( (existingStyle %7C%7C ref) && typeof callback === %22function%22 )%7B%0A
 %09%09%09%09
@@ -1646,16 +1646,15 @@
 %09%09%09%7D
+);
 %0A%09%09%09%7D
- );
 %0A%09%09%7D

move existing style block check outside ajax