function do_replace(parameci){
	    return parameci.replace(/\s+/g,'-');
}

function do_tooltip(){
     $('a.load-local').cluetip({
     local:true, 
     cursor: 'pointer',
     mouseOutClose: true,
     positionBy:'fixed',
     topOffset:22,
     leftOffset:-80,
     sticky: true,
     dropShadow: false
  });
	 $('.share input').focus(function(){
                               $(this).select();
                      });
}
function refreshMyReply() {
	   var thid = $('#thought_id').text();
	   
	   var last_reply_id = $("#reply-list > ul > li:last-child").attr('rel');
	   jQuery.ajax({
	        type: "POST",
	        url: "index_ajax.php",
	        data: 'pag=xreply&thid='+thid+'&last_reply_id='+last_reply_id,
	        cache: false,
	        success: function(reponse){

  		                         eval('var json ='+reponse);
								 if(json.failure){ json = null; return;}								
								 var ul = document.createElement('UL');
								 ul.className = 'tought-list slide';
								 ul.innerHTML = json.innerHTML;
								 var new_id = $(ul).find('> li:nth(2)').attr('rel');
								 if($('#reply-list > ul:nth(0) > li:last-child').attr('rel') == new_id){
									 ul = json = null;
									 return;
								 }								 
			                     $("#reply-list > ul:nth(0)").before(ul);
								 $("#reply-list > ul:nth(0)").slideDown("slow");
								 ul = null;
								 json = null;
								}
	                          });
	   
	 $.timer(20000, function(){
				    refreshMyReply();
				});	
	
	// Hide Gif Spinning Rotator
      $('.ajax_loading').hide();
	
	// Show 'Submit' Button
    $('.post-thought .button').show();
}
function refreshMyReplyToReply(rid) {
	   
	   jQuery.ajax({
	        type: "POST",
	        url: "index_ajax.php",
	        data: 'pag=xreply_to_reply',
	        cache: false,
	        success: function(reponse){
                                 
  		                         eval('var json ='+reponse);
								 if(json.failure){ json = null;  return;}								
								 $('#reply-list-'+rid).after(json.innerHTML);
								 json = null;
								}
	                          });
	 $('#post-reply-'+ rid +' .button').show();
	 $('#post-reply-'+ rid +' .ajax_loading').hide();
}



function refreshMyContent(rel_tid) {
	  
	   var tid = $('#topic_id').val();
	   
	   var first_thought_id = $('#first_thought_id').text();
	   var filter = {'month':1,'week':2,'today':3};
	   var modes = {'best':1,'worst':2,'replies':3};
	   var last_thought_id = $("#tab_content > ul > li:last-child").attr('rel');
	 
	   jQuery.ajax({
	        type: "POST",
	        url: "index_ajax.php",
	        data: 'pag=xtopic&mode='+modes[$('#tabs > ul .ui-tabs-selected  a').attr('rel')]+'&filter='+filter[$('#tabs #sort .ui-tabs-selected  a').attr('rel')]+'&tid='+tid+'&last_thought_id='+last_thought_id+'&rel_tid='+rel_tid,
	        cache: false,
	        success: function(reponse){

  		                         eval('var json ='+reponse);
								 if(json.failure){ json = null;    
								 
								 
								 return; }								
								 var ul = document.createElement('UL');
								 ul.className = 'tought-list slide';
								 ul.innerHTML = json.innerHTML;
			                     $("#tab_content > ul:nth(0)").before(ul);
								 
								 $("#tab_content > ul:nth(0)").slideDown("slow");
								 ul = null;
								 json = null;
								 if(!rel_tid){
									   $("#topic_link").removeClass('hide');
									  }
									  else{
									   $("#topic_link").addClass('hide');	  
										  }
								}
	                          });
							  
		
	   
	  
	 
	$.timer(20000, function(){
				    refreshMyContent();
				});	
	// Hide Gif Spinning Rotator
      $('#post_thought .ajax_loading').hide();
	
	// Show 'Submit' Button
    $('.post-thought .button').show();
	
}

