var ajaxMode=checkAjax();
jQuery(function() {
		if (ajaxMode) {
			jQuery.history.init(function(url) {
				loadPage(url == '' ? 'index.html' : url);
			});
			}
			loadOnFirst();
			
			

    return;
});
var iPhone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i));



function loadOnFirst() {
	
	jQuery('#bgImage').css('display', 'none');
	updateSidebar();
	addToControlBox();
	addToControlBoxBottom();
	addToControlToolbar();
	//sizeControlBox();
	setUpAccordion();
	
	/*Layout and Design*/
	jQuery('#control').css('position', 'fixed');
	jQuery('#bgImage').css('position', 'fixed');
	setNavbarTitle();
	setBackground();
	setUpSlides();
	if (! iPhone) {createBox()}
	
	jQuery("#control").tabs().draggable({opacity: 0.4});
	if (! iPhone) {jQuery("#box").tabs().draggable({opacity: 0.4})};
	//if (iPhone) {createIPhoneClose();}
	if (iPhone) {iPhoneBar();}
	
	/*Functions to Load on Page Load*/
	
	if (! iPhone) {autoHideBox()}	
	if (! iPhone) {setUpPhotoAlbumSB()}
	
	
	/*The Event Handlers*/
	
	//if (! iPhone) {jQuery(window).resize(sizeControlBox)}
	
	if (! iPhone) {jQuery('input[name=autoHide]').click(autoHideBox)}
	if (ajaxMode) {	
	jQuery('#mainNav a').not('#mainNav>ul>li>a').click(function(e) {
		jQuery('#mainNav a').not(jQuery(this)).removeClass('current', 400);
		jQuery(this).addClass('current', 400);
		jQuery('#mainContent').hide();
		theURL=jQuery(this).attr('href');
		jQuery.history.load(theURL);
		e.preventDefault();
		return false;
	});
	}
	
	if (!iPhone) {jQuery('#mainNav>ul>li>a').next().css('display', 'none');}
	
	jQuery('#mainNav>ul>li>a').click(function(e) {
		theURL=jQuery(this).attr('href');
		jQuery('#mainNav a').not(jQuery(this)).removeClass('current', 400);
		jQuery(this).addClass('current', 400);
		jQuery('#mainNav>ul>li>a').not(jQuery(this)).nextAll().hide(400);
		if (jQuery(this).next().length>0) {
			jQuery(this).next().show(200);
			
			if (ajaxMode) {jQuery.history.load(theURL);}
		}
		else {
			if (ajaxMode) {jQuery.history.load(theURL);}
		}
		if (ajaxMode) {
			return false;
		}
		else { return;}
	});
		
	jQuery('#content a').live('click', function(e) {
		theURL=jQuery(this).attr('href');
		var relTag=jQuery(this).attr('rel');
		if ((/shadowbox/gi.test(relTag)) || (/lightbox/gi.test(relTag))) {
			openInSB(theURL);
			e.preventDefault();
			return false;
		}
		else if (relTag!='external'){
			if (ajaxMode) {loadPage(theURL);
			e.preventDefault();
			return false;}
		}
		return;

	});
	
	jQuery('div.ui-widget-content a').not('#mainNav a').live('click', function(e) {
		theURL=jQuery(this).attr('href');
		var relTag=jQuery(this).attr('rel');
		if ((/shadowbox/gi.test(relTag)) || (/lightbox/gi.test(relTag))) {
			openInSB(theURL);
			e.preventDefault();
			return false;
		}
		else if (relTag!='external'){
			if (ajaxMode) {loadPage(theURL);
			e.preventDefault();
			return false;}
		}
		return;

	});
	
	jQuery("#content h2").live('click', function() {
		jQuery('#content h2').each(function() {
			jQuery(this).next().hide(600);
		});
		if (jQuery(this).next().css('display') == 'none') {
  				jQuery(this).next().show(600);
		}
		else {
  				jQuery(this).next().hide(600);
		}
	});
	
	jQuery("#content h3.acToggle").live('click', function() {
		jQuery('#content h3.acToggle').each(function() {
			jQuery(this).next().hide(600);
		});
		if (jQuery(this).next().css('display') == 'none') {
  				jQuery(this).next().show(600);
		}
		else {
  				jQuery(this).next().hide(600);
		}
	});
	
	
	Shadowbox.init({
    				players: ["html","img","swf","flv","qt","iframe"]
	});
	return;
}
  			
function autoHideBox() {
	if (jQuery('input[name=autoHide]:checked').length>0){
		jQuery('#control').hover(
			function(){
				jQuery(this).stop().animate({opacity:0.7}, 1000);
			},
			function() {
				jQuery(this).stop().animate({opacity:0.65}, 5000).stop().animate({opacity:0}, 3000);
		});
		}
		else {
			jQuery('#control').unbind('mouseenter').unbind('mouseleave');
		}
	return;
}

