jQuery("document").ready(function(){
	// register ajax gallery-navigation listeners
/*
	jQuery("a.page-numbers").click(function(e) {
		return ngg_ajax_navigation(e, this);
	});
	jQuery("a.prev").click(function(e) {
		return ngg_ajax_navigation(e, this);
	});
	jQuery("a.next").click(function(e) {
		return ngg_ajax_navigation(e, this);
	});
	
	// register ajax browser-navigation listeners
	jQuery("a.ngg-browser-next").click(function(e) {
		return ngg_ajax_browser_navigation(e, this);
	});
	jQuery("a.ngg-browser-prev").click(function(e) {
		return ngg_ajax_browser_navigation(e, this);
	});
*/

	jQuery("a.star1").mouseenter(function(e) {
		return ngg_star_rating(e, this, 'enter');
	});
	jQuery("a.star2").mouseenter(function(e) {
		return ngg_star_rating(e, this, 'enter');
	});
	jQuery("a.star3").mouseenter(function(e) {
		return ngg_star_rating(e, this, 'enter');
	});
	jQuery("a.star4").mouseenter(function(e) {
		return ngg_star_rating(e, this, 'enter');
	});
	jQuery("a.star5").mouseenter(function(e) {
		return ngg_star_rating(e, this, 'enter');
	});

	jQuery("a.star1").mouseleave(function(e) {
		return ngg_star_rating(e, this, 'leave');
	});
	jQuery("a.star2").mouseleave(function(e) {
		return ngg_star_rating(e, this, 'leave');
	});
	jQuery("a.star3").mouseleave(function(e) {
		return ngg_star_rating(e, this, 'leave');
	});
	jQuery("a.star4").mouseleave(function(e) {
		return ngg_star_rating(e, this, 'leave');
	});
	jQuery("a.star5").mouseleave(function(e) {
		return ngg_star_rating(e, this, 'leave');
	});


	jQuery("a.star1").click(function(e) {
		return ngg_post_rating(e, this);
	});
	jQuery("a.star2").click(function(e) {
		return ngg_post_rating(e, this);
	});
	jQuery("a.star3").click(function(e) {
		return ngg_post_rating(e, this);
	});
	jQuery("a.star4").click(function(e) {
		return ngg_post_rating(e, this);
	});
	jQuery("a.star5").click(function(e) {
		return ngg_post_rating(e, this);
	});

	jQuery("#butt").click(function(e) {
		return ngg_post_comment(e, this);
	});

	jQuery(".resolution").change(function(e) {
		return ngg_download(e, this);
	});

	jQuery("#order-by #main-button").click(function(e) {
		return ngg_show_sort_menu(e, this);
	});

	jQuery(".delete-link").click(function(e) {
		return ngg_remove_comment(e, this);
	});
	
  ngg_ajax.alreadyVoted = eval('('+ngg_ajax.alreadyVoted.replace(/&quot;/g,'"')+')');
	ngg_ajax.voted = [];
});


function ngg_show_sort_menu(e, obj) {
  if (jQuery("#order-by #order-menu").css('display') == 'none')
    jQuery("#order-by #order-menu").slideDown("slow");
  else
    jQuery("#order-by #order-menu").slideUp("slow");
  return false;
}

function ngg_download(e, obj) {
  var ar = obj.id.split('-');
  if (this.value == '')
  {
    alert('Please choose the image resolution first.');
    return false;
  }
  else {
    document.getElementById('download-form').target = '_blank'; 
    document.getElementById('download-form').action = ngg_ajax.path + 'nggajax.php?did='+ar[0]+'&type=down&res=' + document.getElementById(ar[0]+'-resolution').value; 
    document.getElementById('download-form').submit(); 
  }
}


function ngg_set_comments(data) {
  var s = '';
  var extra = ngg_ajax.isAdmin ? '<a href="#" id="rep" class="delete-link">[x]</a>' : '';
  for (var i=0; i<data.comments_count; i++)
  {
    var e2 = extra.replace(/rep/,('del-'+data.comments[i].id+'-'+data.pid));
    s += '<div class="comments-item">' +
         '<div class="comments-top">' +
         '<div class="comments-h">Posted By:&nbsp;</div><div class="comments-user"><a href="mailto:'+data.comments[i].email+'">'+data.comments[i].nick+'</a></div>'+e2+'<div class="comments-date">'+data.comments[i].date+'</div>' +
    		 '<div style="clear: both"></div>'+
         '</div>'+
  		   '<img class="comments-image" src="'+ngg_ajax.path+'images/user.png" alt="" />'+
         '<div class="comments-message">'+data.comments[i].comment+'</div>'+
  		   '<div style="clear: both"></div>'+
         '</div>';   
  }
  jQuery('#comments-content').html(s);
	jQuery(".delete-link").unbind("click");
	jQuery(".delete-link").click(function(e) {
    return ngg_remove_comment(e, this);
	});
}

