var haiku_vote = 0;
var open_haikuc = 0;


$(document).keyup(function(event){
    if (event.keyCode == 27) { // escape
    	if ($('#login').length>0) $('#login').fadeOut();
    	if ($('#gpassword').length>0) $('#gpassword').fadeOut();
    }
});

$(document).ready(function(){
	
	$('#btn_chaikuc').live('click',function(){
		var my_elem = $(this);
		$(this).parent().parent().parent().parent().animate({'height':'129px'},300,function(){
			my_elem.parent().parent().parent().remove();
			open_haikuc = 0;
		});
		return false;
	});
		
	$('a.areply').live('click',function(){
		if ($(this).hasClass('rl')){
			var ww = $(window).height();
			var scroll_top = $(window).scrollTop();
			var top_pos = Math.round((ww-217)/2+scroll_top);
			$('#login').css('top',top_pos+'px');
			$('#login').fadeIn();		
		}
		else{
			var my_id = $(this).attr('id').replace('areply_','');
			var owner1 = $(this).attr('class').replace('areply','').replace(' rl','').replace(' owner_','');
			var owner2 = $(this).parent().attr('class').replace('aholder ','');
			var owner = (owner1==0 || owner2=='isn1')?0:1;
			var eh = $(this).parent().parent().parent().hasClass('hbe')?21:0;
			var bdh = 129+eh;
			if (open_haikuc!=my_id){
				var holder = $(this).parent().parent().parent();
				//var content = '<div id="haiku_cadd" class="haiku_comment open_haikuc_'+my_id+'"><form name="form_haiku_cadd" id="form_haiku_cadd" action="scripts/scr_haiku_cadd.php" method="post"><input type="hidden" name="haikuc_haiku" id="haikuc_haiku" value="'+my_id+'" /><input type="hidden" name="haikuc_notify" id="haikuc_notify" value="1" /><textarea id="haiku_comment" name="haiku_comment">Your reply here</textarea><div id="ahc_submit"><input type="button" name="btn_chaikuc" id="btn_chaikuc" value="Cancel" /><input type="submit" name="btn_chaiku" id="btn_chaiku" value="Submit" class="mr10" /></div></form></div>';
				var extra = '&nbsp;';
				var bh = 280+eh;
				if (owner==1){
					extra = '<input type="hidden" name="haikuc_notify" id="haikuc_notify" value="1" /><strong>Do you want to be notified when someone comments on this Haiku?</strong><div id="ahc_notify"><a href="#" id="ahcn1" class="sel">Yes</a><a href="#" id="ahcn0">No</a></div>';
					bh = 320+eh;
				}
				var content = '<div id="haiku_cadd" class="haiku_comment open_haikuc_'+my_id+'"><form name="form_haiku_cadd" id="form_haiku_cadd" action="scripts/scr_haiku_cadd.php" method="post"><input type="hidden" name="haikuc_haiku" id="haikuc_haiku" value="'+my_id+'" /><textarea id="haiku_comment" name="haiku_comment">Your reply here</textarea><div id="ahc_bottomh" class="clearer"><div id="ahc_bottoml">'+extra+'</div><div id="ahc_bottomr"><input type="submit" name="btn_chaiku" id="btn_chaiku" value="Submit" /></div></div></form></div>';
				if (open_haikuc!=0 && open_haikuc!=my_id){
					var my_elem = $('.open_haikuc_'+open_haikuc);
					$(my_elem).parent().animate({'height':bdh+'px'},300,function(){
						$(my_elem).remove();	
						open_haikuc = 0;
						holder.append(content);
						holder.animate({'height':bh+'px'},300,function(){
							open_haikuc = my_id;
							activate_cadd();
						});
					});
				}
				else{
					holder.append(content);
					holder.animate({'height':bh+'px'},300,function(){
						open_haikuc = my_id;
						activate_cadd();
					});
				}
			}
			else{
				var my_elem = $('.open_haikuc_'+open_haikuc);
				$(my_elem).parent().animate({'height':bdh+'px'},300,function(){
					$(my_elem).remove();	
					open_haikuc = 0;
				});
			}
		}
		return false;
	});
	
	$('div.action a.notify_off').live('click',function(){
		var my_id = $(this).attr('id').replace('notify_','');
		$.post('scripts/scr_haiku_notifyd.php',{id:my_id},function(){
			window.location.reload();
		});		
		return false;
	});

	$('div.hb_extra a.notify_off').live('click',function(){
		var my_id = $(this).attr('id').replace('hn_','');
		$.post('scripts/scr_haiku_notify.php',{id:my_id},function(){
			window.location.reload();
		});		
		return false;
	});

	$('a.hc_delete').live('click',function(){
		var my_id = $(this).attr('id').replace('haikuc_','');
		if (confirm('Are you sure?')){
			$.post('scripts/scr_haiku_cdelete.php',{id:my_id},function(){
				window.location.reload();
			});		
		}
		return false;
	});

	$('a.hu_delete').live('click',function(){
		var my_id = $(this).attr('id').replace('haikuu_','');
		if (confirm('Are you sure?\nAll the haikus and comments will be deleted also.')){
			$.post('scripts/scr_haiku_udelete.php',{id:my_id},function(){
				window.location.reload();
			});		
		}
		return false;
	});

	$('a.adelete').live('click',function(){
		var my_id = $(this).attr('id').replace('haiku_','');
		if (confirm('Are you sure?\nAll the comments will be deleted also.')){
			$.post('scripts/scr_haiku_delete.php',{id:my_id},function(){
				window.location.reload();
			});		
		}
		return false;
	});

	$('#menu li a.rl, #footer li a.rl').live('click',function(){
		var ww = $(window).height();
		var scroll_top = $(window).scrollTop();
		var top_pos = Math.round((ww-217)/2+scroll_top);
		$('#login').css('top',top_pos+'px');
		$('#login').fadeIn();
		return false;
	});

	$('a.vote_up').live('click',function(){
		if ($(this).hasClass('rl')){
			var ww = $(window).height();
			var scroll_top = $(window).scrollTop();
			var top_pos = Math.round((ww-217)/2+scroll_top);
			$('#login').css('top',top_pos+'px');
			$('#login').fadeIn();
		}
		else{
			if (haiku_vote==0){
				haiku_vote = 1;
				var my_id = $(this).attr('id').replace('vote_up_','').replace('p_','').replace('g_','');
				var my_elem = $(this);
				var type = 1;
				$.post('scripts/scr_haiku_vote.php',{id:my_id,type:type},function(data){
					var msg = 'Thank you!';
					if (data==2) msg = 'Already voted!';
					var votes = my_elem.parent();
					var votes_holder = votes.siblings('.votes_holder');
					votes.animate({'opacity':0},{queue:false});
					votes_holder.animate({'opacity':0},function(){
						votes.html('<em>'+msg+'</em>');
						votes_holder.load('scripts/get_haiku_vote.php',{id:my_id},function(){
							votes.animate({'opacity':1},{queue:false});
							votes_holder.animate({'opacity':1},function(){
								haiku_vote = 0;
							});
						});					
					});
				});
			}
		}
		return false;
	});
	$('a.vote_down').live('click',function(){
		if ($(this).hasClass('rl')){
			var ww = $(window).height();
			var scroll_top = $(window).scrollTop();
			var top_pos = Math.round((ww-217)/2+scroll_top);
			$('#login').css('top',top_pos+'px');
			$('#login').fadeIn();
		}
		else{
			if (haiku_vote==0){
				haiku_vote = 1;
				var my_id = $(this).attr('id').replace('vote_down_','').replace('p_','');
				var my_elem = $(this);
				var type = 2;
				$.post('scripts/scr_haiku_vote.php',{id:my_id,type:type},function(data){
					var msg = 'Thank you!';
					if (data==2) msg = 'Already voted!';
					var votes = my_elem.parent();
					var votes_holder = votes.siblings('.votes_holder');
					votes.animate({'opacity':0},{queue:false});
					votes_holder.animate({'opacity':0},function(){
						votes.html('<em>'+msg+'</em>');
						votes_holder.load('scripts/get_haiku_vote.php',{id:my_id},function(){
							votes.animate({'opacity':1},{queue:false});
							votes_holder.animate({'opacity':1},function(){
								haiku_vote = 0;
							});
						});					
					});
				});
			}
		}
		return false;
	});

	$('a.dlogin').live('click',function(){
		var ww = $(window).height();
		var scroll_top = $(window).scrollTop();
		var top_pos = Math.round((ww-217)/2+scroll_top);
		$('#login').css('top',top_pos+'px');
		$('#login').fadeIn();
		return false;
	});
	
	$('#login_close').live('click',function(){
		$('#login').fadeOut();
		return false;
	});

	$('#forgot_password').live('click',function(){
		$('#login').fadeOut(function(){
			$('#btn_gpassword').val('Submit');
			if ($('#btn_gpassword').hasClass('gpassword_close_btn')) $('#btn_gpassword').removeClass('gpassword_close_btn');
			$('#gpassword .fi_submit_left p').html('');
			$('#gpassword_account').val('');
			var ww = $(window).height();
			var scroll_top = $(window).scrollTop();
			var top_pos = Math.round((ww-217)/2+scroll_top);
			$('#gpassword').css('top',top_pos+'px');
			$('#gpassword').fadeIn();
		});
		return false;
	});

	$('.gpassword_close_btn').live('click',function(){
		$('#gpassword').fadeOut();
		return false;
	});

    $('.form div.fih').live('click',function(){
		$(this).children('input').focus();    
    });

    $('#form_password div.fih').live('click',function(){
		$(this).children('input').focus();    
    });

    $('#add_kaiku textarea').focus(function() {  
    	if (!$(this).hasClass('no_clear')){
	        if (this.value == this.defaultValue){  
	            this.value = '';
	        }  
	        if(this.value != this.defaultValue){  
	            this.select();  
	        } 
	    }
    });  

    $('#add_kaiku textarea').blur(function() {  
    	if (!$(this).hasClass('no_clear')){
	        if ($.trim(this.value) == ''){  
	            this.value = (this.defaultValue ? this.defaultValue : '');  
	        }  
	    }
    });  	

    $('.form input[type=text], .form input[type=password]').focus(function() {
		var classes = $(this).parent().attr('class').split(" ");
		var my_class = '';
		if ( classes[0].substr(0,2) == "iw" ){ 
			my_class = classes[0];
		}
		$(this).parent().addClass('focus'+my_class);  
		if (!$(this).hasClass('no_clear')){
	        if (this.value == this.defaultValue){  
	            this.value = '';
	        }  
	        if(this.value != this.defaultValue){  
	            this.select();  
	        }  
		}
    });  

    $('.form input[type=text], .form input[type=password]').blur(function() {  
		var classes = $(this).parent().attr('class').split(" ");
		var my_class = '';
		if ( classes[0].substr(0,2) == "iw" ){ 
			my_class = classes[0];
		}
        $(this).parent().removeClass('focus'+my_class);  
		if (!$(this).hasClass('no_clear')){
	        if ($.trim(this.value) == ''){  
	            this.value = (this.defaultValue ? this.defaultValue : '');  
	        }  
		}
    });  	
    
    $('#login_remember').live('click',function(){
    	if ($(this).hasClass('sel')){
    		$(this).removeClass('sel');
	    	$('input#remember').val(0);
    	}
    	else{
	    	$('input#remember').val(1);
    		$(this).addClass('sel');    	
    	}
    	return false;
    });

    $('#semail').live('click',function(){
    	var my_val = 1;
    	if ($(this).hasClass('sel')){
    		my_val = 0;
    		$(this).removeClass('sel');
    	}
    	else{
    		$(this).addClass('sel');    	
    	}
    	$('#user_semail').val(my_val);
    	return false;
    });

    $('#register_terms ul li a').live('click',function(){
    	var my_val = $(this).attr('href').replace('#terms','');
    	if ($(this).hasClass('sel')){
    		$('#register_terms ul li a').removeClass('sel');
    		$(this).removeClass('sel');
    	}
    	else{
    		$('#register_terms ul li a').removeClass('sel');
    		$(this).addClass('sel');    	
    	}
    	$('#user_terms').val(my_val);
    	return false;
    });
    
    $('#ahc_notify a').live('click',function(){
    	var my_val = $(this).attr('id').replace('ahcn','');
    	if ($(this).hasClass('sel')){
    		$('#ahc_notify a').removeClass('sel');
    		$(this).removeClass('sel');
    	}
    	else{
    		$('#ahc_notify a').removeClass('sel');
    		$(this).addClass('sel');    	
    	}
    	$('#haikuc_notify').val(my_val);
    	return false;
    });

    $('#ah_notify a').live('click',function(){
    	var my_val = $(this).attr('id').replace('ahn','');
    	if ($(this).hasClass('sel')){
    		$('#ah_notify a').removeClass('sel');
    		$(this).removeClass('sel');
    	}
    	else{
    		$('#ah_notify a').removeClass('sel');
    		$(this).addClass('sel');    	
    	}
    	$('#haiku_notify').val(my_val);
    	return false;
    });

	$('#ah_categ ul li a').live('click',function(){
		var my_val = $(this).html();
		$('#ah_sel_categ').html(my_val);
    	$('#haiku_category').val(my_val);
		$('#ah_categ ul').fadeOut();
		return false;
	});	

    $('#ah_sel_categ').live('click',function(){
    	if ($('#ah_categ ul').is(':visible')){
    		$('#ah_categ ul').fadeOut();
    	}
    	else{
    		$('#ah_categ ul').fadeIn();
    	}
    	
    	return false;
    });

	if ($('#myhaiku_avatar a').length>0){
		new AjaxUpload('#myhaiku_avatar', {
			action: 'scripts/scr_avatar_upload.php',
			name: 'user_avatar_upload',
			autoSubmit: true,
			responseType: false,
			onChange: function(file, extension){},
			onSubmit : function(file ,extension){
				if (extension && /^(jpg|jpeg)$/.test(extension)){
					$('#myhaiku_avatar a').addClass('loading');
					$('#myhaiku_avatar a img').fadeOut();
				} else {
					alert('Only JPEG files (.jpg; .jpeg; .jpe) are accepted!');
					return false;				
				}
		
			},
			onComplete: function(file, response) {
				if (response==0){
					$('#myhaiku_avatar a img').fadeIn(function(){
						$('#myhaiku_avatar a').removeClass('loading');
					});
					alert('Image could not be uploaded.\nPlease try again later.');
				}
				else{
					var r = response.split('|');
					$('#user_avatar').val(r[1]);
					$('#myhaiku_avatar a img').attr('src','upload/'+r[0]);
					$('#myhaiku_avatar a img').fadeIn(function(){
						$('#myhaiku_avatar a').removeClass('loading');
					});
				}										
			}
		});	
	}	
});