function isAdmin()
{
	var winUrl = new String(window.location.href);
	if (winUrl.match('admin=1') != null)
		return true;
	return false;	
}


function setAdminInterface()
{
	jQuery('#Content ul.divisionSublinks>li>a').not('.directLink')
		.add('#Footer a')
		.each(function()
				{
					var adminLink = jQuery(this);
					adminLink.attr('href', adminLink.attr('href') + '?admin=1')
				});
	
	jQuery('#Content .InnerContent')
		.add('#Content .InnerContent ul.divisionSublinks')
		.css({
			 	visibility: 'visible',
				display: 'block'
			 });
	jQuery('#Testimonials, #testimonials, #Footer, #FreeSystemCheck, .FooterLogo, .FooterContent').css({height: 'auto', background: '#FFF', position: 'static'});
	
	jQuery('#BottomInfoBubbles').css({position: 'relative', height: 'auto', marginBottom: '20px'});
	jQuery('#LeftInfoBubble').css({background: '#FFF', position: 'relative', height: 'auto', paddingBottom: '10px'});
	jQuery('#BottomInfoBubbles ul').css({background: '#FFF', height: 'auto', position: 'relative'});
	
	jQuery('.infoZone').css({display: 'block', height: 'auto'});	
	jQuery('.infoBubble').css({position: 'static', visibility: 'visible', margin: 'auto', background: 'none', border: '1px solid #CCC'});
}

function resizeWrappers(){
	jQuery('#Content').css('height', '548px');
}

// the custom popups
function initTIpopups()
{
	var popupLinks = jQuery('a.popupTrigger').not('directLink').add('#ResourcesNav a');
	popupLinks
		.each(function(index)
			{
				var ind = index;
				var triggerLink = jQuery(this);
				
				triggerLink
					.click(function()
							{
								TIslideshow.terminate();
								 jQuery('#Content .ContentCell').removeClass('expanded');
								 TIslideshow.hideMaskLayers();
								 jQuery('#Content .ContentLayer').removeClass('expanded');
								 showPopupLinks();
							});
				
				window.customPopups['popup' + ind] = new dc_Popup(triggerLink, {
																		popupIframeName: 'outerPopup' + ind,
																		popupWidth: 893, 
																		popupHeight: 492, 
																		popupBackground: "#FFFFFF",
																		popupLeft:  '0px', 
																		popupTop: '58px',
																		showPopupMask: false,
																		showCloseButton: false
																		});
			});
	
	var ContentSublinks = jQuery('#Content ul.divisionSublinks>li>a[id!=catalog]').not('.directLink');
	ContentSublinks
		.each(function(index)
			{
				var ind = index;
				var triggerLink = jQuery(this);
				
				triggerLink.click(function()
								  {
									 jQuery('#Content .ContentCell').removeClass('expanded');
									 jQuery('#Content .ContentLayer').removeClass('expanded');
										TIslideshow.terminate();
										//jQuery('#Content>iframe').remove();
										showPopupLinks();
								  });
				
				
				window.customPopups['popup' + ind] = new dc_Popup(triggerLink, {
																		popupIframeName: 'popup' + ind,
																		popupWidth: 893, 
																		popupHeight: 492,
																		popupBackground: "#FFFFFF",
																		popupLeft:  '0px', 
																		popupTop: '58px',
																		showPopupMask: false,
																		showCloseButton: false
																		});
			});
	setGSAPopup();
}

function setGSAPopup()
{
	var gsaLink = jQuery('a#catalog');
	var ContentSection = jQuery('#Content');	
	gsaLink.click(function()
					  {
						TIslideshow.terminate();
						//jQuery('body>iframe').remove();
						jQuery('#Content .ContentCell').removeClass('expanded');
						jQuery('#Content .ContentLayer').removeClass('expanded');
						closeCustomPopups();
						showPopupLinks();
					  });
	
	window.customPopups['popupCatalog'] = new dc_Popup(gsaLink, {
															popupIframeName: 'popupCatalog',
															popupWidth: 893, 
															popupHeight: 490, 
															popupBackground: "#FFFFFF",
															popupLeft:  '0px', 
															popupTop: '58px',
															//popupLeft:  ContentSection.offset().left + 'px', 
															//popupTop: ContentSection.offset().top + 58 + 'px',
															showPopupMask: false,
															showCloseButton: false
															});
}

