$(document).ready(function() {		
		
	/*$(".nav1_but").hover(
	  function () {
		$(this).css("backgroundColor","rgba(171,171,171,1)");
		}, 
	  function () {
		$(this).css("backgroundColor","rgba(171,171,171,0)");
	  }
	);*/
	$('.shuffy').click(function(){
		location.href='home.php';
	});
	
	
	
	$(".ev_cont").hover(
	  function () {
	  		if ($(this).children(".event").size() > 1){		
				$(this).children(".scroll_left").animate({
					opacity: 1,
					top: '28',
					left: '-10',
					height: '40',
					width: '27'
				  }, 200, function() {
					// Animation complete.
				  });
				$(this).children(".scroll_right").animate({
					opacity: 1,
					top: '28',
					left: '358',
					height: '40',
					width: '27'
				 }, 200, function() {
					// Animation complete.
				 });
			}
		  }, 
		  function () {
			if ($(this).children(".event").size() > 1){
				$(this).children(".scroll_left").animate({
					opacity: 0.3,
					top: '30',
					left: '0',
					height: '36',
					width: '24'
				  }, 500, function() {
					// Animation complete.
				  });
				 $(this).children(".scroll_right").animate({
					opacity: 0.3,
					top: '30',
					left: '350',
					height: '36',
					width: '24'
				  }, 500, function() {
					// Animation complete.
				  });
				}
			});
			
	$('.scroll').click(function(){
		if ($(this).siblings(".event").size() > 1){		
			$current = parseInt($(this).siblings('.ev_current').val());
			$total = parseInt($(this).siblings(".event").size());
			$side = $(this).attr('id');
			if ($side == "bl"){
				if ($current == 1){
					$next = $total;
				}else{
					$next = $current- 1;
				}
			}else{
				if ($current == $total){
					$next = 1;
				}else{
					$next = $current + 1;
				}		
			}
			$(this).siblings('.ev_' + $current).fadeOut(200,function(){
				$(this).siblings('.ev_' + $next).fadeIn(200);
			});						
			$(this).siblings('.ev_current').val($next);
		}		
	});	

	$(".create_event").click(function(){	
		$( "#dialog-form-event" ).dialog("open");
		return false;
	});
	
	$("#upPhoto").click(function(){	
		$("#dialog-form-upload-club-photos").dialog('open');
		return false;
	});
	
	$("#upAd").click(function(){
		$("#dialogs").empty();		
		$("#dialogs").load("aj_edit_advert.php?sid=0&r=" + Math.random());		
	});
	
	$("#forgot").click(function(){
		$('#reset_email').val($('#user_email').val());
		$('#dialog-forgot').dialog('open');		
	});
	
	$("#chat").click(function(){
		$.get("aj_chat_dialog.php?uid=0&r=" + Math.random(), function(data) {
  			$('#chat_dialog').append(data);
		});
  	});
	
	$(".profpic").click(function(){
		$.get("aj_chat_dialog.php?uid=" + this.id + "&r=" + Math.random(), function(data) {
  			$('#chat_dialog').append(data);
		});
  	});
	
	$(".post_notice").click(function(){	
		$( "#dialog-form-notice" ).dialog("open");
		return false;
	});
	
	$(".attend_event").click(function(){	
		$(this).load("c_post_handler.php?formname=attend&s=" + $(this).html().substring(0, 1) + "&i=" + $(this).attr('id'));
		return false;
	});

	$("#dialog-form-upload-club-photos").dialog({
		autoOpen: false,
		modal: true,
		width:450,		
		resizable: false,
		buttons: {			
			cancel: function() {
				$(this).dialog('close');
			},
			upload: function() {
				document.forms['form-upload-club-photos'].submit();
			}
		},
		close: function() {
		}
	});
	
	$("#dialog-forgot").dialog({
		autoOpen: false,
		modal: true,
		width:405,		
		resizable: false,
		buttons: {			
			cancel: function() {
				$(this).dialog('close');
			},
			'reset': function() {
				document.forms['form-forgot'].submit();
			}
		},
		close: function() {
		}
	});

	$("#dialog-form-event" ).dialog({
		autoOpen: false,
		modal: true,		
		resizable: false,
		width:425,
		close: function() {
		}
	});
	
	$("#dialog-form-notice" ).dialog({
		autoOpen: false,
		modal: true,
		resizable: false,
		width:425,
		close: function() {
		}
	});
	
	$("#dialog-form-acks" ).dialog({
		autoOpen: true,
		modal: true,
		width:500,
		resizable: false,
		close: function() {
		}
	});
	
	
	$(".dial_close").click(function(){	
		$(this).parents('div').dialog("close");
	});

	$( "#event_date" ).datepicker({
		dateFormat: 'yy-mm-dd',
		defaultDate: "+1"
	});
	
	$( "#event_date" ).datepicker("setDate", "+1");
	
	$(".but").button();
		
	$("#wall").keypress(function(event) {
  		if ( event.which == 13  && $("#wall").val() != "") {
			event.preventDefault();
			document.forms["form-wall-post"].submit();
   		}
	});
	
	$(".in_c_com").keypress(function(event) {
		if ( event.which == 13  && $(this).val() != "") {
			event.preventDefault();
			$(this).parent().trigger('submit');
   		}
	});
		
	ping_chat();		
});

function ping_chat(){
	
	$.get("aj_chat_controls.php?c=2&r=" + Math.random(), function(data) {
		if (data.length>0){
			rooms = data.split(",");
			for (var key in rooms){
				if ($('#dialog-chat-' + rooms[key]).length){
					if (!$('#dialog-chat-' + rooms[key]).dialog('isOpen')){
						$('#dialog-chat-' + rooms[key]).dialog('open');						
					}						
				}else{					
					$.get("aj_chat_controls.php?c=3&r=" + rooms[key], function(data){
						$('#chat_dialog').append(data);
					});
				}
			}
		}
	});
	setTimeout("ping_chat()",20000);
}
