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

function resizeImg() {
	var bgImg=jQuery('#bgImage img').each(function() {
		//jQuery(this).removeAttr('width').removeAttr('height');
		var imgwidth = jQuery(this).width();
		var imgheight = jQuery(this).height();
		var winwidth = jQuery(window).width()+100;
		var winheight = jQuery(window).height()+100;
		
		var widthratio = winwidth / imgwidth;
		var heightratio = winheight / imgheight;
		var widthdiff = heightratio * imgwidth;
		var heightdiff = widthratio * imgheight;
		var topPos = -(heightdiff-winheight) /2;
		var leftPos = -(widthdiff-winwidth) /2;
		if(heightdiff>winheight) {
			jQuery(this).css({
				width: winwidth+'px',
				height: heightdiff+'px',
				left:"0",
				top:topPos+"px"
			});//.attr('width',winwidth).attr('height', heightdiff);
			//alert('Resizing Images: \nImageWidth: '+winwidth+' - Image Height: '+heightdiff+'\nWindow Width: '+winwidth+' - Window Height: '+winheight);
		}
		else {
			jQuery(this).css({
				width: widthdiff+'px',
				height: winheight+'px',
				left:leftPos+"px",
				top:"0"
			});//.attr('width',widthdiff).attr('height', winheight);
			//alert('Resizing Images: \nImageWidth: '+widthdiff+' - Image Height: '+winheight+'\nWindow Width: '+winwidth+' - Window Height: '+winheight);
		}
	});
	
}

function getRGBbg(element) {
	return jQuery(element).css('background-color');
}

function fixIEAlpha(element,alpha) {
	//var theDiv=jQuery(element).attr('id');
	var theBGColor=jQuery(element).css('background-color');
	if (theBGColor=='transparent' && (element!='#mainContent')) {
		return;
		}
	if(jQuery(element).find('div.elementWrapper').length>0) {
		return;
	}
	else {
	if (jQuery(element).children().length>1) {
		jQuery(element).children().wrapAll('<div class="elementWrapper">');
	}
	else {
		jQuery(element).children().wrapAll('<div class="elementWrapper">');
		//jQuery(element).children().first().addClass('elementWrapper');
	}
	//document.getElementById('content').className='elementWrapper';
	jQuery(element).append('<div class="IEFixBG">');
	
	//alert('The Background Color of the container is: '+theBGColor);
	//jQuery(element).css('position', 'relative');
	
	jQuery('div.IEFixBG').css({
		opacity: alpha
	});
	//jQuery('div.IEFixBG').css('background-color', theBGColor);
	if (element=='#mainContent') {
		var theElHeight=jQuery(element).innerHeight();
		var theElWidth=jQuery(element).innerWidth();
		jQuery(element).find('div.IEFixBG').css({
			//'height':theElHeight+"px",
			//'width': theElWidth+"px",
			top: '4px',
			left: '4px',
			bottom: '98px',
			right: '4px',
			width: 'auto',
			height: 'auto'
		});
		}
	
	jQuery(element).css('background-color', 'transparent');
	
	}
	return;
}


function addBGAlpha(element,alpha) {
	if ( jQuery.browser.msie && (parseInt(jQuery.browser.version,10))<9 ) {
		fixIEAlpha(element,alpha);
	}
	else {
		var rgbVal=getRGBbg(element);
		rgbVal=rgbVal.replace(/\s/g, "");
		var rgbaVal=rgbVal.substring(0,rgbVal.length-1)+','+alpha+')';
		rgbaVal=rgbaVal.replace('rgb', 'rgba');
		jQuery(element).css('background-color', rgbaVal);
	}
	return;
	
}

function initSB() {
	
	Shadowbox.init({
		//players: ["html","img","swf","flv","qt","iframe"]
		skipSetup: true
	});
	
	
}

function setUpSB() {
	Shadowbox.clearCache();
	jQuery('#content a[rel^="shadowbox"]').each(function() {
		Shadowbox.addCache(jQuery(this)[0]);
		
		
	});
}

