/*
	WeddingRelay core.js
*/

function showTabContent(el){
	$(el).parent().parent().find('li').removeClass('active');
	$(el).parent().addClass('active');
	$(el).parent().parent().nextAll('div').hide();
	var idToShow = $(el).attr('rel');
	$('#' + idToShow).show();
}

function showRolloverContent(el){
	$('#rolloverLinks a').removeClass('active');
	$(el).addClass('active');
	$('#aboutTheService div').hide();
	var idToShow = $(el).attr('rel');
	$('#' + idToShow).show();
} 

function mouseoutRolloverContent(){
	$('#aboutTheService div').hide();
	$('#rolloverLinks a').removeClass('active');
	$('#aboutTheService #default').show();
}

function showStartTeam(src){ 
	if(src == 'dash'){
		$('#addAnother').hide();
		$('#startTeam').addClass('show');
	} else if(src == 'first team'){
		$('#buildTeam').hide();
		$('#startTeam').addClass('show');
	} else  {
		$('#startTeam h2').hide();
		$('#startTeam a#btn_startTeam').hide();
		$('#startTeam form').show();
	}
}

function showSearchTeams(src){ 
	if(src == 'dash'){
		$('#addAnother').hide();
		$('#joinExisting').show();
	} else if(src == 'signup'){
		$('#join').hide();
		$('#joinExisting').show();
	}
}


// Toggle input values
function togi(el,val,event){
	if(event == 'f'){
		if($(el).val() == val){
			$(el).val('');
			$(el).removeClass('inactive');
		}
	} else if(event == 'b'){
		if($(el).val() == ''){
			$(el).val(val);
			$(el).addClass('inactive');
		}
	}
}

function togs(el,val){
	$(el).removeClass('inactive');
}

function setSelects(){
	$('select.inactive').each(function(i){
		($(this).val() != '') ? $(this).removeClass('inactive') : '';
	});
}

// Add invite form fields
function addInviteFields(el){
	var n = $(el).parent().parent().find('li.invite').length + 1;
	var fieldHTML = '<li id="invite'+ n +'" class="invite"><input type="text" name="firstname'+ n +'" value="first name" class="inviteFN inactive" onfocus="togi(this,\'first name\', \'f\');" onblur="togi(this,\'first name\', \'b\');" /><input type="text" name="emailaddr'+ n +'" value="email" class="inviteEM inactive" onfocus="togi(this,\'email\', \'f\');" onblur="togi(this,\'email\', \'b\');" /><select name="role'+ n +'" class="inviteRL inactive" onfocus="togs(this);"><option>role</option></select></li>';

	$(el).parent().before(fieldHTML);
	
}

// Add school form fields
function addSchoolFields(el){
	var n = $('#profileInfo').find('li.school').length + 1;
	var fieldHTML = '<li id="school' + n + '" class="school"><label for="school' + n + '"></label> <input type="text" name="school' + n + '" id="school' + n + '" class="inactive" value="school ' + n + '" onfocus="togi(this,\'school ' + n + '\', \'f\');" onblur="togi(this,\'school ' + n + '\', \'b\');" /> <input type="radio" name="school_privacy' + n + '" value="0" class="radio" checked="checked" /> <input type="radio" name="school_privacy' + n + '" value="1" class="radio" /></li>';

	$(el).parent().before(fieldHTML);

}

// Address book show/hide content
function toggleAddrDetails(el){ 
	$(el).hasClass('closed') ? $(el).removeClass('closed').addClass('expanded').parent().nextAll('dl').show() : $(el).removeClass('expanded').addClass('closed').parent().nextAll('dl').hide();	
}

// Poll functions
function createPollBars(){
	
	$('#pollResults li span').each(function(){ 
		var percent = $(this).html().split(' ')[0];
		(percent == 0) ? percent = '.5' : '';
		var barDIV = '<div class="barwrap"><div class="bar" style="width: ' + percent + '%;"></div></div>';
		$(this).parent().append(barDIV);
		$(this).remove();
	});

	
}

function toggleAnswerFields(el){
	if(el == 'yesno'){
		$('#multichoice').hide();
		$('#yesorno').show();
	}
	else if(el == 'multi'){
		$('#yesorno').hide();
		$('#multichoice').show();
	}
}

function setPollAnswerFields(val){
	if(val == 'yes_no'){
		$('#multichoice').hide();
		$('#yesorno').show();
	}
	else if(val == 'multiple_choice'){
		$('#yesorno').hide();
		$('#multichoice').show();
	}

}

// Message and poll all guests checkbox
function selectAllCheckboxes(el){
	var parentChecked = el.checked;		
	$('ol#guests input.checkbox, ol#partyMembers input.checkbox ').each(function(){
			parentChecked ? (this.checked = "checked") : (this.checked = "");
	});
}

function selectAllGuests(el, isTask) {
	if( isTask == true ){
		varCheckboxes = 'ol#guestsTasks input.checkbox';
	}else{
		varCheckboxes = 'ol#guests input.checkbox';
	}
	
	var parentChecked = el.checked;		
	$(varCheckboxes).each(function(){
			parentChecked ? (this.checked = "checked") : (this.checked = "");
	});
}

