// initiate great things!
var tedxphoenix = {
	init: function() {
    tedxphoenix.setup.init();		
		tedxphoenix.fonts.init();
		tedxphoenix.nav.init();
		tedxphoenix.inputFields.init();
		if ($('.speakers').length > 0) {tedxphoenix.speakers.init();}
		if ($('#twitter .feed').length > 0) {tedxphoenix.twitter.init();}
		// add the scroll event listener
    if (window.addEventListener){
      window.addEventListener('scroll', handleScroll, false);
      //window.addEventListener('scroll', pageTop, false);
    }else{
      window.attachEvent('onscroll', handleScroll);
      //window.attachEvent('onscroll', pageTop);
    }
    pageTop();
    $(window).resize(function() {
      handleScroll();
      //pageTop();  
    });
	}
};

// setup assets
tedxphoenix.setup = {
  init: function() {
    var backTopCheck = false; 
    $('#content, .herospace, .logo-small, #back-to-top').css({'display':'none'});
    $(window).load(function() {
      $('.herospace').fadeIn(1500);        
      // load herospace
      if($('#herospace').length > 0) {     
        $('#herospace').nivoSlider({
          effect:'slideInRight',
          pauseTime:7000,
          startSlide:0,
          directionNav:false,
          directionNavHide:false,
          controlNav:true,
          keyboardNav:true,
          pauseOnHover:false
        });
      }
      
      // sponsors slideshow 
      if($('#sponsors-slideshow').length > 0) {    
        $('#sponsors-slideshow').cycle({ 
          fx:    'fade', 
          speed:  3300 
        });
      }
      
      // sliding tabs
      if($('#coda-slider-1').length > 0) {
        $('#coda-slider-1').codaSlider({
          crossLinking: true,
          dynamicArrows: false,
          dynamicTabs: false
        });
      }       
    });  
}};

// cufon fonts
tedxphoenix.fonts = {
  init: function() {    
    Cufon.replace('h1');
    Cufon.replace('h2');
    Cufon.replace('h3');
}};

// lava lamp effect
tedxphoenix.nav = {
  init: function() {    
    if($('#1').length > 0) {
      // aniamtions   
      $("#1").lavaLamp({
        fx: "backout", 
        speed: 700,
        click: function(event, menuItem) {
          $('.current').find('a').css({"color": "#ef3724"});
          return true;
        }
      });  
    }
}};

// speakers page
tedxphoenix.speakers = {
  init: function() {    
    if($('#1').length > 0) { 
      $('.speakers li').hover(function(){
        $(this).css({'background-color' : '#ededed'}).fadeIn(500);
      }, function () {
        $(this).css({'background-color' : 'transparent'});
      });
    }
  }};

//input/textarea focus stuff
tedxphoenix.inputFields = {
  init: function() {  
    $('input[type="text"]').addClass("idleField");
    $('textarea').addClass("idleField");
   	$('input[type="text"]').focus(function() {
   		$(this).removeClass("idleField").addClass("focusField");
  	  if (this.value == this.defaultValue){ 
  	    	this.value = '';
      }
      if(this.value != this.defaultValue){
    		this.select();
    	}
  	});
  	$('input[type="text"]').blur(function() {
  		$(this).removeClass("focusField").addClass("idleField");
  	  if ($.trim(this.value) == ''){
      	this.value = (this.defaultValue ? this.defaultValue : '');
  	  }
  	});
}};

// twitter shit! 
tedxphoenix.twitter = {
  init: function() {                 
    if ($('#twitter .feed').length) {     
      var ajax_load = "<img src='http://www.tedxphoenix.com/img/ajax-loader.gif' width='16' height='16' style='position:relative;top:50%;left:50%;' />";
      $('#twitter .feed').html(ajax_load).load('http://www.tedxphoenix.com/js/tweets.php').fadeIn(5000);
      setInterval(function() {
        $('#twitter .feed').html(ajax_load).load('http://www.tedxphoenix.com/js/tweets.php').fadeIn(5000);      
      }, 600000);
    }
}};

$(document).ready(tedxphoenix.init);
  
// handle fixed navigation
// Source: http://code.stephenmorley.org/javascript/detachable-navigation/
function handleScroll(){  
  if (window.XMLHttpRequest){
    var offset = window.pageYOffset
               ? window.pageYOffset
               : document.documentElement.scrollTop;
    
    var o1 = $('.logo-small').offset();
    var o2 = $('.lavaLampWithImage').offset();
    var leftWidth = ($(window).width() - 960) / 2;
    var distanceLeftEdge = leftWidth * 0.90;
    logoPosition = leftWidth - distanceLeftEdge;                     
    document.getElementById('nav-container').className =
        (offset > 185 ? 'fixed' : '');  
    
    if ($(window).width() >= 1325) {
      if (offset > 185) {
        $('.logo-small').css({"left": logoPosition + "px"}).fadeIn(); 
      } else {
        $('.logo-small').fadeOut(); 
      }
    } else {
      $('.logo-small').fadeOut();
    }
  }
}

function pageTop() { 
  if ($(window).width() >= 1220) {     
    $(window).scroll(function () {
			if ($(this).scrollTop() > 400) {
				$('#back-to-top').fadeIn();
			} else {
				$('#back-to-top').fadeOut();
			}
		});
	} else {
    $('#back-to-top, #back-to-top-int').fadeOut();
  }                                

	$('#back-to-top, #back-to-top-int').click(function () {
		$('html,body').animate({scrollTop: 0}, "fast", function() {
      //console.log('going...');
    });                           
		return false;
	});
}    
