var Widget = {

	// this vars should be set in <head> server-side
	config: {
		base_url: '',
		site_url: ''
	},
	
	// this method is called on every page
	init: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('a[rel*=facebox]').facebox();
			
			$('.tooltip').qtip({
				position: {
					corner: {
						target: 'topMiddle',
						tooltip: 'bottomMiddle'
					}
				},
				style: {
	                 border: {
	                    width: 0,
	                    radius: 6,
						color: '#000'
	                 },
					width: {
						min: 120
					},
					fontSize: '1.1em',
					lineHeight: '1.5em',
					backgroundColor: '#000',
					fontFamily: 'Helvetica',
					padding: 10, 
					textAlign: 'center',
					tip: {
						corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
						color: '#000',
						size: {
							x: 15,
							y : 8
						}
					},
					name: 'dark'
				},
				hide: 'mouseout'
			});
			
			$('.hint').input_hint();
		
				$('#searchability_button').toggle(function() {

					$('#searchability_button').html('Hide Details');
					$('#searchability_base').hide();
					$('#searchability_detailed').show();

				}, function() {

					$('#searchability_button').html('Show Details');
					$('#searchability_base').show();
					$('#searchability_detailed').hide();

				});

				$('#searchperformance_button').toggle(function() {

					$('#searchperformance_button').html('Hide Details');
					$('#searchperformance_base').hide();
					$('#searchperformance_detailed').show();

				}, function() {

					$('#searchperformance_button').html('Show Details');
					$('#searchperformance_base').show();
					$('#searchperformance_detailed').hide();

				});

				$('#icon_links .details a').bind('click', function() {
					$('#score_overview').hide();
					$('#get_full_report').hide();
					$('#score_details').show();
					return false;
				});

				$('#icon_links .more_info a').bind('click', function() {
					$('#get_full_report').hide();
					$('#score_details').hide();
					$('#score_overview').hide();
					$('#score_info').show();
					return false;
				});

					$('#icon_links .backtoscore a').bind('click', function() {
						$('#get_full_report').hide();
						$('#score_details').hide();
						$('#score_info').hide();
						$('#score_overview').show();
						return false;
					});

			
		});
	},
	
	widget_form_full: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('#get_full_report_form').bind('submit', function(e) {
				e.preventDefault();

				var obj = new JSONscriptRequest($('#get_full_report_form').attr('action') + '?email_address=' + $('#get_full_report #email_address_field').val() + '&newsletter=' + $('#get_full_report #newsletter').val() + '&rid=' + $('#get_full_report #rid').val() + '&report_url=' + $('#get_full_report #report_url').val());     
				obj.buildScriptTag(); // Build the script tag     
				obj.addScriptTag(); // Execute (add) the script tag
				//end addScript

			});

			$('.submit_full_form').bind('click', function(e) {
					e.preventDefault();
					
					var obj = new JSONscriptRequest($('#get_full_report_form').attr('action') + '?email_address=' + $('#get_full_report #email_address_field').val() + '&newsletter=' + $('#get_full_report #newsletter').val() + '&rid=' + $('#get_full_report #rid').val() + '&report_url=' + $('#get_full_report #report_url').val());     
					obj.buildScriptTag(); // Build the script tag     
					obj.addScriptTag(); // Execute (add) the script tag
					//end addScript

			});

				$('a.full_report').bind('click', function() {
					$('#score_overview').hide();
					$('#score_details').hide();
					$('#score_info').hide();
					$('#get_full_report').show();
					return false;
				});
			
		});
	},
	
	widget_form: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('#the_widget_getit a').bind('click', function(e) {
				e.preventDefault();				
				jQuery.facebox('<div id="the_widget_callback_modal" class="default_modal"><div id="container_widget" class="get_this_widget"><div id="widget_closer"><a href="#" onclick="jQuery(document).trigger(\'close.facebox\'); return false;" style="background-color:#bbb; padding:8px; color: #fff; text-decoration:none;">CLOSE WINDOW</a></div><p>Just copy and paste the html below.</p><p><textarea>&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;http://sb.pearanalytics.com/css/sb.css&quot; /&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://sb.pearanalytics.com/js/jquery.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;jQuery.noConflict();&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://sb.pearanalytics.com/js/jquery.input_hint.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://sb.pearanalytics.com/js/jquery.form.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://sb.pearanalytics.com/js/facebox/facebox.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://sb.pearanalytics.com/js/jquery.qtip.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://sb.pearanalytics.com/js/sb_widget.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;Widget.widget_form();&lt;/script&gt;&lt;div id=&quot;the_widget&quot;&gt; &lt;a href=&quot;http://serverbeach.com/&quot; id=&quot;the_widget_pear_link&quot;&gt;by ServerBeach Dedicated Servers&lt;/a&gt; &lt;form action=&quot;http://sb.pearanalytics.com/serverbeach/index&quot; method=&quot;get&quot; id=&quot;the_widget_form&quot;&gt; &lt;input type=&quot;text&quot; name=&quot;url&quot; class=&quot;widget_input hint&quot; value=&quot;&quot; id=&quot;url_input&quot; title=&quot;www.example.com&quot; /&gt; &lt;input type=&quot;image&quot; src=&quot;http://sb.pearanalytics.com/images/sb/sb_widget_submit.gif&quot; name=&quot;submit&quot; class=&quot;widget_submit&quot; /&gt; &lt;/form&gt; &lt;div id=&quot;the_widget_getit&quot;&gt; &lt;a href=&quot;http://www.pearanalytics.com&quot;&gt;Get this widget&lt;/a&gt; &lt;/div&gt;&lt;/div&gt;</textarea></p></div></div>');
			});
			
			$('#the_widget_form').bind('submit', function(e) {
				e.preventDefault();
				if ($('#the_widget_form #url_input').val() === '')
				{
				jQuery.facebox('<div id="the_widget_callback_modal" class="default_modal"><div id="container_widget" class="get_this_widget"><center class="sb_widget_loading">Please enter a website url to continue.<div class="clearfix" style="display:block; margin-top:15px;"><a href="#" onclick="jQuery(document).trigger(\'close.facebox\'); return false;" style="background-color:#bbb; padding:8px; color: #fff; text-decoration:none;">Close this window.</a></div></center></div></div>');
				}
				else
				{
					
					jQuery.facebox('<div id="the_widget_callback_modal" class="default_modal"><center class="sb_widget_loading"><img src="http://sb.pearanalytics.com/images/ajax-loader-big.gif" style="margin:30px;" /><br />We’re crunching the numbers now…<br />your scores will be ready shortly<br />This can take up to 30 seconds</center></div>');
					
					var generator = $('#the_widget_form #generator').val();
					if (generator)
					{
						generator = '&generator=' + generator;
					}
					else
					{
						generator = '';
					}

					var obj = new JSONscriptRequest($('#the_widget_form').attr('action') + '?url=' + $('#the_widget_form #url_input').val() + generator);     
					obj.buildScriptTag(); // Build the script tag     
					obj.addScriptTag(); // Execute (add) the script tag
					//end addScript


					$('#the_widget_form #url_input').val('');
					
				}
			});
			
		});
	}
	
};

Widget.init();

function callback_email(data)
{
	jQuery('#get_full_report').html(data.html_content);
	Widget.init();
}

function callback_loader(data)
{
	jQuery('#the_widget_callback_modal').html(data.html_content);
	Widget.init();
}

// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}
