function add_comment(id){
	if($('popup')){
		$('popup').remove();	
	}
	var div = new Element("div", { id: "popup" });
	var h2 = new Element("h2").update("Loading comment form");
	var loader = new Element("img", { id: "ajax_loader", src: "/images/ajax-loader.gif" });
	var close_popup = new Element("a", { 'class': "close", onclick: "close_popup();return false;", href: "#" }).update("close");
	div.appendChild(close_popup);
	div.appendChild(h2);
	div.appendChild(loader);
	$$('body')[0].appendChild(div);
	
	var scroll_height = document.viewport.getScrollOffsets().top;
	$('popup').setStyle({ top:scroll_height+200+"px", width:"300px", marginLeft:"-160px" });
	
	new Ajax.Request('/comment_form.php?q='+id, { method: 'get', onSuccess: function(elem){
			$('ajax_loader').remove();
			$$('#popup h2')[0].remove();
			
			var div = new Element("div").update(elem.responseText);
			$('popup').appendChild(div);													
		}
	});															
}

function get_friends(){
	if($('popup')){
		$('popup').remove();	
	}
	var div = new Element("div", { id: "popup" });
	var close_popup = new Element("a", { 'class': "close", onclick: "close_popup();return false;", href: "#" }).update("close");
	div.appendChild(close_popup);
	$$('body')[0].appendChild(div);
	
	var scroll_height = document.viewport.getScrollOffsets().top;
	$('popup').setStyle({ top:scroll_height+200+"px", width:"300px", marginLeft:"-160px" });
	
	new Ajax.Request('/friends.php', { method: 'get', onSuccess: function(elem){
			var div = new Element("div").update(elem.responseText);
			$('popup').appendChild(div);													
		}
	});															
}

function submit_comment(id){	
	new Ajax.Request('/comment_submit.php', {
      method: 'post',
      parameters: {
        'gift_id': id,
        'comment': $('comment_box').getValue()
      },

      onComplete: function(transport) {
        $('popup').update("<h2>Success!!</h2><p>Thanks for the support</p>");
		$('popup').fade({ duration:2.5 });
      }
    });
}

function close_popup(){
	if($('popup')){
		$('popup').remove();	
	}	
}

function want_this(id){
	new Ajax.Request('/add_gift.php', {
      method: 'post',
      parameters: {
        'id': id
      },
      onComplete: function(transport) {
        $$('#gift_'+id+' a.button')[0].replace("<strong>Added!</strong>");
      }
    });	
}

function remove_this(id){
	new Ajax.Request('/remove_gift.php', {
      method: 'post',
      parameters: {
        'id': id
      },
      onComplete: function(transport) {
        $$('#gift_'+id+' a.remove')[0].replace("removed");
      }
    });	
}

function delete_gift(id){
	$$('#gift_'+id+' a.delete')[0].replace("<a class='delete' href='#' onclick='delete_gift_confirm("+id+");return false;'>are you sure?</a>");
}

function delete_gift_confirm(id){
	new Ajax.Request('/delete_gift.php', {
      method: 'post',
      parameters: {
        'id': id
      },
      onComplete: function(transport) {
        $$('#gift_'+id+' a.delete')[0].replace("deleted!");
      }
    });	
}

function delete_comment(id){
	$('gift_comment_'+id).replace("<a id='gift_comment_"+id+"' class='delete' href='#' onclick='delete_comment_confirm("+id+");return false;'>are you sure?</a>");
}

function delete_comment_confirm(id){
	new Ajax.Request('/delete_comment.php', {
      method: 'post',
      parameters: {
        'id': id
      },
      onComplete: function(transport) {
        $('gift_comment_'+id).replace("deleted!");
      }
    });	
}

function show_list(elem){
	$$('#nav_list a.active')[0].removeClassName('active');
	$$('#list_items div.active')[0].removeClassName('active');
	$('nav_'+elem).addClassName('active');
	$$('#list_items div.'+elem)[0].addClassName('active');
	$('nav_'+elem).blur();
}

function subscribe(){
	new Ajax.Request('/email_subscribe.php', {
      method: 'post',
      parameters: {
        'email': $('email_updates').value
      },
      onComplete: function(transport) {
        $('subscribe_form').update("Thank you for subscribing.");
      }
    });	
}
