// Cufon JS

Cufon.replace('.gotham');
Cufon.replace('.ketchup-error-container');

var isTabClicked = false;

$(document).ready(function() {
						
	
	// Cufon rollovers
	
	$("#top-bar a").each(function (i) {
		$(this).mouseover(function() {
											  
			Cufon.replace($(this), {
				hover: true,
				color: '#ed217c'
			});
		
		});
		$(this).mouseleave(function() {
										  
			Cufon.replace($(this), {
				hover: true,
				color: '#222222'
			});
		});
	
    });
	$("#navigation a").each(function (i) {
		$(this).mouseover(function() {
											  
			Cufon.replace($(this), {
				hover: true,
				color: '#ffffff'
			});
		
		});
		$(this).mouseleave(function() {
										  
			Cufon.replace($(this), {
				hover: true,
				color: '#dfdfdf'
			});
		});
	
    });
	
	// Some gradient styling for buttons
	
	$('.pink-btn').each(function(el){
		Cufon.replace(this, {
			color: '-linear-gradient(#ececec, 0.2=#fff, #ececec)'
		});
	});
	
	$('.blue-btn').each(function(el){
		Cufon.replace(this, {
			color: '-linear-gradient(#ececec, 0.2=#fff, #ececec)'
		});
	});

	
	// Tabs
	
	$tabs = $('#tabs').tabs(
		/* Can't do this until I figure out how to trigger the cufon replace at the same time as the classes being switched in the jquery tabs			
		{fx: {opacity: 'toggle'}}
		*/
	);

	$('.inner-tabs').each(function(el){
		$(this).mouseover(function(){
			$(this).tabs('rotate', 0, false);
		});
		$(this).mouseout(function(){
			$(this).tabs({ fx: {opacity: 'toggle'} }).tabs('rotate', 20000);
		});
	});		
	
	// Update tab colors when a new tab is selected
	
	$('#tabs .tab-nav > li a').click(function(el) {
		resetSlider();
		Cufon.replace($(this), {
			hover: true,
			color: '#383838'
		});
		
		$(this).parent().siblings().each(function(el){
			Cufon.replace($(this), {
				hover: true,
				color: '#cccccc'
			});
		});
	});
	
	$('.slideshow').cycle({ 
		fx: 'fade', 
		speed: 'fast',
		timeout: 0,
		next: '.next', 
		prev: '.prev' 
	});
	
	// Twitter
	$("#tweets").getTwitter({
		userName: "eyemagine",
		numTweets: 5,
		loaderText: "Loading tweets..."
	});
	
	//Sets the height for the video list based on the loaded video
	$('#video-list').ajaxComplete(function() {		
		$(this).css('height',$('#video iframe').attr('height'));		
	});
	
	//If no video has been loaded, load the first one in the list
	if($('#video').html() == ''){
		firstMov = $("#video-list .watch-video").first();
		sourceUrl = firstMov.attr('ref');
		loadContent(sourceUrl);
	}

	$(".watch-video").each(function (el) {
		$(this).click(function(){
			sourceUrl = $(this).attr('ref');
			loadContent(sourceUrl);
		});
	
	});
	
	
	//popups
	
	$('#jargon-popup').jqm({
		trigger: '#jargon-translator',
		overlay: 80,
		overlayClass: 'whiteOverlay',
		onShow: function(h) {
			h.w.css('opacity',1).fadeIn('slow'); 
        },
      	onHide: function(h) {
        	h.w.fadeOut('slow',function() { 
				if(h.o) h.o.remove();
				$("#jargon-results").empty();
			});
      	}
	});
	
	$('#share-popup').jqm({
		trigger: '#share-btn',
		overlay: 80,
		overlayClass: 'whiteOverlay',
		onShow: function(h) {
			h.w.css('opacity',1).fadeIn('slow'); 
        },
      	onHide: function(h) {
        	h.w.fadeOut('slow',function() { if(h.o) h.o.remove(); });
      	}
	});
	
	$('#subscribe-popup').jqm({
		trigger: '#subscribe-btn',
		overlay: 80,
		overlayClass: 'whiteOverlay',
		onShow: function(h) {
			h.w.css('opacity',1).fadeIn('slow'); 
        },
      	onHide: function(h) {
        	h.w.fadeOut('slow',function() { if(h.o) h.o.remove(); });
      	}
	});
	
	$('#video-popup').jqm({
		trigger: '#video-popup-btn',
		overlay: 80,
		overlayClass: 'whiteOverlay',
		onShow: function(h) {
			h.w.css('opacity',1).fadeIn('slow'); 
        },
      	onHide: function(h) {
        	h.w.fadeOut('slow',function() { if(h.o) h.o.remove(); });
      	}
	});
	
	
	$('#rackspace-popup').jqm({
		/**
		 * trigger is disabled inside this function,
		 * trigger is added after this function for multiple trigger functionality
		 */
		 
		/*trigger: '#rackspace-btn',*/
		overlay: 80,
		overlayClass: 'whiteOverlay',
		onShow: function(h) {
			h.w.css('opacity',1).fadeIn('slow'); 
			$('body').scrollTop(0); // This scrolls the page to the top of browser window once rackspace lightbox is called.
        },
      	onHide: function(h) {
        	h.w.fadeOut('slow',function() { if(h.o) h.o.remove(); });
      	}
	});
	$('#rackspace-popup').jqmAddTrigger('#rackspace-btn');
	$('#rackspace-popup').jqmAddTrigger('#rackspace-logo-btn');
	$('#rackspace-popup').jqmAddTrigger('#rackspace-footer-btn');
	
	//inputs
	
	$('.input-text').each(function(el){			   
		$(this).focus(function() {
			focusInput($(this));
		});					   
		$(this).blur(function() {
			blurInput($(this));
		});
	});
	
	$('.input-textarea').each(function(el){			   
		$(this).focus(function() {
			focusInput($(this));
		});					   
		$(this).blur(function() {
			blurInput($(this));
		});
	});
	
	$('#tabs .tab-nav > li.ui-state-active a').each(function(el) {
		Cufon.replace($(this), {
			hover: true,
			color: '#383838'
		});
		
		$(this).parent().siblings().each(function(el){
			Cufon.replace($(this), {
				hover: true,
				color: '#cccccc'
			});
		});
	});
	
	startSlider();
});


