$(document).ready(function(){

	/* Fixes JS Timing issue with sample-credit-report.js */

	/* Turn on 1st title + 1st image */
	$('#sample-credit-report-img-title-6').parent().parent().css('display','block');

	/* Wire up Legacy onClick events. */
	$('#previousLink').click(function() {
		closeAll();
		showprevious();
	});
	$('#nextLink').click(function() {
		closeAll();
		shownext();
	});

	var currentSection = 6;

	function showSection(newSection){
		currentSection = newSection;
		el = document.getElementById('mboxNP');

		$('#leftArrows').css('display','none');
		$('#previousLink').css('display','none');
		$('#spacerBar').css('display','none');
		$('#nextLink').css('display','none');
		$('#rightArrows').css('display','none');

		if (currentSection == 1){
			//el.innerHTML = '<a href="javascript:shownext();">Next</a> &raquo;'; 
			$('#nextLink').css('display','block');
			$('#rightArrows').css('display','block');
		} else if (currentSection == 11){
			//el.innerHTML = '&laquo; <a href="javascript:showprevious();">Previous</a>'; 
			$('#leftArrows').css('display','block');
			$('#previousLink').css('display','block');
		} else{
			//el.innerHTML = '&laquo; <a href="javascript:showprevious();">Previous</a> | <a href="javascript:shownext();">Next</a> &raquo;'; 
			$('#leftArrows').css('display','block');
			$('#previousLink').css('display','block');
			$('#spacerBar').css('display','block');
			$('#nextLink').css('display','block');
			$('#rightArrows').css('display','block');
		}
		return;
	}

	function showprevious(){
		if (currentSection > 1) {
			currentSection--;
			changeDOM(currentSection);
			var el = document.getElementById('mSection'+currentSection);
			if (el){showSection(currentSection);}
		}
	}

	function shownext(){
		if (currentSection < 11) {
			currentSection++;
			changeDOM(currentSection);
			var el = document.getElementById('mSection'+currentSection);
			if (el){showSection(currentSection);}
		}
	}

	/* Left-Nav Menu's onClick event handler. */
	$('.topLinks').click(function() {
		/* Extract the id from the link. */
		var id = $(this).attr('id').replace('topLink','');
		changeDOM(id);
	});
	
	function changeDOM(id) {
		/* Closes any open tooltips. */
		closeAll();

		/* Remove the old highlighting. */
		$('#sbox250Header div').removeClass('smenuCurrent');

		/* Add new Left Nav highlighting. */
		$('#smenu'+id).addClass('smenuCurrent');

		/* Turn off Parent Divs, which are causing spacing issues. */
		$('.imageTitles').parent().parent().css('display','none');

		/* Turn on only the title + image, which matches the link that was clicked on. */
		$('#sample-credit-report-img-title-' + id).parent().parent().css('display','block');
	}

	/* Fixes JS Timing issue with AddSessionId.js */
	var sessId = mboxFactoryDefault.getSessionId().getId();
	for(var i=0, j=document.links.length; i<j; i++) {
		var linko = document.links[i].href;
		if (linko.indexOf('mboxSession') == -1) {
			if(linko.indexOf('creditexpert.com') > -1 || linko.indexOf('creditexpert.co.uk') > -1 || linko.indexOf('joincreditexpert.com') > -1) {
				if(document.links[i].href.indexOf('?') > 0){
					document.links[i].href = (linko + '&mboxSession=' + sessId);
				}
				else{
					document.links[i].href = (linko + '?mboxSession=' + sessId);
				}
			}
		}
	}

	/* Shows/Hides Orange tooltips on images. */
	$('.msiCloseLink').click(function() {
		closeAll();
	});

	$('.showInfoBox').click(function() {
		closeAll();
		var arrId = $(this).attr('id').split('-');
		$('#mS_' + arrId[1] + '_' + arrId[2]).css('display','block');
	});

	function closeAll() {
		$('.mSectionPopups').css('display','none');
	};
});