// byt core js

// GLOBAL ELEMENTS
var edit_text = $("<span id='edit_text'>Click to Edit</span>");

(function($){
$(document).ready(function() {

//	$.preloadCssImages();

	// Form Functions 
	$("fieldset.search input[type=text]").blur(function() {
		if ($(this).val() != "") {
			$(this).addClass("focus");
		} else {
			$(this).removeClass("focus")
		}
	}); // END focus


	setHeight($(".sidebar"), $(".content_main"), $("#interior"), .97);
	setHeight($(".sidebar"), $(".content_main"), $("#profile"));

	// Featured Description reduce text size
	if( $("#forum_topic .featured .description h3").length > 0){
		reduceFontSize ($(".featured .description h3"),90);
	}
	
	// static demo-only
	$(".so_there").live("click",function(e){e.preventDefault();$(this).removeClass('so_there');$(this).addClass('im_there');});
	$(".im_there").live("click",function(e){e.preventDefault();$(this).removeClass('im_there');$(this).addClass('so_there');});

	// Event Page 
	
	// Show all event information
  var event_home_tiles_on = false;
	$("ul.list_actions li.show a").toggle (
		function() {
			$(".event_calendar ul .day .info").fadeIn("slow");
			$(".event_calendar ul .day .opac").fadeIn("slow");
			$(this).html("hide all tiles");
      event_home_tiles_on = true;
			return false; // prevent default action
		}, 
		function() {
			$(".event_calendar ul .day .info").fadeOut("slow");
			$(".event_calendar ul .day .opac").fadeOut("slow");			
			$(this).html("expose all tiles");
      event_home_tiles_on = false;
			return false; // prevent default action
		}				
	);
	
	// Show event information on hover 
	$(".event_calendar .col .day").hover( 
		function() { 
      if(event_home_tiles_on == false){
  			$(this).find(".info").fadeIn();
	  		$(this).find(".opac").fadeIn();
      }
		},
		function() { 
      if(event_home_tiles_on == false){
  			$(this).find(".info").fadeOut();
	  		$(this).find(".opac").fadeOut();
      }
		}
	);
	
	
	// Event Listing Slide 
	$(".venue_listing .venue h3").live('click',
		function(){
			
			var elem = $(this).parent().find(".more").first();
			var cur = $(this);
			if (cur.hasClass('on')){
				cur.removeClass('on');
				elem.animate({height:"toggle"}, "slow");
			} else{
				cur.addClass('on');
				elem.animate({"height":"100%"}, "slow");
			}
			
			return true;
		}
	);
	
  initEventEdit();
  
	$("#event_search .event_search_fields a").click( 
		function(e) {
			e.preventDefault();// prevent default
			
			if ($(this).hasClass("show_search")) {
				$("#event_search .search_control").animate({"height": "229px"},"slow");
				$(this).html("Hide this Search Control");
				$(this).attr("class","hide_search");
			} else if ($(this).hasClass("hide_search")) {
				$("#event_search .search_control").animate({"height": "31px"},"slow");
				$(this).html("Show this Search Control");
				$(this).attr("class","show_search");			
			}
			
			return false; 

		}
	);

	// Removing empty elements from articles
	$(".article p:empty").remove();
	$(".article ul:empty").remove();
	$(".article li:empty").remove();
	$(".article p").each(function() {
		var elem = $(this);
		
		if(elem.html().length <= 2)
			elem.remove();
	});


	// Super generic Focus / Blur function for all inputs[type=text]
	$("input[type=text]").each( 
		function() {
		
			// Cache this and default value
			var elem  = $(this);
			var default_val = $(this).val();
		
			// Check to make sure focus/blur should be toggled	
			if (default_val != "") {
				elem.focus( function() {
					if ($(this).val() == default_val){
						$(this).val("");
					}
				});
				
				elem.blur( function() {
					if ($(this).val() == "") {
						$(this).val(default_val);
					}
				});
			}
		}
	);
	
	
	$(".wfeaturedarticles_paginate_link").click( function() {
		$(".wfeaturedarticles_container").hide("explode", 1000);
	});
	
}); // END document.ready
})(jQuery);