function limitChars(textid, limit, infodiv){
  var text = $('#'+textid).val(); 
  var textlength = text.length;
  if(textlength > limit){ 
    $('#infodiv').html((limit - textlength)).css("color","#ff1900");
	$('#post_submit').attr('disabled', 'disabled'); ;
    return true;
	}
  else if((limit - textlength) < 30){
    $('#infodiv').html((limit - textlength)).css("color","#c55f54");
     $('#post_submit').removeAttr('disabled');
	 return true;
	 }
  else{
    $('#infodiv').html((limit - textlength)).css("color","#9d9c9c");
	$('#post_submit').removeAttr('disabled');
     return true;
	 }	 
}


// When DOM is ready
$(document).ready(function(){
   
  
  
  //do some css hack (subheader)
   var header_content_height= $("#sub-header-content").height(); 
   if ($("#sub-header-reply").length > 0){   //for reply page
	   var header_content_height= $("#sub-header-reply").height()-3; 
   }
   $(".sub-header").height(header_content_height+19);
   $(".right-column").css('margin-top',header_content_height+34);
   $(".left-column").css('margin-top',header_content_height+28);
  //post and reply form
   $('.inputarea-shadow textarea').live('click',function(){
   var $textarea = $(this);
   	if($textarea.attr('org_value') && $textarea.attr('org_value') != $textarea.val()){ return;}
   
   $textarea.attr('org_value',$textarea.val()).val('').elastic();
  });	
   
   $('.inputarea-shadow textarea').blur(function(){
    var $textarea = $(this);
	if($textarea.val() == ''){
	   	$textarea.val($textarea.attr('org_value'));	
	}
  });	
  
  //post in input for post-thought
 
   $('#post_in').focus(function(){
   if(($('#post_in').val())=='choose a topic'){
    $('#post_in').val('');
   }
  
   }); 

   $("#post_in").autocomplete("php/post_in.php", {
		width: 180,
		selectFirst: true,
		resultsClass:"ac_results ac_post"
               
	});
    $("#post_in").result(function(event, data, formatted) {
		$('#topic_id').val(data[1]);
		var tid = $('#topic_id').val();
	});

  
  //post-thought
  $("#post_submit").click(function(){ 
    // Hide 'Submit' Button
    $('.post-thought .button').hide();
	// Show Gif Spinning Rotator
    $('#post_thought .ajax_loading').show();
	
	var tid = $('#topic_id').val();
	var rel_tid = $('#topic_id').attr('rel'); // use to for topic cat page
	
	
	var thought = $('#thought').val();
	if($('#thought').attr('title')==$('#thought').val()){
		// Hide Gif Spinning Rotator
        $('.ajax_loading').hide();
	    // Show 'Submit' Button
        $('.post-thought .button').show();
		return;
		}
	if (tid) {
		$.post("index_ajax.php", {
			pag: "xtopic",
			act: "thought-add",
			tid: tid,
			thought: thought
		}, function(data){
			 refreshMyContent(rel_tid);
			 $('#thought').val($('#thought').attr('title'));
			 $('#thought').css('height','20px');
			if (!rel_tid) {   //if is on all post page to show second time the post in input
				$('#topic_id').val('');
				
			}
			
		});
	
	$("#choose_topic").slideUp("slow");  
	
	$('#thought').val('');
	$('#post_in').val('choose a topic');
		
	}
	else{ // show the post in input on all post page
		$("#choose_topic").slideDown("slow");
		// Hide Gif Spinning Rotator
        $('.ajax_loading').hide();
	    // Show 'Submit' Button
        $('.post-thought .button').show();
	  }
    });	
   
   
   //post-reply
  $("#reply_submit").click(function(){
	  // Hide 'Submit' Button
    $('.post-thought .button').hide();
	// Show Gif Spinning Rotator
    $('.ajax_loading').show();							
	var thid = $('#thid').val();
	var comment = $('#comment').val();
	if(comment && comment!='write a reply...'){
	 $.post("index_ajax.php", { pag: "xreply", act: "thought-addreply", thid: thid, comment: comment  },
		   function(data){
			 refreshMyReply();
			 $('#comment').val($('#comment').attr('title'));
			 $('#comment').css('height','20px');
			 });
     
	}else{
		
	 $('.post-thought .button').show();
	
    $('.ajax_loading').hide();			
		
	}
 
 });

$('#search_key').focus(function(){
   $(this).val('');	
   });
    $('#search_key').blur(function(){
    if(($('#search_key').val())==''){
    $('#search_key').val('search posts');
   }
  });

do_tooltip();

// tab navigation
$('#tabs > #sub-header-content > .ui-tabs-nav a').live('click',function(e){ 
   
   $('.tought-list .ajax_loading_circle').show();
   $('.tought-list li').hide();
   $('.post-thought').show();
  
		
   $('.options a').attr('rel',0);
	var srcTarget = $(e.target);
	
    var tid = $('.topic_id').text();
	
    var new_thought_id = $(".new_post").attr('rel');
    e.preventDefault();
	e.stopPropagation();
	
	
	var $link = srcTarget.parent('a');
	if(!$link.length){
		$link = srcTarget;
	}
	
	if($link.attr('rel') == 'best' || $link.attr('rel') == 'worst' || $link.attr('rel') == 'replies'){
		$('.post-thought').hide();
		$('#sort').fadeIn('slow');
		$('#tabs #sort .ui-tabs-selected').removeClass('ui-tabs-selected').parent().children(':last').addClass('ui-tabs-selected');
	}else{
		$('#sort').fadeOut('slow');
	}	
	
	if($('#page').attr('rel')=='topic'){  //topic main page
	var modes = {'best':1,'worst':2,'replies':3};
	
	jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=xtopic&mode='+modes[$link.attr('rel')]+'&tid='+tid,
	   cache: false,
	   success: function(reponse){
			var $cont = $('#tab_content > ul');   
  		    eval('var json ='+reponse);
			if(!json.failure){
				
				//$cont.fadeOut();
				$cont.html(json.innerHTML);
				
			
				
              // $cont.fadeIn('slow');
				 
				$link.parent().parent().find('li').removeClass('ui-tabs-selected');
				$link.parent().addClass('ui-tabs-selected');
			
			}
		   
		}
	});
	}
	else if($('#page').attr('rel')=='profile'){ // profile page
		var modes = {'pr_replies':1,'pr_best':2,'pr_worst':3};
		var mid = $('#mid').attr('rel');
		
		
		jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=xprofile&mode='+modes[$link.attr('rel')]+'&mid='+mid,
	   cache: false,
	   success: function(reponse){
			var $cont = $('#tab_content > ul');   
  		    eval('var json ='+reponse);
			if(!json.failure){
				/*$cont.fadeOut();*/
				$cont.html(json.innerHTML);
				do_tooltip();

				/*$cont.fadeIn('slow');*/
				$link.parent().parent().find('li').removeClass('ui-tabs-selected');
				$link.parent().addClass('ui-tabs-selected');
			}
		   
		}
	 });
	}
	
});
//sub tabs
$('#tabs #sort a').click(function(e){
	e.preventDefault();
	e.stopPropagation();
	
	var srcTarget = $(e.target);
	if(srcTarget.parents('li').hasClass('ui-tabs-selected')){
		return;	
	}
   $('.tought-list .ajax_loading_circle').show();
   $('.tought-list li').hide();
   
   $('.options a').attr('rel',0);	
   							  

	
	var tid = $('.topic_id').text();


/*f*/	
	var $link = srcTarget.parent('a');
	if(!$link.length){
		$link = srcTarget;
	}
	
	var filter = {'month':1,'week':2,'today':3};
	var modes = {'best':1,'worst':2,'replies':3};
	
	jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	    data: 'pag=xtopic&mode='+modes[$('#tabs #sub-header-content > ul .ui-tabs-selected  a').attr('rel')]+'&filter='+filter[$link.attr('rel')]+'&tid='+tid,
	   cache: false,
	   success: function(reponse){
			var $cont = $('#tab_content > ul');   
  		    eval('var json ='+reponse);
			if(!json.failure){
				/*$cont.fadeOut();*/
				
				$cont.html('').html(json.innerHTML);
				do_tooltip();

				/*$cont.fadeIn('slow');*/
				$link.parent().parent().find('li').removeClass('ui-tabs-selected');
				$link.parent().addClass('ui-tabs-selected');
				$('.options').show();
			}else{
			   $('.tought-list .ajax_loading_circle').hide();
			   $cont.html('').append(json.innerHTML);
				$('.options').hide();
				$link.parent().parent().find('li').removeClass('ui-tabs-selected');
				$link.parent().addClass('ui-tabs-selected');

			}
		   
		}
	});
	
}); 

