/*
Custom functions 
*/

function setup(){

/* Set ID of DIV CLASS="shell" */
var pageTempate=$(".nav").find(".selected").text().replace(/ /g,'').toLowerCase();
$(".shell").attr("id",pageTempate+"_template");

// Add buildings container to .nav
$('.nav').append('<div class="buildings"/>');

// Add background container to .container
$('.container .inner-wrapper').not('#home_template .container .inner-wrapper, .EditMode .container .inner-wrapper').wrap('<div class="outer-wrapper"/>');

// News ticker scroll
$("#news-ticker").scrollable({
	speed:500,
	circular: true,
	mousewheel: true
	});

// Homepage background slideshow
    var transition_speed = 1000,
    	transition_interval = 10000, 
		bImgNum = $('.background img').length,
		random = Math.floor(Math.random()*bImgNum);

if (bImgNum > 1) {
	$('.background img').css({'display':'block','left':'0','position':'absolute','top':'0'}).fadeOut(0);
	$('.background img').eq(random).addClass('active').fadeIn(transition_speed);

	setInterval(function(){ imgAnimate(); }, transition_interval);  
	
	function imgAnimate(){
		random = random + 1;
		var thisImg = $('.background .active'),
			nextImg = $('.background .active').next();
		if (random == bImgNum){
			nextImg = $('.background img').first();
			random = 0;
		}
		thisImg.removeClass('active').css('z-index','0').fadeOut(transition_speed);
		nextImg.addClass('active');
		//sizeFullscreenImage();
		nextImg.css('z-index','10').fadeIn(transition_speed);
	}
	
} else {
	$('.background img').addClass('active');
}

var img = $('.background img');
var img_height = img.height(); 
var img_width = img.width();
var ratio = img_height/img_width;

// FULLSCREEN BACKGROUND IMAGES
    function sizeFullscreenImage() {
        var container_height = $('.container .inner-wrapper').height() + 150;
        var container_width = $('.container').width();
        
        //size based on width.
        var oh = container_width*(ratio);
        var ow = container_width;
        
        if ( oh < container_height ){
            //size based on height.
            var ow = container_height/ratio;
            var oh = container_height;             
        } 
        $('.container').css('height', oh);            
        img.attr("height", oh).attr("width", ow); // Math.round?
        
        if (pageTempate == 'home'){
        	homeContentCenter();
        } else {
        	contentCenter();
        }
    } 
sizeFullscreenImage();
    
    $(window).resize(function() { 
        sizeFullscreenImage();
    });
    

// Vertically center content blocks
	//homepage
	function homeContentCenter(){
		var hContent = $('#home_template .content').height(),
			hSidebar = $('#home_template .sidebar').height(),
			container_height = $('.container').height(),
			content_margin = (container_height - hContent - 80)/2,
			sidebar_margin = (container_height - hSidebar - 80)/2;
			$('#home_template .content').css({'margin-top':content_margin});
			$('#home_template .sidebar').css({'margin-top':sidebar_margin});
	}
	//interior pages
	function contentCenter(){
		var hContent = $('.outer-wrapper').height(),
			hSidebar = $('.outer-wrapper .sidebar').height(),
			container_height = $('.container').height(),
			content_margin = (container_height - hContent - 40)/2,
			sidebar_margin = (hContent - hSidebar)/2;
			/*if (content_margin<50){ 
				content_margin = 50;
				var ch = $('.container').height();
				$('.container').css('height', ch + 50);
				}*/
			$('.outer-wrapper').css({'margin-top':content_margin});
	//		$('.outer-wrapper .sidebar').css({'margin-top':sidebar_margin});
	}
	

/* Initiate Executive toggle items */
$('.executives li .bio').hide();
$('.executives li a').not('.executives li .vcard a').toggle(
	function() { $(this).text('Hide bio').parent().parent().find('.bio').show('blind', function(){sizeFullscreenImage()} ); },
	function() { $(this).text('Read bio').parent().parent().find('.bio').hide('blind', function(){sizeFullscreenImage()} ); }
	);
$('#executive-toggle a').toggle(
	function() { 
		$(this).text('Hide all'); 
		$('.executives li a').not('.executives li .vcard a').each(
			function(){
				if ($(this).text()=='Hide bio'){ } else { $(this).click().text('Hide bio'); }; 
				});
		},
	function() { $(this).text('Show all'); 
		$('.executives li a').not('.executives li .vcard a').each(
			function(){
				if ($(this).text()=='Read bio'){ } else { $(this).click().text('Read bio'); }
				});
		}
	);

/* Projects column adjustments */
$('.portfolio li').css('margin','0 24px 0 0');
$('.portfolio li:nth-child(4n)').css('margin','0 0 0 1px');


/* Overlay functions */
$('.property-item .tooltip').click(function(){ 
	$(this).parent().find('a').click(); 
	});
	
$('.gallery a').click(function(){
	var largeImg = $(this).attr('href'),
		active = $(this).attr('class');
		//alert (active);
	$('.photo-large').html('<img src="'+largeImg+'" alt="Large Image" />');
	if (active==''){ $('.photo-large').fadeTo(0,0); $('.photo-large').fadeTo("slow",1); }
	$('.gallery a').removeClass('current');
	$(this).addClass('current');
	return false;
	});	
$('.gallery a').first().css("margin-right","13px").click();
$('.gallery a:nth-child(5n)').css("margin", "0 0 0 1px");

/* Initiate shadowbox -- NEEDS TO BE CALLED LAST */
if ($('.portfolio').length>0){
Shadowbox.setup(".portfolio li a", {
	handleOversize: "drag",
   	width: 660,
   	overlayOpacity: 0.7
    });
}
if ($('a.video').length>0){
Shadowbox.setup("a.video", {
	handleOversize: "drag",
   	height: 272,
   	width: 480,
   	overlayOpacity: 0.7,
   	flashVars: {
                autostart : 'true',
				mode : 'overlay',
				scaleMode : 'fit',
				themeColor : '6e93be',
				fontColor : 'ffffff'
                } ,
    players:    ['flv']
    });
}
}; /* End setup */



/* Hide empty elements */
function hideEmpty(element){
	var html = $(element).html(),
		text = $.trim(html);
	if (text==''){
		$(element).css('display','none');
	}
	if (text=='' && element=='.sidebar'){
		$('.content').addClass('wide');
	}
};

/* Embed video player */
function createVideo (videoURL, divID, vWidth, vHeight, lgImage) {
	var flashvars = {}; 
	flashvars.src = videoURL;
	flashvars.themeColor = "6e93be";
	flashvars.mode = "overlay";
	flashvars.autostart = "false";
	flashvars.scaleMode = "stretch";
	flashvars.fontColor = "ffffff";
	flashvars.loadImage = lgImage;
	var params = {}; 
	params.quality = "autohigh"; 
	params.wmode = "transparent";
	params.allowFullScreen = "true";
	var attributes = {};  
	swfobject.embedSWF("/JamestownResources/js/player.swf", "player", 480, 272, "10.0.0", false, flashvars, params, attributes);
};


/* Loading fix for WebKit */
if (!/WebKit/i.test(navigator.userAgent)) {
	$(function () {
		setup();
	});
} else {
	// work around WebKit loading bug
	$(window).load(setup);
};



