$(document).ready(function(){
	
	// Primary Dropdowns
	$('#priNavCont > ul > li > ul').wrap('<div class="dropdownShadowWrapper"><div class="dropdownShadowWrapperContent"></div></div>');
	$('.dropdownShadowWrapperContent').append('<div class="dropdownShadowTop"></div><div class="dropdownShadowBottom"></div>');
	$('#priNavCont ul li ul').css('display', 'block');

	$("#priNavCont > ul > li").hover(
		function () {
			$(this).find('.dropdownShadowWrapper').stop(true, true);
			$(this).find('.dropdownShadowWrapper').fadeIn("fast");
			$(this).addClass('menuActive');
		}, 
		function () {
			$(this).find('.dropdownShadowWrapper').fadeOut("fast");
			$(this).removeClass('menuActive');
		}
	);		

  // Homepage Rich Media
  if($("#homeRichMediaWrapper").length) {
		$("div#homeRichMediaWrapper").smoothDivScroll({ startAtElementId: 'richMediaStart' });
  }
	
	$('#homeRichMediaWrapper a').blend();
	
	// Add class to last li element
	$('#siteBodySidebar > ul > li:last').addClass('last');
	$('#priNavCont > ul > li > ul > li').siblings(':last-child').addClass('last');
	
	// Projects Listing Hovers	
	$(".projectListingItem").hover(
		function () {
			$(this).children('.overlay').stop(true, true);
			$(this).children('.overlay').fadeIn(400);
		}, 
		function () {			
			$(this).children('.overlay').fadeOut(400);			
		}
	);	
	
	// Projects Menu
	$("#projectsMenu > li").hover(
		function () {
			$('#projectsMenu > li > ul').stop(true, true);
			$('#projectsMenu > li > ul').fadeIn("fast");
			$('#bOurProjectsBanner').addClass('active');
		}, 
		function () {
			$('#projectsMenu > li > ul').fadeOut("fast");
			$('#bOurProjectsBanner').removeClass('active');
		}
	);	

  // Cufon Text Replacement
  Cufon.replace('.homeCTA p, .homeLargeCTA .homeLargeCTALeadIn, #siteBodyBottomCTA .leadIn', { fontFamily: 'Sorts Mill Goudy' });
	Cufon.replace('#siteBodyPageContent .leadIn, #siteBodyPageContent blockquote', { fontFamily: 'Sorts Mill Goudy' });
	Cufon.replace('#siteBodyPageContent h1, #siteBodyPageContent h2, #siteBodyPageContent h3, #teamItemPosition', { fontFamily: 'Sorts Mill Goudy' });	
	Cufon.replace('#siteFooterMiddle p, #siteFooterBottom p, #siteFooterTwitterContent, #siteFooterTwitterReadMore', { fontFamily: 'Sorts Mill Goudy', hover: true });
	// Gotham HTF
	Cufon.replace('.homeCTA h2, .homeLargeCTA .homeLargeCTATitle, #siteFooterMiddle h3, #projectDetailsCont h2, #siteBodyBottomCTA h2, .projectListingItem .overlayTitle, #teamItemName h2', { fontFamily: 'Gotham HTF' });	

  // Image Gallery
  if($('.imageGalleryCont').length) {
    		
		// Image Gallery - Define Number Of Visual Thumbnails
		var imageGalleryNumbOfThumbs = 5;
		
		// Get length of thumbnail items
		var imageGalleryThumbLength = $('.imageGalleryNav li').length;
		
		// Add class for no scrollbars if there's not enough thumbnails to scroll through
		if(imageGalleryThumbLength <= imageGalleryNumbOfThumbs) {
			$('.imageGalleryNavCont').addClass('noScroller');
		}
    // Image Gallery - Setup Scrollable Nav
    $(".imageGalleryNavItems").scrollable({ speed: 300 });

    var imageGalleryScroller = $(".imageGalleryNavItems").data("scrollable");
    
    var getNumberOfItems = imageGalleryScroller.getSize();
    var getScrollMax = getNumberOfItems - imageGalleryNumbOfThumbs;
    
    // Image Gallery - Next Button
    $('.bImageGalleryNext, .bGalleryRichMediaNext').click(function() {  
      var getCurrentIndex = imageGalleryScroller.getIndex();        
 
      if(getScrollMax != getCurrentIndex && getNumberOfItems > imageGalleryNumbOfThumbs) {
       imageGalleryScroller.next();
      }
			
			checkScrolling();
      return false;
    });
		
    // Image Gallery - Prev Button
    $('.bImageGalleryPrev, .bGalleryRichMediaPrev').click(function() {
      imageGalleryScroller.prev();
			checkScrolling();
      return false;
    });
		
		// Check whether to disable next/prev buttons
		function checkScrolling() {
			var availableScrollIndex = getNumberOfItems - imageGalleryNumbOfThumbs;
			
			//alert(imageGalleryScroller.getIndex());
			
			//alert("index is: " + imageGalleryScroller.getIndex() + " and MaxIndex is: " + availableScrollIndex);
			
      // Check next button
			if(imageGalleryScroller.getIndex() >= availableScrollIndex) {
				$('.bImageGalleryNext').addClass('disabled');
			} else if(imageGalleryScroller.getIndex() <= availableScrollIndex) {
				$('.bImageGalleryNext').removeClass('disabled');
			}
			
			// Check previous button
			if(imageGalleryScroller.getIndex() != 0) {
				$('.bImageGalleryPrev').removeClass('disabled');
			} else {
				$('.bImageGalleryPrev').addClass('disabled');
			}

	
		}
		
		// Run the scroller check
		checkScrolling();		
    
    // Image Gallery - Setup Tabs
    $(".imageGalleryNav").tabs(".imageGalleryMainImageCont > .imageGalleryMainImageItem", {
    	// enable "cross-fading" effect
    	effect: 'fade',
    	fadeOutSpeed: "slow",
			rotate: true
      });
    
    var imageGalleryNavAPI = $(".imageGalleryNav").data("tabs");
    
    // Image Gallery - Large Next Button
    $('.bGalleryRichMediaNext').click(function() {
			var tabLimit = getNumberOfItems - 1;
			var currentTabIndex = imageGalleryNavAPI.getIndex();
			
			// Scroll to the beginning after reaching the end
			if(currentTabIndex == tabLimit) {
				imageGalleryScroller.begin();		
			}
			
			checkScrolling();
			imageGalleryNavAPI.next();			       
    });    
    
    // Image Gallery - Large Prev Button
    $('.bGalleryRichMediaPrev').click(function() {
			var tabLimit = getNumberOfItems - 1;
			var currentTabIndex = imageGalleryNavAPI.getIndex();
			
			// Scroll to the beginning after reaching the end
			if(currentTabIndex == 0 & getNumberOfItems > imageGalleryNumbOfThumbs) {
				imageGalleryScroller.seekTo(tabLimit);		
			}
			
			checkScrolling();
			imageGalleryNavAPI.prev();			 
    });		

  };      	

});

// Twitter Feed
$(function($){
	$(".tweet").tweet({
		username: "SSDGI",
		count: 1,
		loading_text: "Loading Tweets..."
	}).bind("loaded", function() { Cufon.refresh(); });
});

// External Links
var externalLinks = {
	init: function(){
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") anchors[i].target = "_blank";
		}
	}
}

function siteOnload(){
	externalLinks.init();
}

function addLoadEvent(func){
	var oldOnLoad = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}
	else{
		window.onload = function(){
			oldOnLoad();
			func();
		}
	}
}

addLoadEvent(siteOnload);