// pagination
$('.options a').click(function(e){
	var srcTarget = $(e.target);
   
	var lr = parseInt(srcTarget.attr('rel'),10)+35;
	var tid = $('.topic_id').text();
	e.preventDefault();
	e.stopPropagation();
	
	if($('#page').attr('rel')=='topic'){  //topic main page
	var filter = {'month':1,'week':2,'today':3};
	var modes = {'best':1,'worst':2,'replies':3};
   
	
	jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=xtopic&mode='+modes[$('#tabs ul .ui-tabs-selected  a').attr('rel')]+'&filter='+filter[$('#tabs #sort .ui-tabs-selected  a').attr('rel')]+'&lr='+lr+'&tid='+tid,
	   cache: false,
	   success: function(reponse){
			var $cont = $('#tab_content > ul');   
  		    eval('var json ='+reponse);
			if(!json.failure){
				/*$cont.fadeOut();*/
				$cont.html(json.innerHTML);
				do_tooltip();

				/*$cont.fadeIn('slow');*/
				srcTarget.attr('rel',lr);
			}
		   
		}
	 });
	} //end main topic page
	else if($('#page').attr('rel')=='profile'){ // profile page
	var mid = $('#mid').attr('rel');
	var modes = {'pr_replies':1,'pr_best':2,'pr_worst':3};
    jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=xprofile&mode='+modes[$('#tabs  ul .ui-tabs-selected  a').attr('rel')]+'&lr='+lr+'&mid='+mid,
	   cache: false,
	   success: function(reponse){
			var $cont = $('#tab_content > ul');   
  		    eval('var json ='+reponse);
			if(!json.failure){
				/*$cont.fadeOut();*/
				$cont.html(json.innerHTML);
				

				/*$cont.fadeIn('slow');*/
				srcTarget.attr('rel',lr);
			}
		   
		}
	 });
	
	}
	
	else if($('#page').attr('rel')=='search'){ // search page
	var q = $('#q').attr('rel');
	jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=xsearch&lr='+lr+'&q='+q,
	   cache: false,
	   success: function(reponse){
			var $cont = $('#tab_content > ul');   
  		    eval('var json ='+reponse);
			if(!json.failure){
				/*$cont.fadeOut();*/
				$cont.html(json.innerHTML);
				do_tooltip();

				/*$cont.fadeIn('slow');*/
				srcTarget.attr('rel',lr);
			}
		   
		}
	 });
	
	}
});
// show post details on hover
 
 $("ul.tought-list > li").live('mouseover',function(){
      
	  $(".mini-nav > li a",this).css("display","block");
	  $(".mini-nav > li.report",this).css("display","block");
	   	
	 }).live('mouseout',function(){ 
	  if(!$(".mini-nav > li a.best-btn",this).hasClass('is_best')){
		  $(".mini-nav > li a.best-btn",this).css("display","none");
		  }
	  if(!$(".mini-nav > li a.worst-btn",this).hasClass('is_worst')){
		  $(".mini-nav > li a.worst-btn",this).css("display","none");		 
			 }	
	  if(!$(".mini-nav > li a.best-reply-btn",this).hasClass('is_best')){
		  $(".mini-nav > li a.best-reply-btn",this).css("display","none");
		  }
	  if(!$(".mini-nav > li a.worst-reply-btn",this).hasClass('is_worst')){
		  $(".mini-nav > li a.worst-reply-btn",this).css("display","none");		 
			 }			 
	  if(!$(".mini-nav > li a.replies-btn",this).hasClass('is_replies')){
		  $(".mini-nav > li a.replies-btn",this).css("display","none");		 
			 }
	  
	  $(".mini-nav > li.report",this).css("display","none");
	  $(".mini-nav.reply > li.report",this).css("display","block");
	  $(".mini-nav.reply > li a",this).css("display","block");
						 			 	 
    });


 // show reply to reply form
  $(".reply_comm > .replies-btn").live('click',function(e){
  var srcTarget = e.target;
   if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }
  var elem = $(srcTarget);
  var reply_id = elem.attr('rel');
  reply_id= reply_id+'';
  $("#post-reply-"+ reply_id).slideDown('fast'); 
  e.preventDefault();
  e.stopPropagation();
 });    
  
  // reply to reply hide form
  $(".hide-btn").live('click',function(e){
  var srcTarget = e.target;
  var elem = $(srcTarget);
  var reply_id = elem.attr('rel');
  reply_id= reply_id+'';
  $("#post-reply-"+ reply_id).slideUp('fast'); 
  e.preventDefault();
  e.stopPropagation();
 }); 
 
 //post reply to reply
  $(".reply_to_reply_submit").live('click',function(){
	 var frm = $(this).parents('form');
	 var comment = $('textarea',frm).val();
	 var rid = $('#rid',frm).val();
	 var thid = $('#thid').val();
	 if(comment && comment!='write a reply...'){
	 $('#post-reply-'+ rid +' .button').hide();
	 $('#post-reply-'+ rid +' .ajax_loading').show();		 
	 $.post("index_ajax.php", { pag: "xreply", act: "thought-addreply_to_reply", comment: comment, rid: rid, thid: thid},
		   function(data){
			refreshMyReplyToReply(rid);
			 $('textarea',frm).val($('textarea',frm).attr('title'));
			 $('textarea',frm).css('height','20px');
			 });
	
	 }
	 
   });

 // Voting best for a reply to reply
  $(".best_reply_to_reply > .best-reply-btn").live('click',function(e){
   var srcTarget = e.target;
  if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }  
  var elem = $(srcTarget);
  var reply_to_reply_id = elem.attr('rel');
 
  jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=reply&act=vote-addreply_to_reply&rid='+ reply_to_reply_id +'&best=1',
	   cache: false,
	   success: function(reponse){
	   	eval('var json ='+reponse);
		     if(json.failure==false) // VOTE OK
             {  
                elem.html('');
                elem.html(json.best_num + "<span>like</span>");
			    elem.addClass("is_best");
             }  
             else if(json.voting!=1){
				  $("#loginsign_form").modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });
				 }
		}
	});
	e.preventDefault();
	e.stopPropagation();
 });