function openInSB(theURL, htmlText) {
	if ((/gif$/.test(theURL)) || (/jpg$/.test(theURL)) || (/png$/.test(theURL)) || (/jpeg$/.test(theURL))) {
		Shadowbox.open({
		content: theURL,
		player: "img"
	});
}
	else if ((/flv$/.test(theURL))) {
		Shadowbox.open({
			content: theURL,
			player: "flv"
		});
		}
	else if((/swf$/.test(theURL))) {
		Shadowbox.open({
			content: theURL,
			player: "swf"
		});
	}
	
	else {
		Shadowbox.open({
			content: theURL,
			player: "iframe"
		});
	}
	return;
}

function setUpPhotoAlbumSB() {
	jQuery('.thumbnail-frame a').each(function(e) {
		var oURL=jQuery(this).attr('href');
		var iURL=oURL.replace(/html$/gi, 'jpg');
		jQuery(this).attr('href', iURL);
		jQuery(this).attr('rel', 'shadowbox[photoSet]');
		
	});
	return;
}

function loadPage(theURL) {
    //theURL = theURL.replace(/^.*#/, '');
	//var extension=(theURL.substr(theURL.length-3, theURL.length) == "php")?'php':'html';
	//theURL=theURL+'.'+extension;
	jQuery.get(theURL, function(htmlText) {
		if (jQuery(htmlText).find('div#loadiFrame').length>0) {
			jQuery('#mainContent #content').empty().html('<iframe src="'+theURL+'"id="iframeWrapper" frameborder="0" style="width: 100%; border: 0" />');
			jQuery('#content iframe').load(function() {
				this.height=this.contentWindow.document.body.offsetHeight + 50 + 'px';
			});
			jQuery('#mainContent').fadeIn();
		}
		else if (jQuery(htmlText).find('div#content').length>0) {
			jQuery('#mainContent').load(theURL+" #content", function() {
				jQuery('#control #iPhoneClose').trigger('click');
				if(iPhone) {iPhoneBar();}
				setUpAccordion();
				updateSidebar();
				jQuery('#mainContent').fadeIn();
				fixLinks(theURL);
				setBackground()
				setUpPhotoAlbumSB();
				addToControlBox();
				addToControlBoxBottom();
				addToControlToolbar();
				setUpSlides();
				});
		}
		else {
			openInSB(theURL, htmlText);
		}
		//setUpPhotoAlbumSB();
	});

	return;
}

function loadPageNoAJAX () {
	return;
}

function fixLinks(theURL) {
	if (ajaxMode) {
	if (/http/.test(theURL)) {return;}
	var theSitePath=getSitePath();
	jQuery('#content a').each(function() {
		var oURL=jQuery(this).attr('href');
		oURL=oURL.replace(theSitePath, '');
		if (!(/http/.test(oURL))) {
			var nURL=theURL+"/../"+oURL;
			jQuery(this).attr('href', nURL);
		}
	});
	jQuery('#content img').each(function() {
		
		
		var oIURL=jQuery(this).attr('src');
		oIURL=oIURL.replace(theSitePath, '');
		var nIURL=theURL+"/../"+oIURL;
		jQuery(this).attr('src', nIURL);
	});
	}
	return;
}

function addToControlBox() {
	jQuery('#content .cbox').each(function(i) {
		var cBoxTitle=jQuery(this).find('.cboxTitle').html();
		var gotContent=((/\w+/.test(cBoxTitle))&&(/\w+/.test(cBoxContent)));
		if(!gotContent) {return;}
		var cBoxContent=jQuery(this).find('.cboxContent').html();
		jQuery(this).detach();
		if (jQuery('#control li a[href^=#cb]').html()!=cBoxTitle && (jQuery('#control #cb'+i).length==0)) {
			jQuery('#control>ul').append('<li><a href="#cb'+i+'">'+cBoxTitle+'</a></li>');
			jQuery('#control div.toolbarBottom').before('<div id="cb'+i+'">'+cBoxContent+'</div>');
		}
	});
	return;
}

function updateSidebar() {
		var theSBTitle=jQuery('#content #theSidebar #sidebarTitle').html();
		var gotTitle=/\w+/.test(theSBTitle);
		
		if(!gotTitle) {
			jQuery('#control #theSBLI').css('display', 'none');
			return;
			}
		if(jQuery('#control #theSBLI').length==0) {createSidebar();}
		
		var theSBContent=jQuery('#content #theSidebar #sidebarContent').html();
		jQuery('#content #theSidebar').detach();
		if (jQuery('#control li a[href=#sidebar]').text()!=theSBTitle) {
			jQuery('#control #theSBLI a').html(theSBTitle);
			jQuery('#control #sidebar').html(theSBContent);
			
		}
		jQuery('#control #theSBLI').css('display', 'block');
	return;
}

function createSidebar() {
	jQuery('#control>ul').append('<li id="theSBLI"><a href="#sidebar"></a></li>');
	jQuery('#control div.toolbarBottom').before('<div id="sidebar"></div>');
	jQuery('#control #theSBLI').css('display', 'block');
	return;
}

