//loadFirebugConsole();

// Intialize the DTM Loader
if(window.DTM_LOADER === undefined) {
	var DTM_LOADER = {};
	DTM_LOADER.widgets = {};
	DTM_LOADER.queue = [];
	// Define action to take once yui loader is present on the page
	DTM_LOADER.startup = function() {
		var startup = new YAHOO.util.YUILoader();
		startup.require(['event', 'dom']);
		startup.loadOptional = false;
		startup.insert({
			onSuccess:function() {
				DTM_LOADER.available = true;
//        YAHOO.util.Event.on(window, "load", DTM_LOADER.clearQueue);
        YAHOO.util.Event.onDOMReady(DTM_LOADER.clearQueue);
			}
		});
	}
	DTM_LOADER.addToQueue = function(widget) {
		DTM_LOADER.queue.push(widget);
	}
	DTM_LOADER.clearQueue = function() {
		if(DTM_LOADER.queue.length > 0) {
			var widget = DTM_LOADER.queue.shift();
			DTM_LOADER.load(widget);
			DTM_LOADER.clearQueue();
		}
	}
	DTM_LOADER.register = function(widget) {
		DTM_LOADER.addToQueue(widget);
		if(DTM_LOADER.available) {
			DTM_LOADER.clearQueue();
		}
	}
	DTM_LOADER.load = function(widget) {
		var module, requires = DTM_LOADER.widgets[widget].requires;
		DTM_LOADER.widgets[widget].loader = new YAHOO.util.YUILoader();
		for(var i=0; i<DTM_LOADER.widgets[widget].modules.length; i++) {
			module = DTM_LOADER.widgets[widget].modules[i];
			requires.push(module.name);
			DTM_LOADER.widgets[widget].loader.addModule(module);
		}
		DTM_LOADER.widgets[widget].loader.require(requires);
		DTM_LOADER.widgets[widget].loader.loadOptional = false;
		DTM_LOADER.widgets[widget].loader.data = DTM_LOADER.widgets[widget].launcher;
		DTM_LOADER.widgets[widget].loader.combine = true;
		DTM_LOADER.widgets[widget].loader.insert({
			onSuccess:function(launcher) {
				launcher.data();
			}
		});
	}
	DTM_LOADER.poll = function() {
		if(window.YAHOO !== undefined && YAHOO.util !== undefined && YAHOO.util.YUILoader !== undefined) {
			DTM_LOADER.startup();
		} else {
			window.setTimeout(DTM_LOADER.poll,10);
		}
	}
	// Add the Loader to the page if it is not defined
	if(window.YAHOO === undefined || YAHOO.util === undefined || YAHOO.util.YUILoader === undefined) {
		DTM_LOADER.yuiloader = function() {
			var s = document.createElement('script');
			s.setAttribute('type','text/javascript');
			//s.setAttribute('src','http://yui.yahooapis.com/2.5.2/build/yuiloader/yuiloader-beta-min.js');
			s.setAttribute('src','http://yui.yahooapis.com/2.8.1/build/yuiloader/yuiloader-min.js');
			document.getElementsByTagName('head')[0].appendChild(s);
		}();
	}
	DTM_LOADER.poll();
}

function generateFeedURL(options) {
	//console.log("returning sample feed..")
	if (options === null)
	   options = {};

	//return("/network_source/0.0.1/library/sac-badge/originals/core/j/sample_feed.js"); // For development

	//var base_url = "http://localhost.com:3000/"; // dev
	var base_url = "http://adopt.dogtime.com/"; // prod
  //var base_url = "http://adopt.staging.dogtime.com/"; // staging

	var species = options.species || 'cat';
	var url = base_url + "search/animals/"

  if (options.pap_shelter_id)
    url = url + "by_shelter/pap/" + options.pap_shelter_id + "/";

  url = url + species;

	if (options.breed)
		url += "/" + options.breed;

	url += "/widget.json?limit=20&callback=DTM.widget.sacBadge.callback"

	if (options.random)
	  url += '&random=' + options.random;

	return url;
}

if(DTM_LOADER.widgets.sacBadge === undefined) {
	DTM_LOADER.widgets.sacBadge = {};
	DTM_LOADER.widgets.sacBadge.domClass 	= "wi-sac";

	if (window.dtm_species === undefined)
		dtm_species = null;
	if (window.dtm_breed === undefined)
		dtm_breed = null;
  if (window.dtm_pap_shelter_id === undefined)
      dtm_pap_shelter_id = null;
  if (window.dtm_random === undefined)
      dtm_random = null;

	DTM_LOADER.widgets.sacBadge.feedURL 	= generateFeedURL({'species':dtm_species, 'breed':dtm_breed, 'random':dtm_random, 'pap_shelter_id':dtm_pap_shelter_id});
	DTM_LOADER.widgets.sacBadge.assetURL 	= "/network_source/0.0.1/library/sac-badge/originals/assets/";
	DTM_LOADER.widgets.sacBadge.coreURL 	= "/network_source/0.0.1/library/sac-badge/originals/core/";
	DTM_LOADER.widgets.sacBadge.title 		= null;
	DTM_LOADER.widgets.sacBadge.appURL 		= "http://apps.facebook.com/save-a-cat";
	DTM_LOADER.widgets.sacBadge.modules 	= [	{ name : 'DTM.widget.sacBadge', type : 'js', fullpath : (DTM_LOADER.widgets.sacBadge.coreURL + 'j/dtm-sac-badge-324x160.js') },
																							  { name : 'DTM.widget.sacBadgeCSS', type : 'css', fullpath : (DTM_LOADER.widgets.sacBadge.assetURL + 'c/dtm-sac-badge-324x160.css') } ];
	DTM_LOADER.widgets.sacBadge.requires 	= ['dom', 'element', 'event', 'json', 'animation', 'element', 'carousel'];
	DTM_LOADER.widgets.sacBadge.launcher = function() {
		DTM.widget.sacBadge.init({ domClass : DTM_LOADER.widgets.sacBadge.domClass, url : DTM_LOADER.widgets.sacBadge.feedURL, coreURL : DTM_LOADER.widgets.sacBadge.coreURL, assetURL : DTM_LOADER.widgets.sacBadge.assetURL, title : DTM_LOADER.widgets.sacBadge.title, appURL : DTM_LOADER.widgets.sacBadge.appURL });
	};
	DTM_LOADER.register('sacBadge');
}