// broken out to be called after modal popup updates content
function initEventEdit(){
	// EDIT Blocks in events page
  $("#event_detail .editable").unbind('hover');
	$("#event_detail .editable").hover ( 
		function() { 
			showEditHover($(this)); 
		}, 
		function() { 
			hideEditHover($(this));
		} 
	);

  $("#event_detail .editable").unbind('click');
	$("#event_detail .editable").click (
		function(){editThis($(this));}
	);
  // the following is necessary to prevent event bubbling - and infinite recursion - when triggering the embedded edit links from a div
  $("#event_detail .editlink").unbind('click');
	$("#event_detail .editlink").click (
    function(event){event.preventDefault();return false;}
  );	
}

// Adds animation to change the background color when a user is online
function userOnline () {
	$(".user_online").animate({'backgroundColor':"#CD5A7B"}, 1500);	
	$(".user_online").animate({'backgroundColor':"#d8d8d8"}, 1500);
}

/**
	Text size reduction function:
	 elem = text node
	 len  = the baseline length that you want to test against (ie: if the length is less than the len, then don't do anything)
*/
function reduceFontSize (elem, len) {
	
	// Check elem length against baseline len		
	if (elem != null && elem.html().length >= len) {
		// Get current fontsize (remove stupid 'px' from string)
		var fontsize = elem.css("fontSize").substr(0,elem.css("fontSize").length-2);	
		
		// Calculate new fontsize based on the percetage of how much longer the string length is than the baseline reduce fontsize by that much percentage + 5 (5 seemed to do a logical trick)
		var size = fontsize * (len / (elem.html().length) ) - 5;

		// Set new font size
		elem.css({"font-size":size+"px","margin-top":5+"px"});
	}
	
}

function showEditHover(elem) {	
	$(elem).css("position","relative").find(".edit_opac").fadeIn();
}

function hideEditHover(elem) {
	$(elem).find(".edit_opac").fadeOut();
}

function editThis (elem){
  elem.find('.editlink').click();
}

function handleModalFormResponse(data,formId,updateId,replaceId){
  if(jQuery(data).size() > 0){
    if(data.errors !== undefined){
      jQuery('#'+formId).find('.loading').removeClass('loading');
      jQuery('#'+formId).addFormErrors(data.errors);
    }
    else if(data.flash){
      var flashtype = data.flash.type;
      var message = data.flash.message;

      flashMessage(message,flashtype);
      if(flashtype === "success"){
        Shadowbox.close();
      }
      else{
        jQuery('#'+formId).find('.loading').removeClass('loading');
      }
    }
    else if(data.location !== undefined && data.location !== ''){
      window.location = data.location;
    }
    else{
      if(data.content){
        if(updateId !== undefined && updateId !== ''){
          jQuery('#'+updateId).html(data.content);
        }    
        if(replaceId !== undefined && replaceId !== ''){
          jQuery('#'+replaceId).replaceWith(jQuery(data.content));
        }
      }
      if(data.replace_elements !== undefined){
        for(var id in data.replace_elements){
          jQuery('#'+id).replaceWith(jQuery(data.replace_elements[id]));
        }
      }
      if(data.update_elements !== undefined){
        for(var id in data.update_elements){
          jQuery('#'+id).html(data.update_elements[id]);
        }
      }
      Shadowbox.close();
    }
  }
}


/**
Sets the height of the specified container to match the other container
 elem = what you are setting the height of
 cont = container that needs the height
 base = what to set the height to	
 parent = body id (opt)
*/
function setHeight(elem, base, body, adjust) {
	if (body == null)
		body = $("body");
	
	if (adjust == null)	
		adjust=1;
	// Sidebar height for interior pages
	if ( body.length > 0 ) {		
		var height = (elem.height() < base.height() ) ? (base.height()-20) : elem.height();
		elem.height(height*adjust);		
	}	

}	

/* Animates the body's background to fade from one color to another */
function bodyBreathe(){

	// BG Breathe
	$("body").animate({backgroundColor:"#f80f8b"}, 30000, function() {
		$("body").animate({backgroundColor:"#00ffb4"}, 30000);
	});


}	