// Voting worst for a reply to reply
  $(".worst_reply_to_reply > .worst-reply-btn").live('click',function(e){
   var srcTarget = e.target;
  if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }  
  var elem = $(srcTarget);
  var reply_to_reply_id = elem.attr('rel');
 
  jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=reply&act=vote-addreply_to_reply&rid='+ reply_to_reply_id +'&worst=1',
	   cache: false,
	   success: function(reponse){
	   	eval('var json ='+reponse);
		     if(json.failure==false) // VOTE OK
             {  
                elem.html('');
                elem.html(json.worst_num + "<span>dislike</span>");
			    elem.addClass("is_worst");
             }  
             else if(json.voting!=1){
				  $("#loginsign_form").modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });
				 }
		}
	});
	e.preventDefault();
	e.stopPropagation();
 });
// Voting best for a reply
  $(".best > .best-reply-btn").live('click',function(e){
  var srcTarget = e.target;
  if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }  
  var elem = $(srcTarget);
  var reply_id = elem.attr('rel');
  var tid = $('#tid').val();
  var thid = $('#thid').val();
  jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=reply&act=vote-addreply&tid='+ tid +'&thid='+ thid +'&rid='+ reply_id +'&best=1',
	   cache: false,
	   success: function(reponse){
	   	eval('var json ='+reponse);
		     if(json.failure==false) // VOTE OK
             {  
                elem.html('');
                elem.html(json.best_num + "<span>like</span>");
			    elem.addClass("is_best");
             }  
             else if(json.voting!=1){
				  $("#loginsign_form").modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });
				 }
		}
	});
	e.preventDefault();
	e.stopPropagation();
 });
  
