$(document).ready(function() {
  init();
});

function init(){
    imagePreview();

    var max_size = 220;
    $("#catalog img").each(function(i) {
      if ($(this).height() > $(this).width()) {
	    var h = max_size;
	    var w = Math.ceil($(this).width() / $(this).height() * max_size);
      } else {
	    var w = max_size;
	    var h = Math.ceil($(this).height() / $(this).width() * max_size);
      }
      $(this).css({ height: h, width: w });
    });

};

function spoilerChange(vThis){
    var vSel = vThis.options[vThis.selectedIndex].text;
    document.location = window.location.pathname +"?m="+ vSel;
};

this.imagePreview = function(){
	/* CONFIG */

		xOffset = 200;
		yOffset = 20;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result

	/* END CONFIG */
	$("#spoilerList .hover").hover(function(e){
		this.t = this.title;
		this.title = "";
        this.img = this.getAttribute("img");
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.img +"' alt='Image preview' />"+ c +"</p>");
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("75");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });

	$("#spoilerList .hover").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};
