// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function lightbox(action) {	
	lb = $('#lightbox');
	if(action == 'close') {
		lb.hide();
		$('#lightbox-overlay').hide();
		$('#lightbox-inner').html('');		
	} else {
		$('#lightbox-overlay').show();
		lb.show();
    $('#lightbox-content').css('position', 'absolute');
    $('#lightbox-content').css('top', $(window).scrollTop() + "px");
    $('#lightbox-content').css('left',  (($(window).width() - $('#lightbox-content').width()) / 2) + "px");
	  if(jQuery.browser.msie) {
      if(jQuery.browser.version == '6.0') {
  	    $('#lightbox-overlay').css("top", $(window).scrollTop() + "px");
  	    $('#lightbox-overlay').css('width', $(window).width() + 'px');
  	    $('#lightbox-overlay').css('height', $(window).height() + 'px');
  	    $(window).scroll(function() {
  	      $('#lightbox-overlay').css("top", $(window).scrollTop() + "px");
  	    });
  	  }
	  }
	}
}

function load_score() {
  $.ajax({dataType:'script', url:'/quizzes/results'})
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

var flux_user = null;

function set_user() {
  Flux.loadContext(function(context) {
    if(context.user)
      flux_user = context.user;
  });
}

function flux_session() {
  atl_cookie = readCookie('flux_ucid');  
  if(flux_user) {
    if(flux_user.communityMember) {
      if(flux_user.ucid != atl_cookie) {
        // user was logged in as one community member, now logged in as another
        // OR user has just logged in
        $.ajax({data:'ucid='+flux_user.ucid, dataType:'script', url:'/people/flux_session'});
      }
    } else if(atl_cookie) {
      // user was logged in as community member, now logged in as non-community member
      $.ajax({dataType:'script', url:'/people/flux_session'});
    }
  } else if(atl_cookie) {
    // user was previously logged in, not anymore
    $.ajax({dataType:'script', url:'/people/flux_session'});  
  }
}

function drop_downs() {
  $('.drop-down').each(function() {
    var d = this;
    var p = $(this).parent();
    // hide/show drop-down
    $(p).hover(
      function () {
        $(d).show();
      },
      function () {
        $(d).hide();
      }
    );
    
    // rollover effect
    $(d).children('ul').children('li').each(function() {
      var c = this;
      $(c).hover(
        function () {
          $(c).addClass('over');
        }, 
        function () {
          $(c).removeClass('over');
        }
      );
    });
  });
}

function setup_quiz_question(number) {
  $('#response-form table tr').each(function() {
    var id = $(this).attr('id').replace(/row-/i, '');
    $(this).hover(
      function() {
        $(this).addClass('highlight');
      },
      function() {
        $(this).removeClass('highlight');
      }
    );
    $(this).click(function() {
      var r = this;
      // deselect others
      $('#response-form table tr').each(function() {
        var i = $(this).attr('id').replace(/row-/i, '');
        $(this).removeClass('selected');
        $('#quiz-checkbox-'+i).removeClass('on');
        $('#question_'+number+'_'+i).attr('checked', '');
      });
      $(r).addClass('selected');
      $('#quiz-checkbox-'+id).addClass('on');
      $('#question_'+number+'_'+id).attr('checked', 'checked');
    });
  });
}

var chatter_display = 'tweets';
function chatter(id) {
  if(id != chatter_display) {
    $('ul#chatter-menu').attr('class', 'flat chatter-'+id)
    $('ul.chatter-list').each(function() {
      $(this).hide();
    });
    $('ul#'+id).show();
    chatter_display = id;
  }
}



  
function clear_empty_fields() {
  $('#challenge-participants input[id$="_first_name"]').each(function(e) {
    if($(this).val() == 'First Name')
      $(this).val('');
  });
  $('#challenge-participants input[id$="_last_name"]').each(function(e) {
    if($(this).val() == 'Last Name')
      $(this).val('');
  });
  $('#challenge-participants input[id$="_email"]').each(function(e) {
    if($(this).val() == 'Email')
      $(this).val('');
  });
  
  // general info
  f = $('.general-info input[id$="_first_name"]');
  if(f.val() == 'First')
    f.val('');
  f = $('.general-info input[id$="_last_name"]');
  if(f.val() == 'Last')
    f.val('');
  $('.general-info input[id$="_address_1"]').each(function(e) {
    if($(this).val() == 'Line 1')
      $(this).val('');
  });
  $('.general-info input[id$="_address_2"]').each(function(e) {
    if($(this).val() == 'Line 2')
      $(this).val('');
  });
  $('.general-info input[id$="_city"]').each(function(e) {
    if($(this).val() == 'City')
      $(this).val('');
  });
  $('.general-info input[id$="_zip"]').each(function(e) {
    if($(this).val() == 'Zip')
      $(this).val('');
  });
  f = $('.general-info input[id$="_email"]');
  if(f.val() == 'Email')
    f.val('');
  f = $('.general-info input[id$="_telephone"]');
  if(f.val() == 'XXX-XXX-XXXX')
    f.val('');
  return true;
}

function highlightInput(input) {
  var parent = $(input).parent('dd').parent('dl');
  if(!parent.hasClass('active')) {
    $('dl').removeClass('active');
    parent.addClass('active');
  }
}

function defaultValue(obj, val) {
  $(obj).bind("focus", function(e) {
    if($(this).val() == val) {
      $(this).val('');
      $(this).addClass('focus');
    } 
  });
  $(obj).bind("blur", function(e) {
    if($(this).val() == '') {
      $(this).val(val);
      $(this).removeClass('focus');
    } else if(!$(this).hasClass('focus')) {
      $(this).addClass('focus');
    }
  });
  $(obj).trigger('blur');
}

function copyTemporaryAddress(obj) {
  if($(obj).attr('checked')) {
    $('.general-info input[id*="temporary_address"]').each(function(e) {
      id = $(this).attr('id').replace(/(.*)temporary_/i, '');
      $('.general-info input[id$="permanent_'+id+'"]').val($(this).val());
    });
  }
}


// SLIDING CONTENT PANES
var sliderHeight = "150px";

function initialize_sliders() {
  $(".slider-menu").html('<a href="#" class="button slider-down">Read More</a>');

  $('.slider').each(function () {
    var current = $(this);
    current.attr("box_h", current.height());
    current.children('.slider-menu').children('a').click(function() {
      openSlider(current); return false; 
    });
  });

  $(".slider").css("height", sliderHeight);
}

function openSlider(s) {
  var open_height = $(s).attr("box_h") + "px";
  $(s).animate({"height": open_height}, { duration: "slow" });
  $(s).children('.slider-menu').html('<a href="#" class="button slider-up">Close</a>');
  $(s).children('.slider-menu').children('a').unbind();
  $(s).children('.slider-menu').children('a').click(function() { closeSlider(s); return false; });
}
function closeSlider(s) {
  $(s).animate({"height": sliderHeight}, {duration: "slow" });
  $(s).children('.slider-menu').html('<a href="#" class="button slider-down">Read More</a>');
  $(s).children('.slider-menu').children('a').unbind();
  $(s).children('.slider-menu').children('a').click(function() { openSlider(s); return false; });
}



var quizResults = null;
function getQuizResults() {
	return quizResults;
}
function refreshQuizResults() {
	if(quizResults != null)
		$('#carousel')[0].loadQuizResults(quizResults);
}
function learnMoreLightbox() {
	$.ajax({complete:function(request){lightbox()}, success:function(request){$('#lightbox').html(request);}, url:'/quizzes/explanation'}); return false;
}
function shareScoreLightbox() {
	$.ajax({complete:function(request){lightbox();}, success:function(request){$('#lightbox').html(request);}, url:'/quizzes/share' }); return false;
}

// Page Load
$(function() {
  set_user();
  flux_session();
  drop_downs();
  initialize_sliders();
});