// Voting worst for a reply
  $(".worst > .worst-reply-btn").live('click',function(e){
  var srcTarget = e.target;
  if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }  
  var elem = $(srcTarget);
  var reply_id = elem.attr('rel');
  var tid = $('#tid').val();
  var thid = $('#thid').val();
  jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=reply&act=vote-addreply&tid='+ tid +'&thid='+ thid +'&rid='+ reply_id +'&worst=1',
	   cache: false,
	   success: function(reponse){
	   	eval('var json ='+reponse);
		     if(json.failure==false) // VOTE OK
             {  
                elem.html('');
                elem.html(json.worst_num + "<span>dislike</span>");
			    elem.addClass("is_worst");
             }  
             else if(json.voting!=1){
				  $("#loginsign_form").modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });
				 }
		}
	});
	e.preventDefault();
	e.stopPropagation();
 });

// Voting best for a thought
  $(".best-btn").live('click',function(e){
  var srcTarget = e.target;
  if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }  
  var elem = $(srcTarget);
  var thought_id = elem.attr('rel');
  var tid = $('.topic_id').text();
  jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=topic&act=vote-add&tid='+ tid +'&thid='+ thought_id +'&best=1',
	   cache: false,
	   success: function(reponse){
	   	eval('var json ='+reponse);
		     if(json.failure==false) // VOTE OK
             {  
			    elem.html('');
                elem.html(json.best_num + "<span>like</span>");
			    elem.addClass("is_best");
				
             } 
			 else if(json.voting!=1){
				  $("#loginsign_form").modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });
				 }
           }
	
	});
    
	e.preventDefault();
	e.stopPropagation();
 });	
  
