// JavaScript Document
var PS = {};

PS.unsubscribe = function(link, id, type) {
	$.post('http://{site}/', {
		ACT: 35, type: type, id: id
		}, function(rsp) {
		$(link).parent().html('<input type="checkbox" name="categories[]" value="'+rsp.id+'"/>'+rsp.text);
	}, "json");
	
};

PS.addRemoveFeaturedPrntr = function(add, event) {
	$("#featured-events").parent().parent().replaceWith('<ul id="featured-events" class="form jcarousel-skin-tango"');
	$("input[checked]").each(function() {
		var img = $(this).parent().contents('img').attr('src');
		var li = '<li id="event'+this.value+'">'+
				'<img height="60" width="60" src="'+img+'"/>'+
		//{!--<p>{title}</p>--}
		'</li>';
		$("#featured-events").append(li);
	});
	$("#featured-events").jcarousel(); 
}

PS.sign_up = function(event_id, forward) {
	$.getJSON(
		'/index.php', 
		{ACT: 37, form: 'sign_up', member_id: 0, entry_id: event_id}, 
		function(json) {
			if (json.success) {
				$("#register-login").dialog({ hide: 'slide' });
				$('#signup').load("/learn/upcoming/?ajax_panel=signupform", function(resp) {
					
					//$(this).jqm().jqmShow();
				//	$('#not-started-yet').bind('dialogclose', function() {
						/*$('.jqmClose').bind('click', function() {
							if (!forward) {
								location.reload(true);
							}
							else {
								location = $('#button_' + PS.current_event).attr('href');
							}
						});*/
					//});
					
					
					$("#signup form input[name=field_id_31]").val(10800);
					$("#signup form input[name=event_id]").val(event_id);
					$("#signup form").ajaxSubmit({url:'/index.php', success:function() {
						$("#signup form input[name=field_id_31]").val(86400);
						$("#signup form").ajaxSubmit({url:'/index.php',success:function() {
							$("#not-started-yet").dialog('option', 'buttons', { "Ok" : function() {$("#not-started-yet").dialog('close')} });
							$("#not-started-yet").dialog('option', 'title', 'Sign up successfull');
							var title = $('#button_' + event_id).parent().parent().parent().prev().contents("h4").text();
							$("#not-started-yet").html('You are now signed up to '+title+'. Press OK to continue.');
							$('#not-started-yet').bind('dialogclose', openEvent);
							
							if ($('#button_' + event_id).hasClass('before-fifteen') || !$('#button_' + event_id).hasClass('enter')) {
								$('#not-started-yet').dialog("open");
							} else {
								openEvent();
							}
							
					
					
						}});
					}});
					
					var openEvent = function() {
						if (!forward) {
							location.reload(true);
						}
						else {
							location = $('#button_' + event_id).attr('href');
							
							if ($('#button_' + event_id).hasClass('view-event')) {
								location.reload();
							}
						}
					};
					
					$('.notthanks').bind('click',function() {
						if (!forward) {
							location.reload(true);
						}
						else {
							location = $('#button_' + PS.current_event).attr('href');
						}
					});
				
				
				});
				
				//$('#signup').jqm().jqmShow();
			}
			else {
				location = url;
			}
		}
	);
		
};

PS.changeReminder = function(jango_id,entry_id, link) {
	var li = $(link).parent();
	var text = $(link).prev().text();
	li.children('span.timebefore').html('');
	li.children('.change-reminder, .remove-reminder').remove();
	li.children('span.timebefore').append($('#reminders-box form:first, #reminder form:first').clone(true).show());
	li.children('span.timebefore').children('form').children('select').children('option').map(function() {
		if (text == $(this).text()) {
			this.selected = true;
		}
	});
	
	li.children('span.timebefore').children('form')
		.append('<input type="hidden" name="entry_id" value="'+entry_id+'"/>')
		.append('<input type="hidden" name="jango_id" value="'+jango_id+'"/>');;
	return false;
};
PS.deleteReminder = function(jango_id, entry_id, link) {
	$('<img>').attr('src', '/images/ajax-loader.gif').css(
								{
									position:'absolute', top: $(link).offset().top, left: $(link).offset().left
								}).appendTo('body').attr('id', 'loading');
							
	$.post('/?ACT=36',
		{jango_id: jango_id, entry_id: entry_id},
		function() {
			$(link).parent().remove();
			$("#loading").remove();
		}
	);
	return false;
};

PS.popupReminder = function(entry_id) {
	$('#reminder-popup').attr('class', 'jqmWindow').appendTo('body').jqm({ajax:"/includes/reminders/" + entry_id, onLoad: function(hash) {hash.w.append('<a href="#" id="jqmClose-bottom" class="jqmClose png">Close</a>').children('div').children('div').children('form').hide()}}).jqmShow();
}