function unselectAllGuests(isTask){
	if( isTask == true ){
		varCheckboxes = 'input#allGuestsTasks';
	}else{
		varCheckboxes = 'input#allGuests';
	}
	
	$(varCheckboxes).each(function(){
		this.checked = "";
	});	
}
//---------
function selectAllMembers(el, isTask) {
	if( isTask == true ){
		varCheckboxes = 'ol#partyMembers input.checkbox';
	}else{
		varCheckboxes = 'ol#guests input.checkbox';
	}
	
	var parentChecked = el.checked;		
	$(varCheckboxes).each(function(){
			parentChecked ? (this.checked = "checked") : (this.checked = "");
	});
}

function unselectAllMembers(isTask){
	if( isTask == true ){
		varCheckboxes = 'input#allMembersTasks';
	}else{
		varCheckboxes = 'input#allGuests';
	}
	
	$(varCheckboxes).each(function(){
		this.checked = "";
	});	
}
//-------------

// Event/task desc toggle
function toggleDesc(el){ 
	$(el).parent().hide();
	$(el).parent().next().fadeIn(100);
}

// Fix inline-block labels in FF2
function ff2LabelFix(){

  // Processing
 // $( 'form.signup, #loginOrSignup form' ).find( 'li label' ).not( '.nocmx' ).each( function( i ){
   // var labelContent = $(this).html();
    //var labelWidth = $(this).width() + 'px';

//    var labelSpan = document.createElement( 'span' );
  //      labelSpan.style.display = 'block';
    //    labelSpan.style.width = labelWidth;
      //  labelSpan.innerHTML = labelContent;
       // $(labelSpan).addClass('ff2');
    //this.innerHTML = null;
    //this.appendChild( labelSpan );
  //} );

}

function showNextSection() {
	i++;
	$('li#headline' + i).show('fast');
	$('li#body' + i).show('fast');
	if(i == 5){
		$('a.vendAddLink').hide('fast');
	}
}	

function popupWindow(URL) {
	window.open(URL,'WeddingRelay.com legal copy','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=543,height=622');
}

$(document).ready(function(){

	// Helpers for CSS3 in IE
	$('#event_search_results tbody td:last-child').addClass('last-child');

	setSelects();
	
	createPollBars();
	
	setPollAnswerFields($('#polltype input:checked').val());
	
	$("#start_date").datepicker({ mandatory: true, dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'] });
	$("#end_date").datepicker({ mandatory: true, dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'] });
	
	ff2LabelFix();
	
	// Event handlers
	$('#loginOrSignup ul.tabs a').click(function(){ showTabContent(this); return false; });
	$('#vendorInfo ul.tabs a').click(function(){ showTabContent(this); return false; });
	//$('#rolloverLinks a').mouseover(function(){ showRolloverContent(this); return false; });
	//$('#rolloverLinks').mouseout(function(e){ e.stopPropagation(); mouseoutRolloverContent(); });
	$('a#btn_startTeam').click(function(){ showStartTeam($(this).attr('rel')); return false; });
	$('a#btn_searchTeams').click(function(){ showSearchTeams($(this).attr('rel')); return false; });
	$('#buildTeam a.btn').click(function(){ showStartTeam($(this).attr('rel')); return false; });
	$('li#addInvite a').click(function(){ addInviteFields(this); return false; });
	$('li#addSchool a').click(function(){ addSchoolFields(this); return false; });
	$('a.btn_showAddrDetails').click(function(){ toggleAddrDetails(this); return false; });
	$('#polltype input').click(function(){ toggleAnswerFields($(this).attr('id')); });
	$('#checkall').click(function(){ selectAllCheckboxes(this); });
	$('#allGuests').click(function(){ selectAllGuests(this); });
	$('ol#guests input.checkbox:not("#allGuests")').click(function(){ unselectAllGuests(); });
	$('#allGuestsTasks').click(function(){ selectAllGuests(this, true); });
	$('ol#guestsTasks input.checkbox:not("#allGuestsTasks")').click(function(){ unselectAllGuests(true); });
	$('#allMembersTasks').click(function(){ selectAllMembers(this, true); selectAllGuests(this, true); });
	$('ol#partyMembers input.checkbox:not("#allMembersTasks")').click(function(){ unselectAllMembers(true); unselectAllGuests(true); });
	$('a.more').click(function(){ toggleDesc(this); return false; });
	
	//popups for invite information
    $(document).ready(function(){
		$("#button").bind("click", function(e) {
		  $("#msgPreview").css({
		          left:$(this).offset().left-161,
		          top:$(this).offset().top-$("#bubble").height()-230
		      }).show();
		  e.stopPropagation(); // Stops the following click function from being executed
		  $(document).one("click", function(f) {
		      $("#msgPreview").hide();
		  });
		  return false;
		});
	});
	
	//popup for new member info
	$(document).ready(function(){
		$("a.whatsThis").bind("click", function(e) {
		  $("#msgAutoNote").css({
		          left:$(this).offset().left-224,
		          top:$(this).offset().top-$("#bubble").height()-94
		      }).show();
		  e.stopPropagation(); // Stops the following click function from being executed
		  $(document).one("click", function(f) {
		      $("#msgAutoNote").hide();
		  });
		  return false;
		});
	});


});