// legacy (as in BETA) widgets

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}

function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

function concatObject(obj) {
  str='';
  for(prop in obj)
  {
    str+=prop + " value :"+ obj[prop]+"\n";
  }
  return(str);
}


function get_testimonials(hash) {
	host = unescape(window.location.hostname);
	/*
	www = host.substr(0,4);
	if(www != "www.") {
		host = "www."+host;
	}
	*/
	if(undefined == window.r) {
		r = 0;
	}
	
	if(undefined == window.cat) {
		cat = 0;
	}
	
	if(undefined == window.prod) {
		prod = 0;
	}
	
	if(undefined == window.tid) {
		tid = 0;
	}
	
	if(typeof site_url == "undefined") {
		site_url = "https://www.testimonialdirector.com";
	}
	
	var script= document.createElement('script');
	var url_vars = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	script.type= "text/javascript";
	script.src=site_url+"/get_dyn_testimonials.php?h="+hash+"&d="+host+"&v="+url_vars+"&r="+r+"&cat="+cat+"&prod="+prod+"&tid="+tid;
	document.body.appendChild(script);
}

function updateComments(txt) {
	document.getElementById(div_id).innerHTML = txt;
	if(typeof do_scroll == 'function') {
		do_scroll();
	} 
}


// new widgets

function get_testimonial_data(hash,div_id,limit) {
DomReady.ready(function() {
	
	host = unescape(window.location.hostname);
	/*
	www = host.substr(0,4);
	if(www != "www.") { 
		host = "www."+host;
	}
	*/
	if(undefined == window.r) {
		r = 0;
	}
	if(undefined == window.cat) {
		cat = 0;
	}
	if(undefined == window.prod) {
		prod = 0;
	}
	if(undefined == window.tid) {
		tid = 0;
	}
	
	if(typeof site_url == "undefined") {
		site_url = "https://www.testimonialdirector.com";
	}
	if(typeof limit == "undefined") {
		limit = "no";
	}
	
	var script= document.createElement('script');
	var url_vars = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	script.type= "text/javascript";
	script.src=site_url+"/get_dyn_testimonials_beta.php?h="+hash+"&d="+host+"&div="+div_id+"&v="+url_vars+"&r="+r+"&cat="+cat+"&prod="+prod+"&tid="+tid+"&limit="+limit;
	
	//script=script.replace(/\\'/g,'\'');
	//script=script.replace(/\\"/g,'"');
	//script=script.replace(/\\0/g,'\0');
	//script=script.replace(/\\\\/g,'\\');
	
	
	//alert(concatObject(script));
	script = eval(script);

	document.body.appendChild(script);

});
}

function show_testimonials(txt,div_id,branding,affiliator) {
	
	if(typeof site_url == "undefined") {
		site_url = "https://www.testimonialdirector.com";
	}
	
	var affiliate_code = '';
	
	if(affiliator != 0) {
		affiliate_code = '?affiliate='+affiliator;
	}
	
	if(typeof do_scroll != 'function' && branding == 1) {
		txt += '<div class="td_branding" style="display: block"><a style="display: inline-block" href="'+site_url+affiliate_code+'" target="_blank"><img src="'+site_url+'/_img/powered_by.gif" width="173" height="23" alt="Powered by Testimonial Director" border="0" /></a></div>'
	}
	
	document.getElementById(div_id).innerHTML = stripslashes(txt);
	
	if(typeof do_scroll == 'function') {
		do_scroll();
	
		if(branding == 1) {
		$('#'+div_id).append('<div class="td_branding" style="display: block"><a style="display: inline-block" href="http://www.testimonialdirector.com/'+affiliate_code+'" target="_blank"><img src="https://www.testimonialdirector.com/_img/powered_by.gif" width="173" height="23" alt="Powered by Testimonial Director" border="0" /></a></div>');
	}
		
	}
	
}



(function(){

    var DomReady = window.DomReady = {};

	// Everything that has to do with properly supporting our document ready event. Brought over from the most awesome jQuery. 

    var userAgent = navigator.userAgent.toLowerCase();

    // Figure out what browser is being used
    var browser = {
    	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
    	safari: /webkit/.test(userAgent),
    	opera: /opera/.test(userAgent),
    	msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )),
    	mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))
    };    

	var readyBound = false;	
	var isReady = false;
	var readyList = [];

	// Handle when the DOM is ready
	function domReady() {
		// Make sure that the DOM is not already loaded
		if(!isReady) {
			// Remember that the DOM is ready
			isReady = true;
        
	        if(readyList) {
	            for(var fn = 0; fn < readyList.length; fn++) {
	                readyList[fn].call(window, []);
	            }
            
	            readyList = [];
	        }
		}
	};

	// From Simon Willison. A safe way to fire onload w/o screwing up everyone else.
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
	    window.onload = func;
	  } else {
	    window.onload = function() {
	      if (oldonload) {
	        oldonload();
	      }
	      func();
	    }
	  }
	};

	// does the heavy work of working through the browsers idiosyncracies (let's call them that) to hook onload.
	function bindReady() {
		if(readyBound) {
		    return;
	    }
	
		readyBound = true;

		// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
		if (document.addEventListener && !browser.opera) {
			// Use the handy event callback
			document.addEventListener("DOMContentLoaded", domReady, false);
		}

		// If IE is used and is not in a frame
		// Continually check to see if the document is ready
		if (browser.msie && window == top) (function(){
			if (isReady) return;
			try {
				// If IE is used, use the trick by Diego Perini
				// http://javascript.nwbox.com/IEContentLoaded/
				document.documentElement.doScroll("left");
			} catch(error) {
				setTimeout(arguments.callee, 0);
				return;
			}
			// and execute any waiting functions
		    domReady();
		})();

		if(browser.opera) {
			document.addEventListener( "DOMContentLoaded", function () {
				if (isReady) return;
				for (var i = 0; i < document.styleSheets.length; i++)
					if (document.styleSheets[i].disabled) {
						setTimeout( arguments.callee, 0 );
						return;
					}
				// and execute any waiting functions
	            domReady();
			}, false);
		}

		if(browser.safari) {
		    var numStyles;
			(function(){
				if (isReady) return;
				if (document.readyState != "loaded" && document.readyState != "complete") {
					setTimeout( arguments.callee, 0 );
					return;
				}
				if (numStyles === undefined) {
	                var links = document.getElementsByTagName("link");
	                for (var i=0; i < links.length; i++) {
	                	if(links[i].getAttribute('rel') == 'stylesheet') {
	                	    numStyles++;
	                	}
	                }
	                var styles = document.getElementsByTagName("style");
	                numStyles += styles.length;
				}
				if (document.styleSheets.length != numStyles) {
					setTimeout( arguments.callee, 0 );
					return;
				}
			
				// and execute any waiting functions
				domReady();
			})();
		}

		// A fallback to window.onload, that will always work
	    addLoadEvent(domReady);
	};

	// This is the public function that people can use to hook up ready.
	DomReady.ready = function(fn, args) {
		// Attach the listeners
		bindReady();
    
		// If the DOM is already ready
		if (isReady) {
			// Execute the function immediately
			fn.call(window, []);
	    } else {
			// Add the function to the wait list
	        readyList.push( function() { return fn.call(window, []); } );
	    }
	};
    
	bindReady();
	
})();