// JavaScript Document

var bClosedByUser = 0;
$(function(){
	$.fn.supersized.options = {  
		startwidth: 640,  
		startheight: 480,
		vertical_center: 1,
		slideshow: 1,
		navigation: 1,
		transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
		pause_hover: 0,
		slide_counter: 1,
		slide_captions: 1,
		slide_interval: 8000  
	};
	$('#supersize').supersized(); 
	$(".menuLink").bind("click", function(event){
		return changeContent(event);
	});
	$(".sideBar a").bind("click", function(event){
		return changeContent(event);
	});
	$(".closeWindow").bind("click", function(event){
		return useParentChangeContent(event);
	});
	$('.newWindow').unbind('click');
});


function useParentChangeContent(eventTrigger){
	parent.changeContent(eventTrigger);
	return false;
}

	
function changeContent(objTrigger){
	//hide iframe
	document.getElementById('loading').style.display = '';
	//highlight selected
	objMenuTable = document.getElementById('menuTable');
	
	arrMenuLinks = objMenuTable.getElementsByTagName('a');
	
	// make sure the trigger was from the menuTable and not subMenu before clearing
	clearSelection = 0;
	for(i=0; i < arrMenuLinks.length; i++){
		if(objTrigger.target.id == arrMenuLinks[i].id){
			clearSelection = 1;
		}
	}
	
	if (clearSelection || objTrigger.target.id == 'logo'){
		for(i=0; i < arrMenuLinks.length; i++){
			$(arrMenuLinks[i]).removeClass('menuLinkSelected').addClass('menuLink');
		}
	}
	if (objTrigger.target.id != 'logo'){
		$(objTrigger.target).removeClass('menuLink').addClass('menuLinkSelected');
	}
	// 400
	$(".subMenu").hide(10);
	//50
	$(".mainContent").hide(10, function (){
		objIframe = document.getElementById('frameContent');
		objIframe.src = objTrigger.currentTarget;
	  });

	
	return false;
}

// this function is called from the child (iFrame) onload function.
function configSubMenu(){
	//300
	$(".mainContent").show(100);
	document.getElementById('loading').style.display = 'none';
	//400
	$(".subMenu").hide(100);
	var iframeEl = document.getElementById('frameContent');
	if ( iframeEl.contentDocument ) { // DOM
		var childSubMenu = iframeEl.contentDocument.getElementById('subMenuContent');
		var childHeader = iframeEl.contentDocument.getElementById('pageHeader');
		var mainMenu = iframeEl.contentDocument.getElementById('mainMenu');
		
	} else if ( iframeEl.contentWindow ) { // IE win
		var childSubMenu = iframeEl.contentWindow.document.getElementById('subMenuContent');
		var childHeader = iframeEl.contentWindow.document.getElementById('pageHeader');
		var mainMenu = iframeEl.contentWindow.document.getElementById('mainMenu');
	}
	
	// config sidebar SIGN UP based on what page is currently being shown
	objSideBarSignUp_standard = document.getElementById('enewsletter1');
	objSideBarSignUp_implants = document.getElementById('enewsletter2');
	objSideBarSignUp_invisalign = document.getElementById('enewsletter3');
	if(iframeEl.src.indexOf("dental-implants.php") > 1){
		objSideBarSignUp_standard.style.display = 'none';	
		objSideBarSignUp_implants.style.display = '';
		objSideBarSignUp_invisalign.style.display = 'none';
		//alert('is implant');
	}else if(iframeEl.src.indexOf("invisalign.php") > 1){
		objSideBarSignUp_standard.style.display = 'none';	
		objSideBarSignUp_implants.style.display = 'none';
		objSideBarSignUp_invisalign.style.display = '';
		//alert('is implant');
	}else{
		objSideBarSignUp_standard.style.display = '';	
		objSideBarSignUp_implants.style.display = 'none';
		objSideBarSignUp_invisalign.style.display = 'none';
	}
	
	mainMenu.style.display = 'none';
	
	newContent = childSubMenu.innerHTML;
	if(jQuery.trim(newContent) != ''){
		// 400
		$(".subMenu").show(1500, function (){windowResized();});
		document.getElementById('subMenu').innerHTML = newContent;
		childSubMenu.style.display = 'none';
		// attach JQUERY functions to submenu
		$(".subMenu .menuLink").bind("click", function(event){
			return changeContent(event);
		});
		
	}
	newHeader = childHeader.innerHTML;
	childHeader.style.display = 'none';
	document.getElementById('headertext').innerHTML = newHeader;
	
}

function windowResized(){
	//alert('talking');
	objIFrame = document.getElementById('mainContent');
	objSideBar = document.getElementById('sideBar');
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	if (myWidth < 900){
		objSideBar.style.display = 'none';
	}
	if (myWidth > 900 && bClosedByUser == 0){
		objSideBar.style.display = '';
	}
	newSidePadding = Math.ceil(myWidth * 0.03);
	objIFrame.style.left = newSidePadding + 'px';
	// the + 20 at the end of the next line used to be - 20 but I changed this when swam changed the width of the side menu.
	newFrameWidth = (myWidth - (newSidePadding * 2)) - 20;
	if (objSideBar.style.display != 'none'){
		newFrameWidth = newFrameWidth - 240;
		newSubMenuWidth = myWidth - 260 - 140;
	}else{
		newFrameWidth = newFrameWidth - 25;
		newSubMenuWidth = myWidth - 35 - 140;
	}
	objIFrame.style.width = newFrameWidth + 'px';
	
	newTopPadding = Math.ceil(myHeight * 0.03);
	objIFrame.style.top = newTopPadding + 'px';
	newHeight = myHeight - newTopPadding - 150;
	objIFrame.style.height = newHeight + 'px';
	
	if (newSubMenuWidth < 736){
		newSubMenuWidth = 736;
	}
	document.getElementById('subMenu').style.width = newSubMenuWidth + 'px';
}

function userCloseMenu(){
	bClosedByUser = 1;
	closeSideMenu();
}
function closeSideMenu(){
	document.getElementById('sideBar').style.display = 'none';
	//$("#sideBar").hide(1000, function () {windowResized();});
	windowResized();
}

function userOpenMenu(){
	bClosedByUser = 0;
	openSideMenu();
}
function openSideMenu(){
	document.getElementById('sideBar').style.display = '';
	//$("#sideBar").show(1000,function () {windowResized();});
	windowResized();
}

function closeMainWindow(){
	// 100
	$(".mainContent").hide(30);
}
