
var contentDiv = '.content .editor';

$(document).ready(function() { 
	if(MstIncGlobal.cookie('ChecksLinks') == 'true'){ 
		$(contentDiv).prepend('<div class="testLinks"><a href="javascript:testLinks()" name="a">links</a> </div>');
		$(contentDiv).append('<div class="linksResults"></div>');
	}	
});

var failedCounter = 0;
var successCounter = 0;
var comingSoonCount;

function testLinks() { 

	//var _selfThirdPartyLinks = $('a[href*=http://]')

	var localLinks = $('.content .editor a')
		.not('[href*=http]')
		.not('[target*=_blank]')
		.not('[onclick*=Third]')
		.not('[name]') //not anchors
		.not('[alt*=Back]') //not back to top links
		.not('.comingSoonLink');
	
	var count = localLinks.length; 

	if(count > 0) {
		$('.testLinks').html('<img src="/mstinc/web/images/loading35x35.gif" border="0" />');
		
		localLinks.each(function(i) {
			var thisUrl = $(this).attr('href');
			var element = $(this); 

			$.ajax({
				url: thisUrl, 
				type: 'post',
				method: 'post',
				cache: false,
				statusCode: {
					0: function() { 
						// This is actually a "301 - moved permanently message". Used for moving from http to https pages
						// http://bugs.jquery.com/ticket/6062  
						element.addClass('linkSuccess');
						successCounter++;
						if (!--count) writeResults();						
					},
					200: function() {
						element.addClass('linkSuccess');
						successCounter++;
						if (!--count) writeResults();						
					},		
					301: function() { // external links (does not work)
						alert('301');
						element.addClass('linkSuccess');
						successCounter++;
						if (!--count) writeResults();						
					},					
					404: function() { 
						element.addClass('linkFail');
						failedCounter++;
						if (!--count) writeResults();				
					}					
				},
				error: function() {},
				success: function() {}
			});	
		});
	} 
	//else  { $('.testLinks').addClass('noLinks').html('No links to test'); }	
	else  { writeResults(); }	
}

function writeResults() { 
	failedLinks =     $('a.linkFail');
	passedLinks =     $('a.linkSuccess');
	comingSoonLinks = $('a.comingSoonLink').not('#comingSoonLinkTest');
	thirdPartyLinks = $(contentDiv+' a[onclick*=Third],' +contentDiv+ ' a[target*=_blank],' +contentDiv+ ' a[href*=http]');
	
	$('.testLinks').empty();
	$('.testLinks').append('<a class="linkFail" href="javascript:showOnlyFailedLinks()">' + failedCounter + ' local failed</a>');
	$('.testLinks').append('<a class="linkSuccess" href="javascript:showOnlyPassedLinks()">' + successCounter + ' local passed</a>');
	$('.testLinks').append('<a class="comingSoonLink" href="javascript:showOnlyComingSoonLinks()" id="comingSoonLinkTest">' + comingSoonLinks.length + ' coming soon</a>');
	$('.testLinks').append('<a class="thirdPartyLink" href="javascript:showOnlyThirdPartyLinks()">' + thirdPartyLinks.length + ' tpd/_blank</a>');
	$('.comingSoonLink').addClass('comingSoonLinkIgnored');		
	$(thirdPartyLinks).addClass('thirdPartyLink');	
	
	this.showOnlyFailedLinks = function() {
		failedLinks.detach();
		$(contentDiv).empty().append('<h2>Local links that 404\'d on this page:</h2><br /><br />');
		failedLinks.each(function() { 
			$(this).appendTo($(contentDiv)).wrap('<p />');
		});
	}		
	this.showOnlyPassedLinks = function() {
		passedLinks.detach();
		$(contentDiv).empty().append('<h2>Local links that resolved properly on this page:</h2><br /><br />');
		passedLinks.each(function() { 
			$(this).appendTo($(contentDiv)).wrap('<p />');
		});
	}		
	this.showOnlyComingSoonLinks = function() {
		comingSoonLinks.detach();
		$(contentDiv).empty().append('<h2>Coming soon links on this page:</h2><br /><br />');
		comingSoonLinks.each(function() { 
			$(this).appendTo($(contentDiv)).wrap('<p />');
		});
	}	
	this.showOnlyThirdPartyLinks = function() {
		thirdPartyLinks.detach();
		$(contentDiv).empty().append('<h2>Third party links on this page:</h2><br /><br />');
		thirdPartyLinks.each(function() { 
			$(this).appendTo($(contentDiv)).wrap('<p />');
		});
		$(contentDiv).append('<br /><br /><div class="linkTestButton"><a href="javascript:testAllThirdPartyLinks()">Test all third party links by opening them in iframes</a></div>');
		$(contentDiv).append('<div class="thirdPartyLinksIframes"></div>');
	}			
	
	this.testAllThirdPartyLinks = function() {
		thirdPartyLinks.each(function() { 
			$('.linkTestButton').hide();
			$(this).appendTo('.thirdPartyLinksIframes').append('<br />&nbsp;&nbsp; - <a href="'+$(this).attr("href")+'" class="thirdPartyLink">' + $(this).attr("href") + '</a>').append('<br />');			
			$('.thirdPartyLinksIframes').append('<iframe src="'+$(this).attr("href")+'" class="thirPartyLinkIframeInstance"></iframe> <br /><br />')
		});		
	}
}
