$(document).ready(function() {
						   
	// Tenant Form Setup
	if($('#tenant_services').length > 0){
		$('#head').append('<p id="tenantLink"><a href="tenant">Tenant Service Request</a></p>');
		$('#tenantLink').click(function(){
			$('#tenant_services').animate({top: '-1px'},{duration: 'slow'});	
			return false;
		});
		var randomNumber=Math.floor(Math.random()*11);
		$('#tenant_services').load('/data/tenant-services.php?id='+randomNumber, function(){initializeForm();});
	}
						   
	// preload CSS images
	$.preloadCssImages();

    // rollovers
    PEPS.rollover.init();

    // PNG tranparency for IE6
    	$('.png').pngfix();
	
	// Current Links
	$('.current').click(function(){
		return false;
	});
	
	// external links
    $("a[@rel='external']").click(function() {
        window.open($(this).attr('href'));
        return false;
    });
	
	// Fade in Site Map
	if($('#site_map').length > 0){
		$('#SitePlan').find('.right540').hide();
		$('#SitePlan').find('.right540').fadeIn(2000);
	}	

    // set div links
    $(".link, .acrobat_link").click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
    });
	
	// Start Intro on Index Page
	
	$('#intro, #tagline, #enter').css('display', 'none');
	
	
	if ($('#trees').length > 0){
		startIntro();
	}
	if($('#nav').length > 0) {
		$('#nav').prepend('<p><a id="prev">Prev</a></p>');	
		$('#nav').append('<p><a id="next">Next</a></p>');
	}
	
	// Phase Map for Site Plan Panel
	if($('#site_map').length > 0){
		$('a','#phase_nav').click(showOverlay);
	}
	
	// Remove Hash from Subnav
	
	$('a','.subnav').each(function(){
		var i = $(this).attr('href');
		var i = i.replace('#','');
		$(this).attr('href', i);
	});
	
	
	// PDF Links
	$('#site_map, #floor_plan1, #floor_plan2, #building1_img, #building2_img, #office_img_1, #office_img_2, #office_img_3, #location_img').hover(function(){
			if($.browser.msie){
				$('.acrobat_link', this).show();
			}else{
				$('.acrobat_link', this).fadeIn('normal');
			}
		}, function(){
			if($.browser.msie){
				$('.acrobat_link', this).hide();
			}else{
				$('.acrobat_link', this).fadeOut('normal');
			}
		});
	
	

	// SiteMap Subnav
	
	if($('#siteplan_tabs').length > 0){
			$('.subpanel','#SitePlan').hide();
			$('#SitePlan1').show();
			$('a', '#siteplan_tabs').click(function(){
				if(!$(this).hasClass('current')){
					$('a', '#siteplan_tabs').removeClass('current');
					var toShow = '#' + $(this).attr('href');
					$('.subpanel','#SitePlan').hide();
					$(toShow).show();
					$(this).addClass('current');
				}
				return false;
			});
	}
	
	// Floor Plans Subnav
	
	if($('#siteplans_tabs').length > 0){
			$('.subpanel','#FloorPlans').hide();
			$('#OneStoryPlan').show();
			$('a', '#siteplans_tabs').click(function(){
				if(!$(this).hasClass('current')){
					$('a', '#siteplans_tabs').removeClass('current');
					var toShow = '#' + $(this).attr('href');
					$('.subpanel','#FloorPlans').hide();
					$(toShow).show();
					$(this).addClass('current');
				}
				return false;
			});
	}
	
	// Pictures Slideshow
	$('#picturesSlideshow').cycle({ 
    fx:     'scrollHorz',
	speed: 600,
    prev:   '#picturesNavPrev', 
    next:   '#picturesNavNext',
	nowrap: 1,
	after: onAfter,
    timeout: 0 
	});
	
	
	
});

function showOverlay(){
	var whichMap = '#' + $(this).attr('href');
	$('#phase_nav').find('a').removeClass('current');
	$(this).addClass('current');
	if(whichMap == '#site_map'){
		$('div', '#site_map').hide();	
	}else{
		$('div', '#site_map').hide();
		$(whichMap).fadeIn(600);
	}
	return false;
}



function startIntro(){
	$(document).oneTime(1000, 'introTimer', function(){
		$('#trees').fadeOut(5000, function(){
				$('#intro').fadeIn(2000, function(){
					$('#tagline').fadeIn(1000, function(){
						$('#enter').fadeIn(1000);
					});
				});							  
		});
	});
}

function initializeForm(){
	var tenantFormOptions = { 
        target:        '#tenant_services', 
        //beforeSubmit:  showRequest, 
        success:      processResponse 
	};
	$('#tenant_form').submit(function() { 
    	$(this).ajaxSubmit(tenantFormOptions); 
        return false; 
   	});
	$('#cancel_tenant_request').click(function(){
		$('#tenant_services').animate({top: '-283px'},{duration: 'slow'});
		$('#tenant_services').clearForm();
		$('.error','#tenant_services').removeClass('error');
		return false;
	});
}
function processResponse(responseText) {
	var responseKey = responseText.substring(1,2);
	if(responseKey == 'h'){
		setTimeout('tenantFormRecall()', 3000);
	}else{
		initializeForm();
	}
}
function tenantFormRecall() {
	$('#tenant_services').fadeOut(1600);
	setTimeout('resetTenantForm()', 1800);
}
function resetTenantForm(){
	$('#tenant_services').css({top: '-283px'});
	$('#tenant_services').show();
	var randomNumber=Math.floor(Math.random()*11);
	$('#tenant_services').load('/data/tenant-services.php?id='+randomNumber, function(){initializeForm();});	
}

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('#picturesNavPrev')[index == 0 ? 'hide' : 'show']();
    $('#picturesNavNext')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}