function ngg_remove_comment(e, obj) {
  var ar = obj.id.split('-');
  jQuery.post(ngg_ajax.path + "nggajax.php", 'remcomment=1&id=' + ar[1] + '&pid=' + ar[2] + '&' + ngg_ajax.sid, 
    function(data){
      data = eval('('+data+')');
      if (data.result > 0)
      { 
        ngg_set_comments(data);
      }
    }
  );
  return false;
}

function ngg_post_comment(e, obj) {
  obj.disabled = true;
  jQuery('#comments-return-message').html('');
  jQuery.post(ngg_ajax.path + "nggajax.php", jQuery("#comments-form").serialize() + '&' + ngg_ajax.sid, 
    function(data){
      obj.disabled = false;
      data = eval('('+data+')');
      if (data.result > 0)
      { 
        jQuery('#comments-form :input').not(':button, :submit, :reset, :hidden').val('');
        ngg_set_comments(data);
      }
      document.getElementById('code').src = ngg_ajax.path + 'captcha/captcha.php?id=' + Math.random() + '&' + ngg_ajax.sid;
      jQuery('#comments-return-message').html(data.message);
    }
  );
}


function ngg_post_rating(e, obj) {
  var ar = obj.id.split('-');
  if (obj.disabled || ngg_ajax.voted[ar[0]] || ngg_ajax.alreadyVoted[ar[0]]) return false;
  obj.disabled = true;
  jQuery.post(ngg_ajax.path + "nggajax.php", 'rating='+ar[1]+'&pid='+ar[0], 
    function(data){
      obj.disabled = false;
      data = eval('('+data+')');
      if (data.result > 0)
      { 
        document.getElementById(ar[0]+'-6').innerHTML = 'thanks for rating!';  
        for (i=1;i<=5;i++)
        {
          if (i<=ar[1])
            document.getElementById(ar[0]+'-'+i).style.backgroundPosition = "-34px 0";
          else
            document.getElementById(ar[0]+'-'+i).style.backgroundPosition = "-17px 0";
          //document.getElementById(ar[0]+'-'+i).style.backgroundImage = "url("+ngg_ajax.path+"/images/astars2.gif)";
        }
        ngg_ajax.voted[ar[0]] = true;   
      }
    }
  );
  return false;
}

function ngg_star_rating(e, obj, action) {
  var ar = obj.id.split('-');
  //var message = ['','poor','nothing fancy','good','great','awesome!','you\'ve voted yet'];
  var message = ['','','','','','','already voted'];
  if (ngg_ajax.voted[ar[0]]) 
    return;

  if (action == 'enter')
  {
    omessage = document.getElementById(ar[0]+'-6').innerHTML; 
    if (ngg_ajax.alreadyVoted[ar[0]])
    {
      document.getElementById(ar[0]+'-6').innerHTML = message[6];
      return;
    }
    //obj.style.backgroundImage = "url("+ngg_ajax.path+"/images/astars2.gif)";
    //obj.style.backgroundPosition = "-33px 0";
    for (i=1;i<=ar[1];i++)
      //document.getElementById(ar[0]+'-'+i).style.backgroundImage = "url("+ngg_ajax.path+"/images/astars2.gif)"; 
      document.getElementById(ar[0]+'-'+i).style.backgroundPosition = "-34px 0";
    //document.getElementById(ar[0]+'-6').innerHTML = message[ar[1]];
  } else 
  if (action == 'leave')
  {
    if (ngg_ajax.alreadyVoted[ar[0]])
    {
      document.getElementById(ar[0]+'-6').innerHTML = omessage;  
      return;
    }
    //obj.style.backgroundImage = "url("+ngg_ajax.path+"/images/astars.gif)";  
    var rating = omessage.split(' /');
    //obj.style.backgroundPosition = "0 0";
    for (i=1;i<=ar[1];i++)
    {
      //document.getElementById(ar[0]+'-'+i).style.backgroundImage = "url("+ngg_ajax.path+"/images/astars.gif)";
      if (parseFloat(rating[0])>(i-0.5))
        document.getElementById(ar[0]+'-'+i).style.backgroundPosition = "-1px 0";
      else
        document.getElementById(ar[0]+'-'+i).style.backgroundPosition = "-17px 0";
    }
    document.getElementById(ar[0]+'-6').innerHTML = omessage;  
  }
}