// Voting worst for a thought
  $(".worst-btn").live('click',function(e){
  var srcTarget = e.target;
  if(srcTarget.tagName.toLowerCase() == 'span'){
	  srcTarget = srcTarget.parentNode;
  }  
  var elem = $(srcTarget);
  var thought_id = elem.attr('rel');
  
  var tid = $('.topic_id').text();
  jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=topic&act=vote-add&tid='+ tid +'&thid='+ thought_id +'&worst=1',
	   cache: false,
	   success: function(reponse){
	   	eval('var json ='+reponse);
		     if(json.failure==false) // VOTE OK
             {  
                 
				elem.html('');
                elem.html(json.worst_num + "<span>dislike</span>");
			    elem.addClass("is_worst");
				 
             }  
             else if(json.voting!=1){
				  $("#loginsign_form").modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });
				 }
		
		 }
	});
	e.preventDefault();
	e.stopPropagation();
 });  
  

// Launch MODAL BOX if the Login/Signin Link is clicked
$(".logsign_link").click(function(){
$('object').css("visibility","hidden");
$('embed').css("visibility","hidden");
$.modal.close();
$('#login_url').attr("value",location.href);
$('#signup_url').attr("value",location.href);

$('#status').css("display","block");
$('#status_anonymous').css("display","none");

$('#loginsign_form').modal({'onClose': function(){ 
										$('object').css('visibility','visible');
										$('embed').css('visibility','visible');
										$.modal.close();
									}
						   });

});

// Launch MODAL BOX if the report Link is clicked
$(".report").live('click',function(e){
	//pas 1 zice ca loading
	//pas 2 cand so incarcat il aratam
	//pas 3 there is no pass 3 :D
	if($('#report-form').lenght){
		$('#report-form').remove();
	}
	
$('object').css("visibility","hidden");
$('embed').css("visibility","hidden");

var thid = $(e.target).attr('rel');


jQuery.ajax({  
   type: "POST",
   url: "index_ajax.php",
   data: 'pag=xreport&thid='+thid,
   cache: false,
   success: function(reponse){
   		//debugger;
		eval('var json ='+reponse);
		if (json.failure) {
			$.modal.close();
			return;			
		}
		$('body').append($('<div>').attr('innerHTML',json.innerHTML).find('#report-form'));
		$.modal.close(); 
		$('#report-form').modal();	
		
		}
	
});  		

});

