// myfy.utils necessitates jQuery
if (typeof myfy === 'undefined') {
    var myfy = {};
}

myfy.utils = function() {
	
	
	/*
	 * [randomRange]
	 * Return a random int between and min and max value 
	 * 
	 * @param min{int}
	 * @param max{int}
	 */
	var _this = {
		onReady: function() {
			$('img.rollover').hover(
				function(){
					this.src = this.src.replace("off","over");
				}, function(){
					this.src = this.src.replace("over","off");
				}
			)
		}
	}
	
	$(document).ready(function(){
		_this.onReady();
	})

	//public utilities;
	return {
		
		/*
		 * [log]
		 * Send message to console.log if it exists. If doesn't exist use alert.  
		 * 
		 * @param message{string}
		 */
        log: function(message) {
            //if('console' in window && 'log' in window.console)
            if (typeof window.console != 'undefined' && typeof window.console.log != 'undefined') {
                console.log(message);
            }
            else {
                // do nothing
                alert(message);
            }
        }, 
		
		/*
		 * [loadData]
		 * Basic Ajax load using jQuery  
		 * 
		 * @param message{string}
		 */
		
		loadData: function(_url, _async, _timeout, _datatype, _callback){
			$.ajax({
				url: _url || myfy.utils.log("Could not load Data. No Data File Specified"),
				async: _async || false,
				timeout: _timeout || 10000,
				dataType: _datatype || "xml",
				
				error: function displayError(request, errorType, errorThrown) { 
					
					if(request)
						myfy.utils.log("Request: " + request);
						
					if(errorType)	
						myfy.utils.log("Error Type: " + errorType);
						
					if(errorThrown)	
						myfy.utils.log("Error Thrown: " + errorThrown);						
						
				},
				
				success: function(data){
					/*var xml;
					if ($.browser.msie && typeof data == "string") {
						xml = new ActiveXObject("Microsoft.XMLDOM");
						// xml.async = false;
						xml.loadXML(data);
					
					} else {
						xml = data;
					}*/
					_callback(data);
					//myfy.utils.log("asdfasdf");
				}
				
			})
			
		}, 
		
		loadImage: function (targetContainer, sourceSrc) {
						
			if(!$(targetContainer).hasClass("spinner")){
				$(targetContainer).addClass("spinner");	
			}
			
			$("img", targetContainer).load(function() {
				$("img", targetContainer).hide();									  
			  
				$(this).fadeIn("slow", function(){
				   $(this).parent().removeClass("spinner");						  
				});
			  
			  	//remove all previous errors
				$(".error").remove();
			  
			}).error(function () {
				var msg = "Could not load image: " + sourceSrc;
				$("<div style=\"position:absolute; top: 10px; left: 10px; color:#FFF; font-size:14px; \" class=\"error\">"+msg+"</div>").appendTo(targetContainer);
			}).attr('src', sourceSrc);
  		}, 
		
		/*
		 * [randomRange]
		 * Return a random int between and min and max value 
		 * 
		 * @param min{int}
		 * @param max{int}
		 */
		
		randomRange: function (min, max) {
			var range = max - min + 1;
  			return Math.floor(Math.random()*range + min);
		}
		
		
    }
}();