// Videos

function loadContent(sourceUrl) {
	$('#video').load(sourceUrl);
		
}


// Form Inputs

function focusInput(thefield){
	if (thefield[0].defaultValue==thefield[0].value){ thefield[0].value = ""; }
	$(thefield).addClass('active');
} 

function blurInput(thefield){
	if(thefield[0].value == ''){ thefield[0].value = thefield[0].defaultValue; }
	if($(thefield).hasClass('active') && thefield.value == thefield.defaultValue){
		$(thefield).removeClass('active');
	}
}

function externalLinks() {  
 if (!document.getElementsByTagName) return;  
 var anchors = document.getElementsByTagName("a");  
 for (var i=0; i<anchors.length; i++) {  
   var anchor = anchors[i];  
   if (anchor.getAttribute("href") &&  
       anchor.getAttribute("rel") == "external")  
     anchor.target = "_blank";
 }
 $("#tweets").contents().find("a").each(function (el) {
		if (this.getAttribute("href")) { 
	     	this.target = "_blank";
		}
	});
 
}
function resetSlider()
{	
	/**
	 * Resets Slider
	 */
	$('.inner-tabs').each(function(el)
	{
		$(this).tabs('abort');		
		$(this).tabs('rotate', 0, false);
		$(this).tabs('select', 0);	
	});
	startSlider();
}
function startSlider()
{
	/**
	 * Reinitializes Slider
	 */
	$('.inner-tabs').each(function(el)
	{
		$(this).tabs('abort');
		$(this).tabs({ selected: 0 });		
		$(this).tabs({ fx: {opacity: 'toggle'} }).tabs('rotate', 20000); // rotates 20 seconds
	});	
}


window.onload = externalLinks;