function ngg_ajax_navigation(e, obj) {
	// try to find page number
	var pageNumber = 0;
	if (jQuery(obj).hasClass("page-numbers")) {
		pageNumber = jQuery(obj).contents()[0].data;
	} else if (jQuery(obj).hasClass("prev")) {
		pageNumber = jQuery(obj).attr("id").substr(9);
	} else if (jQuery(obj).hasClass("next")) {
		pageNumber = jQuery(obj).attr("id").substr(9);
	}
	
	// try to find gallery number by checking the parents ID until we find a matching one
	var currentNode = obj;
	while (null != currentNode.parentNode && !jQuery.nodeName(currentNode.parentNode, "body") && "ngg-gallery-" != jQuery(currentNode.parentNode).attr("id").substring(0, 12)) {
		currentNode = currentNode.parentNode;
	}
	
	if (jQuery(currentNode.parentNode).attr("id")) {
		var gallery = jQuery(currentNode.parentNode);

		// we found a gallery, let's extract the post id & gallery id
		var payload = gallery.attr("id").substring(12);
		var separatorPosition = parseInt(payload.indexOf("-"));
		
		var galleryId = payload.substr(0, separatorPosition);
		var postId = payload.substr(separatorPosition + 1);

		if ( (galleryId.length == 0) || (postId.length == 0) ) {
			return true;
		}
				
		ngg_show_loading(e);
		
		// load gallery content
		jQuery.get(ngg_ajax.path + "nggajax.php", {p: postId, galleryid: galleryId, nggpage: pageNumber, type: "gallery"}, function (data, textStatus) {
			
			// delete old content
			gallery.children().remove();
			
			// add new content
			gallery.replaceWith(data);
			
			// add ajax-navigation, again
			jQuery("document").ready(function(){
				// remove old listeners to avoid double-clicks
				jQuery("a.page-numbers").unbind("click");
				jQuery("a.prev").unbind("click");
				jQuery("a.next").unbind("click");
				
				// add shutter-listeners again
				shutterReloaded.init('sh');
				
				jQuery("a.page-numbers").click(function(e) {
					return ngg_ajax_navigation(e, this);
				});
				jQuery("a.prev").click(function(e) {
					return ngg_ajax_navigation(e, this);
				});
				jQuery("a.next").click(function(e) {
					return ngg_ajax_navigation(e, this);
				});
				
				ngg_remove_loading();
			});
		});
		
		// deactivate HTML link
		return false;
	}
	
	// an error occurred, use traditional HTML link
	return true;
};

function ngg_ajax_browser_navigation(e, obj) {
	

	// try to find gallery number
	if ("ngg-prev-" == jQuery(obj).attr("id").substr(0, 9) || "ngg-next-" == jQuery(obj).attr("id").substr(0, 9)) {
		
		// extract the image-id
		var imageNumber = jQuery(obj).attr("id").substr(9);

		// find the image-browser-container
		var currentNode = obj;
		while (null != currentNode.parentNode && !jQuery.nodeName(currentNode.parentNode, "body") && !jQuery(currentNode.parentNode).hasClass("ngg-imagebrowser")) {
			currentNode = currentNode.parentNode;
		}
		
		if (jQuery(currentNode.parentNode).hasClass("ngg-imagebrowser")) {
			var gallery = jQuery(currentNode.parentNode);

			// let's extract the post id & gallery id
			var payload = gallery.attr("id").substring(17);
			var separatorPosition = parseInt(payload.indexOf("-"));
			
			var galleryId = payload.substr(0, separatorPosition);
			var postId = payload.substr(separatorPosition + 1);
			
			if ( (galleryId.length == 0) || (postId.length == 0) ) {
				return true;
			}
			
			ngg_show_loading(e);
			
			// get content
			jQuery.get(ngg_ajax.path + "nggajax.php", {p: postId, galleryid: galleryId, pid: imageNumber, type: "browser"}, function (data, textStatus) {
				// delete old content
				gallery.children().remove();
				
				// add new content
				gallery.replaceWith(data);
				
				// add ajax-navigation, again
				jQuery("document").ready(function(){
					// remove old listeners to avoid double-clicks
					jQuery("a.ngg-browser-next").unbind("click");
					jQuery("a.ngg-browser-prev").unbind("click");
					
					// add shutter-listeners again
					shutterReloaded.init('sh');
					
					// register ajax browser-navigation listeners
					jQuery("a.ngg-browser-next").click(function(e) {
						return ngg_ajax_browser_navigation(e, this);
					});
					jQuery("a.ngg-browser-prev").click(function(e) {
						return ngg_ajax_browser_navigation(e, this);
					});
					
					ngg_remove_loading();
				});
			});
	
			// deactivate HTML link
			return false;
		}
	}
	
	return true;
}

var loadingImage;
function ngg_show_loading(obj) {
	loadingImage = jQuery(document.createElement("img")).attr("src", ngg_ajax.path + "images/ajax-loader.gif").attr("alt", ngg_ajax.loading);

	jQuery("body").append(loadingImage);
	
	jQuery(loadingImage).css({
		position: "absolute",
		top: (obj.pageY + 15) + "px",
		left: (obj.pageX + 15) + "px"
	});
	
	jQuery(document).mousemove(function(e) {
		loadingImage.css({
			top: (e.pageY + 15) + "px",
			left: (e.pageX + 15) + "px"
		});
	});
}

function ngg_remove_loading() {
	jQuery(document).unbind("mousemove");
	
	jQuery(loadingImage).remove();
}