// closes all custom popups
function closeCustomPopups()
{
	if (window.customPopups)
	{
		for (var i in window.customPopups)
			try
				{window.customPopups[i].off();}
			catch(err){}
	}
}

function rotateLeftInfoBubble()
{
	jQuery('#itemlist')
		.cycle({ 
			fx:     'scrollDown',
			pause: 1, 
			timeout: 12000, 
			delay:  -1000
		});
}

function rotateInfoBox()
{
	jQuery('#BottomInfoBoxScroller ul')
		.cycle({ 
			fx:     'scrollDown',
			pause: 1, 
			timeout: 8000, 
			delay:  -1000
		})	
}

function disableMainNavLinks()
{
	jQuery('#Content h2 a.navLink').click(function(){return false})
}

function getDefaultSelection()
{
	var avSelections = new Array();
	
	var pageUrl = new String(window.location.href);
	var filter = /\?s=[a-zA-Z0-9_:\/\?=\.]+/;
	var filterMatch = new String(pageUrl.match(filter));
	if (filterMatch != 'null')
	{
		var pageName = new String(filterMatch.substring(3));
		if (!pageName.match('http'))
			pageName = pageName + '.html';
		return pageName
	}
	return null
}

function loadDefaultSelection(pageUrl)
{
	window.customPopups['defaultPopup'] = jQuery.dc_PopupIframe({
																popupIframeName: 'defaultPopup',
																popupParentNode: jQuery('#Content'),
																popupWidth: 893, 
																popupHeight: 492,
																popupBackground: "#FFFFFF",
																popupLeft:  '0px', 
																popupTop: '58px',
																showPopupMask: false,
																showCloseButton: false,
																popupDefaultSrc: pageUrl
															});
	window.customPopups['defaultPopup']
				.load(function()
						{
						  	jQuery(this).css('visibility', 'visible');
							
						})
	showPopupLinks();
	var defWaitMessage = null;
}

function showPopupLinks()
{
	jQuery('a#HomeButton')
		.add('a#LinkToPageButton')
		.css('visibility', 'visible');	
		
	jQuery('a#LinkToPageButton')
		.unbind('click')
		.click(function()
				{
					var lnk = jQuery(this);
					var contentFrame = jQuery('#Content iframe:last');
					
					var contentFrameUrl = new String(contentFrame.attr('src'));
					
					var pageUrl = new String(window.location.href);
					var filter = /[^\?]+/;
					
					var filterMatch = new String(pageUrl.match(filter));
					var directUrl = filterMatch + '?s=' + contentFrameUrl.replace(/\.html/, '');
					
					var urlPopup = jQuery('#PageUrlPopup');
					urlPopup.find('a.PageUrlPopupCloseBtn')
							.click(function()
									{
										jQuery('#PageUrlPopup').css('visibility', 'hidden');
										return false;
									})
					jQuery('#PageUrlPopup .txtPageDirectUrl')
						.val(directUrl)
						.select()
						.click(function(){this.select()});
					urlPopup.css('visibility', 'visible');					
					return false;
				})
}


jQuery.extend(window, {customPopups: {}});

jQuery(function(){
	if (isAdmin())
		setAdminInterface();
	else
	{
		disableMainNavLinks();
		resizeWrappers();
		initTIpopups();
		TIslideshow = new Slideshow();
			
		var defaultSelection = getDefaultSelection();
		if (defaultSelection != null)
		{
			TIslideshow.init('stopped');	
			TIslideshow.terminate();	
			loadDefaultSelection(defaultSelection);
		}
		else
		{
			TIslideshow.init();
		}	
		setTimeout(rotateInfoBox, 4000);
		setTimeout(rotateLeftInfoBubble, 4000);
		
	}
})