// When the report  form is submitted
$("#submit_report").live('click',function(e) {  
 
// Hide 'Submit' Button
$('#submit_report').hide();

// Show Gif Spinning Rotator
$('#ajax_loading_report').show();
// -- Start AJAX Call --
jQuery.ajax({  
       type: "POST",
	   url: "index_ajax.php",
	   data: $('#report').serialize(),
	   cache: false,
	   success: function(msg_report){
	   	eval('var json ='+msg_report);
		     if(json.failure==false) //  OK?
             {  
               
				$('#simplemodal-container').css("width","487px");
                $('#simplemodal-container').css("height","330px");
                $('#report-form').html( '<div>' + '<div class="top"></div>'+
	                               '<div class="middle clearfix" style="background:#fff none;padding-top:10px;">' + 
	                               "<div class='success' style='margin:0px;'>Thanks! An email has been sent to admin with your request.</div></div>" + 
	                               '<div class="bottom"></div>');
				// After 1 seconds redirect the 
                
			  }  
              else // ERROR?
               {   
                $('#notification_error_report').html(json.error);
				$('#ajax_loading_report').hide();
			   }  
		
		 }
  });  
 
    
    $('#submit_report').show();
  
// -- End AJAX Call --

return false;

}); // end submit event

// show forgot box
$("#forgot_link").click(function(){
$('#status').css("display","none");
$('#forgot_status').css("display","block");
			
});
// When the forgot  form is submitted
$("#submit_forgot").click(function() {  
$('#forgot .button').hide();
$('#forgot .ajax_loading').show();
// -- Start AJAX Call --
jQuery.ajax({  
       type: "POST",
	   url: "index_ajax.php",
	   data: $('#forgot').serialize(),
	   cache: false,
	   success: function(msg_forgot){
	   	eval('var json ='+msg_forgot);
		     if(json.failure==false) // Details OK?
             {  
                $('#simplemodal-container').css("width","487px");
                $('#simplemodal-container').css("height","330px");
                $('#forgot_status').html( '<div>' + '<div class="top"></div>'+
	                               '<div class="middle clearfix" style="background:#fff none;padding-top:10px;">' + 
	                               "<div class='success' style='margin:0px;'>Thanks! An email has been sent to your email address with instructions to reset your password.</div></div>" + 
	                               '<div class="bottom"></div>');
	         }  
              else // ERROR?
               {   
                $('#notification_error_forgot').html(json.error);
				
			   }  
			  }
  });  
   $('#forgot .ajax_loading').hide();
   $('#forgot .button').show();

  
// -- End AJAX Call --

return false;

}); // end submit event




// When the login  form is submitted
$("#submit_login").click(function(){  

// Hide 'Submit' Button
$('#login .button').hide();
// Show Gif Spinning Rotator
$('#login .ajax_loading').show();

// -- Start AJAX Call --
jQuery.ajax({  
       type: "POST",
	   url: "index_ajax.php",
	   data: $('#login').serialize(),
	   cache: false,
	   success: function(msg_login){
	   	eval('var json ='+msg_login);
		     if(json.failure==false) // LOGIN OK?
             {  
                $('#simplemodal-container').css("width","487px");
                $('#simplemodal-container').css("height","330px");
                $('#status').html( '<div>' + '<div class="top"></div>'+
	                               '<div class="middle clearfix" style="background:#fff none;padding-top:10px;">' + 
	                               "<div class='success' style='margin:0px;'>You are successfully logged in! <br /> Please wait while you're redirected...</div></div>" + 
	                               '<div class="bottom"></div>');
				
				// After 1 seconds redirect the 
				var url = json.url;
                setTimeout(function(){
							go_to_private_page(url);
				}, 1500);  
              }  
              else // ERROR?
               {   
                $('#notification_error').html(json.error);
				$('#ajax_loading_login').hide();
			   }  
		
		 }
  }); 
 // Hide Gif Spinning Rotator
$('#login .ajax_loading').hide();  
 // Show 'Submit' Button
$('#login .button').show();

  
// -- End AJAX Call --

return false;

}); // end login event

 


