// On load
if (!window.console) { window.console = { log: function(obj){ /* define own logging function here, or leave empty */ } }; }
var deleteMessages = ['JEEZ, Don\'t scare me like that!','Well, don\'t click DELETE then!','Phew, That was close!'];
$(document).ready(function() {
	//alert('huh');
	$('#friendlistcontent').hide();
	$('#friendlisttoggle').click( function() {
		$('#friendlistcontent').slideToggle("slow");
	});
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	var config = {    
		 sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 10, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	$('body').mouseleave( function(){
		$(this).find(".sub").hide();
	});

	$("ul#topnav > li").has(".sub ul").each( function() {
		var content = $(this).find('.sub ul');
		$("a:first", this).click( function(e) {; 
			content.addClass("navigator");
			e.preventDefault();
			megaHoverOut();
			$.colorbox({open:true,inline:true,href:content }); 
		});
	});
	$("h4.expander").click(function(){
			  $(this).next("div.collapsible_container").animate({
              "height": "toggle", "opacity": "toggle"
              }, 250,function() {
						//$('#widget').masonry(); 
				});
    });
	if($("a.popup").length) {
		var $poppedup = $("a.popup");
		var contWidth = '';
		var contHeight = '';
		var contTitle = '';
		$poppedup.each( function(e) {
			console.log(e);
			console.log($(this));
			
			if($(this).attr('data-w')!=undefined) {
				contWidth = $(this).attr('data-w');
			} else if($(this).attr('w')!=undefined) {
				contWidth = $(this).attr('w');
			} else {
				alert('Not able to set width of colorbox popup');
			}
			if($(this).attr('data-h')!=undefined) {
				contHeight = $(this).attr('data-h');
			} else if($(this).attr('h')!=undefined) {
				contHeight = $(this).attr('h');
			} else {
				alert('Not able to set width of colorbox popup');
			}
			$(this).colorbox({title:contTitle,innerWidth:contWidth, innerHeight:contHeight,opacity:0.4, iframe:true});
		});
	}
	if($("a.delete_popup").length) {
   		$(".delete_popup").click( function() {
			var $elementToActOn = $(this);
			if(confirm($elementToActOn.attr('title') + "?")) {
				$elementToActOn.colorbox({transition:"fade",innerWidth:'350', innerHeight:'200',opacity:0.9,title:$elementToActOn.attr('title'), open:true, iframe:true});
				return false;
			} else {
				 var deleteMessage = deleteMessages[Math.floor(Math.random() * deleteMessages.length)];
				alert(deleteMessage);//'JEEZ, Don\'t scare me like that!'
				return false;
			}
		});
	}
	$('#content').masonry({
	  singleMode: false,
	  columnWidth: 315,
	  itemSelector: '.brick',
	  resizeable: true,
	  animate: true,
	  animationOptions: {},
	  saveOptions: true
	});
	$(":input.datetime").attr("readonly","true").dynDateTime({
			showsTime: true,
			ifFormat: "%Y-%m-%d %H:%M:00",
			daFormat: "%l;%M %p, %e %m,  %Y",
			align: null,
			electric: false,
			singleClick: true
		});
	$(":input.dateonly").attr("readonly","true").dynDateTime({
			ifFormat: "%Y-%m-%d",
			daFormat: "%l;%M %p, %e %m,  %Y",
			align: null,
			electric: false,
			singleClick: true
		});
	$(":input#datepicker").blur(function(){
             var newDate = this.value;
             $(":input.childDate").val(newDate);
    });
    

});
function showActionMessage(message,messageClass)
{
	var scrolledOffset = $(window).scrollTop();
	var $targetDiv = $('#action_msg');
	messageClass = messageClass || "successful";
	var $targetDiv = $('#action_msg');
	var scrollDistance = scrolledOffset + 50;
	$targetDiv.removeClass().addClass(messageClass).html(message);
	$('#action_msg').animate({top: scrolledOffset},100).animate({top: scrollDistance},2000).delay(2500).animate({top: "-100"},"slow");
}
function popup(URL,width,height){window.open(URL,'_popup','width='+width+',height='+height+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=no,screenX=400,screenY=400,top=400,left=400')}
function gone(URL,width,height){ var r=confirm("Are you SURE you want to delete?");if(r==true){window.open(URL,'_blank','width='+width+',height='+height+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=no,screenX=400,screenY=400,top=400,left=400')}else{alert("Well don't press delete then")}} function SendHome(){ setTimeout('parent.location.reload();','1500')}
function submitToPopup(formObject,width,height){popup(formObject.action,width,height);formObject.submit();return false;}
function deleteConfirmation(url) {
	var answer = confirm("Are you sure you want to delete that!")
	if (answer){
		popup(url,225,225);
	}
	else{
		alert("PHEW!!  That was close!")
	}
}
