// OnReady
$(document).ready(function(){
	// Set click handler for search box
	$("#searchbox").click(function(){ $("#searchbox").val(""); });
	
	if($.fn.comments){
	    $(".jquerycomments").each(function(){
		    var tit = $(this).attr("title");
		    $(this).attr("title","");
		    typandid = tit.split(",");
		    var typ = typandid[0];
		    var idd = typandid[1];
		    $(this).comments({
			    type:typ,
				id:idd
				});
		});
	}

	// like link ajax
	updateLikeLinks();

	// Generate selectors
	$(".selectFill").each(function(){
		var start =    ($(this).attr('class').match(/a\d+/) + "").match(/\d+/);
		var end =      ($(this).attr('class').match(/b\d+/) + "").match(/\d+/);
		var skip =     ($(this).attr('class').match(/skip\d+/) + "").match(/\d+/);
	        var selected = ($(this).attr('class').match(/s\d+/) + "").match(/\d+/);
		$(this).html(genSelectHtml(start,end,skip,selected));
	    });
	$(".select0_200_0").html(genSelectHtml(0,200,1,0));
	$(".select0_200_200").html(genSelectHtml(0,200,1,200));
	$(".select0_100_0").html(genSelectHtml(0,100,1,0));
	$(".select0_100_100").html(genSelectHtml(0,100,1,100));

	// Change non-loaded pattern images to a notification image
	$(".pattern").each(function(){
		$(this).error(function(){
			$(this).attr("src","im/gen/inqueue.png");
		    });
	    });
	// Also for mini ones
	$(".pattern_mini").each(function(){
		$(this).error(function(){
			$(this).attr("src","im/gen/inqueue_mini.png");
		    });
	    });

	// Change non-loaded pattern images to a notification image
	$(".alpha_pattern").each(function(){
		$(this).error(function(){
			$(this).attr("src","im/gen/inqueue.png");
		    });
	    });
	// Also for mini ones
	$(".alpha_pattern_mini").each(function(){
		$(this).error(function(){
			$(this).attr("src","im/gen/inqueue_mini.png");
		    });
	    });

	if($.fn.rate!=undefined){
	    $(".rate").each(function(){
		    var type_and_id = $(this).attr("title").match(/\d+/g);
		    $(this).attr("title","");
		    var type = type_and_id[0];
		    var id = type_and_id[1];
		    $(this).rate({id:id,type:type});
		});
	}
	
	if($.fn.scrollFollow!=undefined){
	    $("#ads").scrollFollow({delay:200,  speed:1000});
	}


	if($.fn.thumbnail){
	    $(".gallery_table img").thumbnail({originalPath:"im/gal/n"});
	    $(".forum_table .ppic").thumbnail({originalPath:"im/ppic"});
	}
    });

function genSelectHtml(start,end,skip,selected){
    var selectContent = "";
    var sel = "";
    for(var i=start; i<=end; i++){
	if(i==selected) sel=" selected";
	else sel ="";
	selectContent = selectContent + "<option" + sel + ">" + i + "</option>";
    }
    return selectContent;
}

function updateLikeLinks(){
    $(".like").unbind('click');
    $(".like").click(function(){
	    var clicked = $(this);
	    var url = clicked.attr("href");
	    clicked.attr("href","javascript:return true;");
	    clicked.html("Loading...");
	    var target_id = (url.match(/comment_like=\d+/)+'').match(/\d+/);
	    var target_type = (url.match(/comment_like_type=\d+/)+'').match(/\d+/);
	    $.get("ajax/like.php?id="+target_id+"&type="+target_type,function(data){
		    if(data==1){
			clicked.parent().html('You like this. <a href="comment_unlike=' + target_id + '&comment_unlike_type=' + target_type + '" class="unlike">Unlike</a>');
			updateLikeLinks();
		    }
		});
	});

    $(".unlike").unbind('click');
    $(".unlike").click(function(){
	    var clicked = $(this);
	    var url = clicked.attr("href");
	    clicked.attr("href","javascript:return true;");
	    clicked.html("Loading...");
	    var target_id = (url.match(/comment_unlike=\d+/)+'').match(/\d+/);
	    var target_type = (url.match(/comment_unlike_type=\d+/)+'').match(/\d+/);
	    $.get("ajax/unlike.php?id="+target_id+"&type="+target_type,function(data){
		    if(data==1){
			clicked.parent().html('You don\'t like this. <a href="comment_like=' + target_id + '&comment_like_type=' + target_type + '" class="like">Like</a>');
			updateLikeLinks();
		    } else {
			alert('Error updating like.');
		    }
		});
	});
}

function fillSelectWithNumbers(sel,start,end){
    alert("jaj");
    sel.html('<option>LAL</option>')
}

function formfocus(){
    var log = document.getElementById('loginform');
    if(log){
	log.focus();
    }
}
window.onload = formfocus;
function checkUncheckBox(box){
    if(box.checked==true){
	box.checked=false;
    } else {
	box.checked=true;
    }
}
function rescale(im,mw,mh){
    if(im.width > mw){
	var w = im.width;
	var h = im.height;
	im.height = mw * h / w;
	im.width = mw;
    }
    if(im.height > mh){
	var w = im.width;
	var h = im.height;
	im.width = mh * w / h;
	im.height = mh;
    }
}