// When the anonymous  form is submitted
$("#anonymous").submit(function(){  
// Hide 'Submit' Button
$('#submit_anonymous').hide();
// Show Gif Spinning Rotator
$('#ajax_loading_anonymous').show();

// -- Start AJAX Call --
jQuery.ajax({  
       type: "POST",
	   url: "index_ajax.php",
	   data: $('#anonymous').serialize(),
	   cache: false,
	   success: function(msg_anonymous){
	   	eval('var json ='+msg_anonymous);
		     if(json.failure==false) // Details OK?
             {  
               
				$('#simplemodal-container').css("width","487px");
                $('#simplemodal-container').css("height","330px");
                $('#anonymous_form').html( '<div>' + '<div class="top"></div>'+
	                               '<div class="middle clearfix" style="background:#fff none;padding-top:10px;">' + 
	                               "<div class='success' style='margin:0px;'>Thanks! Please sign up to avoid repeating this step.</div></div>" + 
	                               '<div class="bottom"></div>');
				// After 1 seconds redirect the 
                setTimeout('go_to_private_page()', 1000); 
			  }  
              else // ERROR?
               {   
                $('#seccode_response_anonymous').html(json.seccode_error);
				$('#ajax_loading_anonymous').hide();
			   }  
		
		 }
  });
  
  
    
    $('#submit_anonymous').show();
  
// -- End AJAX Call --
return false;

}); // end submit event





// When the signup  form is submitted
$("#submit_signup").click(function(){  
// Hide 'Submit' Button
$('#signup .button').hide();
// Show Gif Spinning Rotator
$('#signup .ajax_loading').show();

// -- Start AJAX Call --
jQuery.ajax({  
       type: "POST",
	   url: "index_ajax.php",
	   data: $('#signup').serialize(),
	   cache: false,
	   success: function(msg){
	   	eval('var json ='+msg);
		     if(json.member_id) // LOGIN OK?
             {  
                
				
				$('#simplemodal-container').css("width","487px");
                $('#simplemodal-container').css("height","330px");
                $('#status').html( '<div>' + '<div class="top"></div>'+
	                               '<div class="middle clearfix" style="background:#fff none;padding-top:10px;">' + 
	                               "<div class='success' style='margin:0px;'>Thank you for registering! <br /> Please wait while you're redirected...</div></div>" + 
	                               '<div class="bottom"></div>');
				
				// After 1 seconds redirect the 
               var url = json.url;
                setTimeout(function(){
							go_to_private_page(url);
				}, 1500); 
              }  
              else // ERROR?
               {  
			    if(json.error){
				$('#signup_response').html(json.error);
			    }else{
			    $('#signup_response').html('');
		 	    }
                if(json.username_error){
				$('#username_response').html(json.username_error);
			    }else{
			    $('#username_response').html('');
		 	    }
			    if(json.email_error){
			    $('#email_response').html(json.email_error);
			    }else{
			    $('#email_response').html('');
			    }
			    if(json.password_error ){
			    $('#password_response').html(json.password_error);
			    }else{
			    $('#password_response').html('');
			    }   
				if(json.seccode_error ){
			    $('#seccode_response').html(json.seccode_error);
			    }else{
			    $('#seccode_response').html('');
			    }    
				
              }  
		
		 }
  });  
  
  
    
// Hide Gif Spinning Rotator
$('#signup .ajax_loading').hide();  
 // Show 'Submit' Button
$('#signup .button').show();
  
// -- End AJAX Call --
return false;

}); // end submit event
});








function go_to_private_page(page){
	if(!page){page='index.php?log=1'}
	window.location = page ; // Log
}

function check(){	
var username = $('#username').val();
var email = $('#email').val();
var password = $('#password').val();
var seccode = $('#captcha').val();

	jQuery.ajax({
	   type: "POST",
	   url: "index_ajax.php",
	   data: 'pag=login_box&act=member-check&username='+ username +'&email='+email +'&password='+password+'&seccode='+seccode,
	   cache: false,
	   success: function(response){
	   	eval('var json ='+response);
		    if(json.username_error){
				
			$('#username_response').html(json.username_error);
			}else{
			$('#username_response').html('');
			}
			if(json.email_error){
			  
			  $('#email_response').html(json.email_error);
			 }else{
			 $('#email_response').html('');
			 }
			if(json.seccode_error && seccode){
				
			$('#seccode_response').html(json.seccode_error);
			}else{
			$('#seccode_response').html('');
			} 
			if(json.password_error && password){
			 $('#password_response').html(json.password_error);
			 }else{
			 $('#password_response').html('');
			 } 
			
	        
		}
	});
}



 