function addToControlBoxBottom() {
	jQuery('#content .cboxbottom').each(function(i) {
		var cBoxBContent=jQuery(this).html();
		var theID=jQuery(this).children().first().attr('id');
		jQuery(this).detach();
		var found=0;
		jQuery('#control div.boxBottom>div').each(function() {
			if (jQuery(this).attr('id')==theID) {
				found=1;
				}
		});
		if(!found) {
			jQuery('#control div.toolbarBottom').before('<div class="boxBottom">'+cBoxBContent+'</div>');
		}
	});
	return;
}

function addToControlToolbar() {
	jQuery('.tbar').each(function(i) {
		var tbarContent=jQuery(this).html();
		var theID=jQuery(this).children().first().attr('id');
		jQuery(this).detach();
		var found=0;
		jQuery('#control div.toolbarBottom>div').each(function() {
			if (jQuery(this).attr('id')==theID) {
				found=1;
				}
		});
		if(!found) {
			jQuery('#control div.toolbarBottom').append(tbarContent);
		}
	});
	return;
}

function sizeControlBox() {
	theSpace=(jQuery('#pageHeader').offset().left)-40;
	jQuery('#control').css('maxWidth', theSpace);
	return;
}

function createBox() {
	if (jQuery('#content .box').length==0) {
		return;
	}
	var theBoxWrapper=jQuery('<div id="box"><ul></ul></div>');
	theBoxWrapper.insertAfter('#control');
	jQuery('#box').css('position', 'fixed');
	jQuery('#content .box').each(function(i) {
		var boxTitle=jQuery(this).find('.boxTitle').html();
		var boxContent=jQuery(this).find('.boxContent').html();
		jQuery(this).detach();
		if (jQuery('#box li a[href^=#b]').text()!=boxTitle) {
			jQuery('#box>ul').append('<li><a href="#b'+i+'">'+boxTitle+'</a></li>');
			jQuery('#box').append('<div id="b'+i+'">'+boxContent+'</div>');
		}
	});
	return;
}


function setUpAccordion() {
		jQuery('#content h2').each(function() {
			if (!(jQuery(this).hasClass('showOnInit'))) {
				jQuery(this).next().hide();
			}
		});
		return;
}

function getSitePath() {
	var theFullURL=location.href;
		var theFullURLArray=theFullURL.split('/');
		theFullURLArray.pop();
		var theSitePath=theFullURLArray.join('/');
		theSitePath+='/';
		return theSitePath;
}

function setBackground () {
	if (jQuery('#content #background').length==0) {
		return;
	}
	jQuery('#bgImage').empty().append(jQuery('#content #background'));
	
	if (jQuery('#bgImage img').length>1) {
		jQuery('#bgImage #background').cycle({fx:'fade', speed:1000, timeout: 20000});
	}
	
	if (jQuery('#background').hasClass('noresize')) {
		var imgWidth=jQuery('#background img').attr('width');
		jQuery('#background').css({position: 'absolute', width: imgWidth, left: '50%', marginLeft: -imgWidth/2, height: 'auto'});
		jQuery('#background img').css({width: imgWidth, height: 'auto'});
		jQuery('#bgImage').css({width: imgWidth, marginLeft: -imgWidth/2, left: '50%', position: 'fixed'});
	}
	jQuery('#bgImage img').load(function() {
		if (! iPhone) {
		jQuery('#bgImage').fadeIn(1000);
		}
	});
	return;
}

function setNavbarTitle () {
	if (typeof navbarTitle !='undefined') {
		jQuery('#control ul li:first a').html(navbarTitle);
	}
	return;
}

function createIPhoneClose() {
	jQuery('#control .ui-tabs-nav').append('<div id="iPhoneClose"></div>');
	jQuery('#control #iPhoneClose').click(function(e) {
		jQuery('#control>div').css({
		'height': '0',
		overflow: 'hidden', 
		padding: '0'
		});
		jQuery('#control').css({
			'minHeight': 0
		});
		jQuery('#control #iPhoneClose').detach();
	});
	return;
	
}

function iPhoneBar() {
	window.scrollTo(0,1);
	jQuery('#control>div').css({
		'height': '0',
		overflow: 'hidden', 
		padding: '0'
		});
	jQuery('#control ul.ui-tabs-nav li a').click(function (e) {
		jQuery('#control>div').css({
			height: 'auto',
			overflow: 'visible'
		});
		jQuery('#control').css({
			'minHeight': '140%',
			height: 'auto'
			});
			jQuery('#control #iPhoneClose').detach();
			createIPhoneClose();
	});
	return;
}

function setUpSlides() {
	jQuery('div.slides').each(function(i) {
		jQuery(this).css('display', 'none');
		if (typeof slideWait !='undefined') {
			sWait=slideWait;
		}
		else {
			sWait=5000;
		}
		if (typeof slideDur !='undefined') {
			sDur=slideDur;
		}
		else {
			sDur=500;
		}
		if (typeof slideFX !='undefined') {
			sFX=slideFX;
		}
		else {
			sFX='fade';
		}
		
		jQuery(this).fadeIn();
		jQuery(this).cycle({fx: sFX, speed: sDur, timeout: sWait, pause: 1});
		
	});
	return;


}