PS.showGlobalLogin = function(url) {
	$('#global').jqmShow();
	if (url) {
		$('#globalLogIn form :hidden[name="RET"]').attr('value', url);
	}
}

// <![CDATA[
 $(document).ready(function() {
  $('a').focus(function(event) {
   $(this).blur();
  });
  
  $('ul.rating li a').click(function(event) {
   event.preventDefault();
   var par  = $(this).parents('ul.rating');
   var id   = $(par).attr('id');
   var num  = $(this).text();
   $(par).removeClass().addClass('rating star'+num);
   $('#rating-'+id).val(num);
  });
 });
// ]]>

$(document).ready(function() {
	/*$('#register-login').jqm();
	$('#not-yet-started').jqm();
	//$('#email').bind('focus', function() {this.value=''});
	$('body').append($('.jqmWindow'));(*/
	//$('#register-login').dialog({autoOpen: false});
	
	$("h3.review-hide").click(function(){$(".review-panel").slideToggle("fast");$(this).toggleClass("active");});	

	$("ul.events-list li:last, .bd > ul li:last").css("border-bottom","none").addClass("last");
	$("#featured-events-frontend").jcarousel(); 
	
	$("#sidebar .hd > h4").click(function(){$(this).parent().next().slideToggle("fast");$(this).toggleClass("active");}); 
	$("#sidebar > .mod, #sidebar > .mod > .bd").addClass("round"); 
	
	$('#not-yet-started').jqm();
	
	$(".sponsor_info:odd").addClass("odd");
	$(".sponsor_info:last").addClass("last");

	$(".entry:odd").addClass("odd");

	$("#authors-box p").addClass("authors-bio");
	
	$('#create-event-upload').jqm({modal: true, trigger: 'a.uploader'});
	$('#create-event-upload2').jqm({modal: true, trigger: 'a.uploaderbm'});
	
	$('#daf-tell').jqm({modal: true, trigger: 'a.daf-tell'});
    $('#daf-tell-multi').jqm({ajax:'@href',modal:false, trigger: 'a.daf-tell-multi'}); 
		
	
	
	$("a.showLogin").click(function() {$('#global-login').dialog('open')});
	$("a.showRegister").click(function() {$('#global-register').dialog('open')});
	
	$('#frmGlobalRegister').ajaxForm(
	{ 
		dataType: 'json',
		success: function(json) { 
			if (json.success) {
				$('#frm-error').html('You have succesfully registered. You will now be logged in!');
				location.reload(true);
			}
			else {
				$('#frm-error').html((errors[json.error]));
			}
		}
	}
	);

$('ul#profile-ul li:nth-child(even)').addClass("alt");
$('ul#profile-ul li').hover(function() {  $(this).addClass('pretty-hover');}, function() {  $(this).removeClass('pretty-hover');});


$('#f_cr_browse').change(function() {if(this.value!=""){location=this.value}});


$('ul#cat_topics li:nth-child(even)').addClass("alt");

$('#container-1 > ul').tabs();

$(".toggler").toggle(
			function() {
				$('#bio-extended').slideDown();
				$(this).html("Hide full bio...");
			},
			function() {
				$('#bio-extended').slideUp();
				$(this).html("Show full bio...");
			});


$("#banner-daf h2, #banner-daf h3, #banner-daf li p, #banner-daf div.sponsor h4, #banner-daf div.sponsor p").dropShadow({left: 1, top: 1, opacity: 0.7, blur: 2});
$('div#banner-daf ul li').addClass("png");

$("input#email").focus( function()
 { // only select if the text has not changed
 if(this.value == this.defaultValue) { this.value=''; }})
$('#frm-login').ajaxForm(
	{
	beforeSubmit: function(data, form) {
		//form.parent().append('<span id="animation">Registering <br/> <img src="/images/ajax-loader-squares.gif"/></span>');
		$('#frm-error').hide();
		this.success.form = form;
	},
	success: 
		function(response) { 
			var error;
			if (response.match('You must submit your username and password')) {
				$('#frm-error').show().html('You must submit your username and password!').insertAfter($("#login form > div.hiddenFields"));
				return;
			}
			if (response.match('The username you submitted was not found in the database') ||
				response.match('The password you submitted was not correct')) {
				$('#frm-error').show().html('Your username and/or password are incorrect!').insertAfter($("#login form > div.hiddenFields"));
				return;
			}
			
			$('#frm-error').show().html('You have logged in succesfully!').insertAfter($("#login form > div.hiddenFields")).css('background-color', 'blue');
			
			if (PS.current_event && PS.current_event != 0) {
				var form = this[0].form;
				$.post('/index.php', {event_id:PS.current_event,ACT: is_signed_up_action}, function(signed_up) {
					if (!eval(signed_up)) {
						form.parent().append('<span id="animation">Signing Up...<br/> <img src="/images/ajax-loader-squares.gif"/></span>');
						PS.sign_up(PS.current_event, true);
					}
					else {
						location = $('#button_' + PS.current_event).attr('href');
						location.reload();
					}
				}, "json");
			}
			else {
				location.reload(true);
			}
		}
	});
$('#frm-register').ajaxForm(
{ 
	url: '/index.php',
	beforeSubmit: function(data, form) {
		$("#register form :submit").hide()
		$('#frm-error').hide();
		$('<span id="animation">Registering <br/> <img src="/images/ajax-loader-squares.gif"/></span>').insertAfter($("#register form"));
		this.success.form = form;
	},
	success: function(data) {
		$("#animation").remove();
		if (!data.match('{success')) {
			var buffer = $('<div id="buffer"/>');
			buffer.html(data);
			buffer.css('display', 'none');
			$('body').append(buffer);
			var error = $('#buffer li').html()
			$("#buffer").remove();
			$('#frm-error').show().html(error).insertAfter($("#register form > div.hiddenFields"));
			$("#register form :submit").show();
		}
		else {
			var json = eval('(' + data + ')');
			if (json.success) {
				$('#frm-error').show().html('You have successfully registered. You will now be automatically logged in!').insertAfter($("#register form > div.hiddenFields")).css('background', 'blue');
				if (PS.current_event && PS.current_event != 0) {
					var form = this[0].form;
					$('#frm-error').fadeOut(2700, function() {
					$('<span id="animation">Signing Up...<br/> <img src="/images/ajax-loader-squares.gif"/></span>').insertAfter($("#register form"));
					PS.sign_up(PS.current_event, true);
					});
					
				}
				else {
					location.reload(true);
				}
			}
		}
	}
}
);

$(function() {
	$(".join-class").filter('.view-event').not('.before-fifteen').not('.attend').click(function() {
		$("#presentation-div").insertBefore('#content');
	});
	$("#close-button").click(function() {
		$("#presentation-div").insertAfter('#content p:first');
	});
});


$('.join-class').live('click', function() {
	var id = $(this).attr('id').split('_')[1];
	if ($(this).hasClass('attend') || $(this).hasClass('attend-archive')) {
		/*if ($(this).hasClass('open')) {
			location.hash = "enter";
		} */			
		
		$("#register-login").dialog("open");
		if ($(this).hasClass('restricted')) {
			$("#register").hide().parent().parent().width(300);
		}
		PS.current_event = $(this).attr('id').split('_')[1];
		return false;
	}
	else if ($(this).hasClass('sign-up')) {
		$(this).parent().append('Signing up <br/> <img src="/images/ajax-loader-squares.gif"/>');
		PS.sign_up(id, $(this).hasClass('enter'));
		return false;
	}
	else if ($(this).hasClass('enter') && $(this).hasClass('before-fifteen')) {
		$('#not-started-yet').dialog("open");
		return false;
	}
	else if ($(this).hasClass('view-event')) {
		if ($(this).hasClass('virtualyeshiva')) {
			if ($(this).attr('id').split('_')[1] == 0) {
				var dialog = $('<div style="height:100%;width:100%; overflow:hidden;padding:0px"><iframe height="100%" width="100%" src="/virtualyeshivaarchives/'+location.href.match(/\d+/)[0]+'/"></iframe></div>');
				dialog.appendTo("body").dialog({width:1024,height:768});
			}
			else {
				location.href="/open_virtual_yeshiva_event.php?event_id=" + $(this).attr('id').split('_')[1];
			}
		}
		else {
			var button = $(this);
			$("#close-button").click(function() {
				$('#presentation').hide().attr('src', '');
				$("#close-button").hide();	
				button.show();
			
				return false;
			});
		
			$('#presentation').attr('src', $('#presentation').attr('rc'));
			if ($("#presentation")[0].contentDocument) {
				$("#presentation")[0].contentDocument.location.href = $('#presentation').attr('src');
			}
			else {
				//$("#presentation")[0].contentWindow.document.location.href = $('#presentation').attr('src');
			}
			$('#presentation').show();
		
			$("#close-button").show();	
			$(this).hide();
		}
		
		$.get('/log_user/' + id);
		
		return false;
	}
});
	if (location.hash == '#enter') {
		$('.join-class').filter('.view-event').not('.before-fifteen').click();
	}
});

