$(document).ready(
	
	function () {

	  var just_loaded = true;
	  var searchterms_loaded = false;
	  var clickstream_loaded = false;
	  var demographics_loaded = false;
	  var jigsaw_loaded = false;
	  var relatedlinks_loaded = false;
	  var trafficstats_loaded = false;
	  var reviews_loaded = false;
	  	
	  	
	  // Stats widget
	  
	  $('#tw-tabmodule ul.tw-tabnav li').click(function () {
			$(this).find('a').click();
	  });

	  if (($.cookie('ax_gr_last_stat') == null) || ($.cookie('ax_gr_last_stat') == '')) {
		$.cookie('ax_gr_last_stat', '#reach', { expires: 1});	  
	  }

	  var tabTwContainers = $('#tw-tabmodule div.tw-table');

	  $('#tw-tabmodule ul.tw-tabnav a').click(function () {
		tabTwContainers.removeClass('statselected').hide();
		tabTwContainers.filter(this.hash).addClass('statselected').show();
		$('#tw-tabmodule ul.tw-tabnav li').removeClass('selected');
		$(this).parent().addClass('selected');
		$.cookie("ax_gr_last_stat", this.hash, { expires: 1});
		
		 $('.tw-graph img').each( function(i) {
		 	 var visible_table = $('#tw-tabmodule div.statselected');
		 	 var y = visible_table.attr("y");		 	 
		 	 var c = visible_table.attr("c");
			 var new_traffic_graph_url = this.src; 
			 new_traffic_graph_url = new_traffic_graph_url.replace(/\&y=\w\&/,'&y='+y+'&');
			 new_traffic_graph_url = new_traffic_graph_url.replace(/\&c=.{0,1},\&/,'&c='+c+'&');
			 this.src =new_traffic_graph_url;
		 }
		 );
         trackCustomEvent && trackCustomEvent.call && trackCustomEvent('widget/'+$(this).text());
			 
		return false;
	  });

	  // Main tabs	  
	  
	  if (location.hash != '') {
		$.cookie('ax_gr_last_tab', location.hash, { expires: 1});	  	  
	  } else if (($.cookie('ax_gr_last_tab') == null) || ($.cookie('ax_gr_last_tab') == '')) {
		$.cookie('ax_gr_last_tab', '#trafficstats', { expires: 1});	  
	  }

	  var tabContainers = $('#siteinfotabs > div');
	  var tabNavList = $('#siteinfotabs ul.tabnav');
	  tabNavList.find('li').click(function () {
			$(this).find('a').click();
	  });

		$('#showReviews').click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('#siteinfotabs ul.tabnav li').removeClass('selected');
			$.cookie("ax_gr_last_tab", this.hash, { expires: 1});
    	$('#siteinfotabs ul.tabnav li#reviewsTab').addClass('selected');
			$('#reviews').show();
		}
		)
																			
		
		
	  tabNavList.find('a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('#siteinfotabs ul.tabnav li').removeClass('selected');
		$.cookie("ax_gr_last_tab", this.hash, { expires: 1});
		
		$(this).parent().addClass('selected');
		if (!just_loaded) {
        	trackCustomEvent && trackCustomEvent.call && trackCustomEvent('tab/'+$(this).text());
        	
        	if (flip_ads) {
				var ord = Math.floor(Math.random()*1E16);
				$('iframe.adframe').each( function() {
					var ad_url = this.src; 
					ad_url = ad_url.replace(/\&ord=\d+\&/,'&ord='+ord+'&');
					this.src = ad_url;
				});
			}
        
        }
        if (!searchterms_loaded && (this.hash == '#keywords')) {
        	searchterms_loaded = true;
        	var html_snippet = "/site/searchterms;"+site_signature+"/"+escape(site)+"/?adult="+site_is_adult+"&category="+site_category;
        	$("#keywords-content").load(html_snippet);
        }
        if (!clickstream_loaded && (this.hash == '#clickstream')) {
        	clickstream_loaded = true;
        	var html_snippet = "/site/clickstream;"+site_signature+"/"+escape(site);
        	$("#clickstream").load(html_snippet);
        }
        if (!demographics_loaded && (this.hash == '#demographics')) {
        	demographics_loaded = true;
        	var html_snippet = "/site/demographics;"+site_signature+"/"+escape(site);
        	$("#demographics-content").load(html_snippet);
        }
        if (!jigsaw_loaded && (this.hash == '#contactinfo')) {
        	jigsaw_loaded = true;
        	var html_snippet = "/site/jigsaw;"+site_signature+"/"+escape(site);
        	$("#jigsaw").load(html_snippet);
        }  
        
        if (!relatedlinks_loaded && (this.hash == '#relatedlinks')) {
        	relatedlinks_loaded = true;
					$('img.favicon').each(function(){
						var site = $(this).attr('site');
			  		$(this).attr('src', 'http://www.alexa.com/cgi-bin/favicon.php?url='+site);
					});        
        }
        if (!reviews_loaded && (this.hash == '#reviews')) {
        	reviews_loaded = true;
        }
        if (!trafficstats_loaded && (this.hash == '#trafficstats')) {
        	trafficstats_loaded = true;
        	$('#tw-tabmodule ul.tw-tabnav a').filter("[href=\'"+$.cookie('ax_gr_last_stat')+"\']").click();
        }
        just_loaded = false;
        
		return false;
	  }).filter("[href=\'"+$.cookie('ax_gr_last_tab')+"\']").click();

		


	  $('#date-range').change(function() {
		 var selected = $(this).children("option:selected");       
		 if ($.inArray(selected.val(), new Array('7d', '1m', '3m', '6m', '1y','2y','5y')) >= 0) {
			 $('.tw-graph img').each( function(i) {
				 var new_traffic_graph_url = this.src;
				 new_traffic_graph_url = new_traffic_graph_url.replace(/\&r=\d\w\&/,'&r='+selected.val()+'&');
				 this.src =new_traffic_graph_url;
				 $("option[value='" +selected.val()+ "']").attr('selected','selected');
			 }
			 );
			 $.cookie("ax_gr_range", selected.val(), { expires: 365});
		 }
	  });
	  
	  $('#tw-compare').click(function() {
			
	  		var graph_url = $('.tw-graph img').attr('src');
	  		var pattern = new RegExp(/&u=(.*?)&/);
			var mymatch = pattern.exec(graph_url);
	  		// Compose a string of all the urls
	  		
	  		 var url_string = mymatch.length > 0 ? mymatch[0] : "";
			 $('.tw-sites').each( function(i) {
			 	if (this.value !== '') {
				 	url_string = url_string + "&u=" + this.value;
				 }
			 }
			 );
			 url_string = url_string + "&";
			 	  		
			 $('.tw-graph img').each( function(i) {
				 var new_traffic_graph_url = this.src;
				 new_traffic_graph_url = new_traffic_graph_url.replace(/&u=.*\&/,url_string);
				 this.src =new_traffic_graph_url;
			 }
			 );
	  });	  
	  
	  $('#toggleMoreCountry').click(function() {
	  	$('#moreCountry').toggle().removeClass('textOff');
	  	if ($(this).text() == 'More') {
	  		$(this).text('Less');
	  	} else {
	  		$(this).text('More');	  	
	  	}
	  });

	  $('#toggleMoreTraffic').click(function() {
	  	$('#moreTraffic').toggle().removeClass('textOff');	  
	  	if ($(this).text() == 'More') {
	  		$(this).text('Less');
	  	} else {
	  		$(this).text('More');	  	
	  	}
	  });
	  
	  $('.searchLink').click(function() {
			var text=$(this).text();
			window.location = '/search?q='+text;
		});
		
		$('#moreRelated').click(function() {
	  	       	var html_snippet = "/site/relatedlinks;"+site_signature+"/"+escape(site);
        	$('#relatedlinks-content').load(html_snippet);
					});
	
	
		// add a "rel" attrib if Opera 7+
		if(window.opera) {
			if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
				$("a.jqbookmark").attr("rel","sidebar");
			}
		}
	
		$("a.jqbookmark").click(function(event){
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link
			var url = this.href;
			var title = this.title;
	
			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title);
			} else if(window.opera) { // Opera 7+
				return false; // do nothing - the rel="sidebar" should do the trick
			} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
				 alert('Unfortunately, this browser does not support the requested action,'
				 + ' please bookmark this page manually.');
			}
	
		});

	
	}
);