function setUpPhotoAlbumSB() {
	
	Shadowbox.clearCache();
	jQuery('div.thumbnail-frame a').each(function() {
		var theOURL=jQuery(this).attr('href');
		var theNURL=theOURL.replace(/\.html$/gi, '.jpg');
		var theTitle=jQuery(this).next('p.thumbnail-caption').text();
		jQuery(this).attr({
			'href': theNURL,
			'rel': 'shadowbox[photoSet]',
			'title': theTitle
		});
	});
	Shadowbox.setup('div.thumbnail-frame a', {
			player: 'img'
			});
	
}



function loadOnFirst() {
	
	jQuery('#bgImage').css('display', 'none');
	updateSidebar();
	addToControlBox();
	addToControlBoxBottom();
	addToControlToolbar();
	initSB();
	setUpAccordion();
	//setAlpha();
	
	/*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 && ! iPad) {autoHideBox()}	
	setUpPhotoAlbumSB();
	
	
	/*The Event Handlers*/
	
	if (! iPhone && ! iPad && jQuery('#control').css('width')=='auto') {
		jQuery(window).resize(sizeControlBox);
		}
	
	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 (jQuery('div.thumbnail-frame a').length>0) {
						
						setUpPhotoAlbumSB();
					}
		else if (jQuery('#content a[rel^="shadowbox"]').length>0) {
						setUpSB();
					}
		else if (relTag!='external' || !(/\.pdf$/.test(theURL))){
			if (ajaxMode) {
				loadPage(theURL);
				e.preventDefault();
				return false;}
			}
			else {
				return true;
				}
		return true;

	});
	
	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 if((/pdf$/.test(theURL))) {
		//window.open(theURL, '_blank');
		return false;
	}
	
	else {
		Shadowbox.open({
			content: theURL,
			player: "iframe"
		});
	}
	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" allowtransparency="true" style="width: 100%; border: 0;background-color: transparent" />');
			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();
				setAlpha();
				if (jQuery('div.thumbnail-frame a').length>0) {
						
					setUpPhotoAlbumSB();
				}
				else if (jQuery('#content a[rel^="shadowbox"]').length>0) {
					setUpSB();
				}
				
				jQuery('#content a').each(function() {
					var theLink=jQuery(this).attr('href');
					if (jQuery(this).attr('rel')=='external' || /\.pdf$/.test(theLink)) {
						jQuery(this).attr('target', '_blank');
						jQuery(this).click(function() {
							window.open(theLink, '_blank');
							return true;
						});
					}
					else {
					}
				});
				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 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 img').removeAttr('width').removeAttr('height');
		resizeImg();
		jQuery('#bgImage #background').cycle({fx:'fade', speed:2000, timeout: 20000, containerResize: 0});
		}
	
	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'});
	}
	else {
		resizeImg();
	}
	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;


}

var ajaxMode=checkAjax();
jQuery(function() {
	if (ajaxMode) {
		jQuery.history.init(function(url) {
			loadPage(url == '' ? 'index.html' : url);
		});
	}
	sizeControlBox();
	//setAlpha();
	var interval=window.setInterval("resizeImg()",500);
	if(iPad) {
		var theFullWidth=jQuery('#control').outerWidth()+jQuery('#container').outerWidth()+60+'px';
		jQuery('body').css('width', theFullWidth);
		}
	
	if (iPad) {
	window.addEventListener('orientationchange', function () {
		//alert('Orientation Changed');
		var theFullWidth=jQuery('#control').outerWidth()+jQuery('#container').outerWidth()+60+'px';
		jQuery('body').css('width', theFullWidth);
		//jQuery('meta[name="viewport"]').attr('content', 'width=' + theFullWidth + ', user-scalable:yes');
		});
		}
	
	loadOnFirst();
});

jQuery(window).resize(function() {
	resizeImg();
	sizeControlBox();
});


/*jQuery(window).load(function() {
	if (window.PIE) {
        jQuery('#mainContent').add('#footer').add('#pageHeader').each(function() {
            PIE.attach(this);
        });
    }
});*/


