/*
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 design layers */
$(".header .nav").append("<div class='color-shim-left'/>").append("<div class='color-shim-right'><div class='color-shim-top-right'/><div class='color-shim-top'/></div>");
$(".header .nav ul").prepend("<li class='color-shim-left'> </li>");
$(".container").append("<div class='shadow-shim-left'/>").append("<div class='shadow-shim-right'/>").append("<div class='bottom-shim'/>");
$('.externallinks ul li').not('.externallinks ul li:last').after('<li>&#92;</li>');
$('.footer-menu li').not('.footer-menu li:last').after('<li>&#92;</li>');
//$('q, blockquote p').append('&#8221;').prepend('&#8220;');
$('.executives li:even').css('background-color','#e8ebee');
$('.pdf').append(' <img src="/JamestownResources/images/pdf_small.gif"/>');
$('.doc').append(' <img src="/JamestownResources/images/doc_small.gif"/>');

/* Remove empty structural DIVs */
hideEmpty('.subnav');
hideEmpty('.sidebar');
if ( $('.sidebar').length==0){
	$('.content').addClass('wide');
}

/* CSS styles for JavaScript supported browsers */
$("#mini-scroll-1").add("#mini-scroll-2").css({"position":"absolute"});

/* 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() { $(this).text('Read bio').parent().parent().find('.bio').hide('blind'); }
	);
$('#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'); }
				})
		}
	);

/* Initiate scrollable items */
$("#property-scroll").scrollable({
	circular: true, 
	mousewheel: true, 
	visableItems:6,
	speed:500,
	items: ".scrollitems"
	}).autoscroll();
$("#mini-scroll-1").scrollable({
	circular: true, 
	mousewheel: true,
	speed:1500,
	next: ".noButton",
	prev: ".noButton"
	}).hover(
	function() {
		$('#mini-scroll-1').data('scrollable').next();
		miniScroll1 = setInterval ( "$('#mini-scroll-1').data('scrollable').next();", 4000 );		
		},
	function(){
		clearInterval (miniScroll1);
		});
$("#mini-scroll-2").scrollable({
	circular: true, 
	mousewheel: true,
	speed:1500,
	next: ".noButton",
	prev: ".noButton"
	}).hover(
	function() {
		$('#mini-scroll-2').data('scrollable').next();
		miniScroll2 = setInterval ( "$('#mini-scroll-2').data('scrollable').next();", 4000 );		
		},
	function(){
		clearInterval (miniScroll2);
		});
$("#news-ticker").scrollable({
	circular: true,
	mousewheel: true
	});

/* Main scroller pause assistant */
var mainscroller = $('#property-scroll').data('scrollable');
$('.banner').mouseenter(function(){ clearTimeout(scrollerRestart); mainscroller.stop();  });
$('.header, .subnav, .content').mouseenter(function(){ scrollerRestart = setTimeout( mainscroller.play(), 2000); });

/* Initiate tooltips items */
$("#property-scroll .property-item").each(function(index){
	var imgTip = $(this).find("img");
	$(imgTip).tooltip({
		position: 	'bottom right',
		effect: 'fade',
		relative: true,
		offset: [-50,-165],
        onBeforeShow: function (e, index) {
            $('#property-scroll').data('scrollable').pause();
            }
		});
	});
	
/* 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 ($('#property-scroll').length>0){
Shadowbox.setup(".property-item a", {
	handleOversize: "drag",
   	width: 660,
   	overlayOpacity: 0.7
    });
}
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);
};



