$(document).ready(function(){
	$("#searchinput").autocomplete({
    source: "scripts/search.php",
  	minLength: 4
  });
	$("#upload_form").validate({
		rules: {
			gender:{
      	required: true,
      	rangelength: [1, 6]
    	}
    }
	});
	
	$(".convert").each( function(){
		var calc = $(this).text();
		splitnumb = calc.split("*");
		var convNumb =  (splitnumb[1]*splitnumb[0])*10;
		convNumb = (Math.round(convNumb)) / 10;
		$(this).text(convNumb);
	});
	$(".sort_item h2, .sort_cursor, .sort_left h2").css({"cursor": "s-resize"});
	$(".sortable").sortable({
		items: '.sort_item',
		axis: 'y',
		revert: true,
		handle: 'h2, div.sort_item .colhead',
		placeholder: 'ui-placeholder ui-corner-all',
		forcePlaceholderSize: true,
		update: function(){
			var sort_order = $(".sortable").sortable('toArray');
			var sort_orderNEW = "";
			for (var i in sort_order){
				sort_orderNEW += sort_order[i].replace("item_", "")+"_";
			}
			$.cookie('ui-sortable', sort_orderNEW, { expires: 366, path: '/', domain: 'cyclingtorrents.nl'});
			$.cookie('ui-sortable', sort_orderNEW, { expires: 366, path: '/', domain: 'www.cyclingtorrents.nl'});
		}
	});
	$(".sortable_left").sortable({
		items: '.sort_left',
		axis: 'y',
		revert: true,
		handle: 'h2',
		placeholder: 'ui-placeholderL ui-corner-all',
		forcePlaceholderSize: true,
		update: function(){
				var sort_left = $(".sortable_left").sortable('toArray');
				var sort_leftNEW = "";
				for (var i in sort_left){
					sort_leftNEW += sort_left[i].replace("itemL_", "")+"_";
				}
				$.cookie('ui-sortableLeft', sort_leftNEW, { expires: 366, path: '/', domain: 'cyclingtorrents.nl'});
				$.cookie('ui-sortableLeft', sort_leftNEW, { expires: 366, path: '/', domain: 'www.cyclingtorrents.nl'});
		}
	});
	var sort_cookie = $.cookie('ui-sortable');	
	if(sort_cookie){
		sort_cookie = sort_cookie.split("_");
		var sort_html = "";
		//alert(sort_cookie.length);
		for (var b = 0; b < (sort_cookie.length - 1); b++){
			if(b==0){
			}
			else{
				sort_html = $("#item_"+sort_cookie[b]).html();
				//alert(sort_html);
				$("#item_"+sort_cookie[b]).remove();
				//alert(sort_html);
				$(".sortable").append("<div  class='sort_item' id='item_"+sort_cookie[b]+"'>"+sort_html+"</div>");
			}
		}
	}
	
	var sortL_cookie = $.cookie('ui-sortableLeft');	
	if(sortL_cookie){
		sortL_cookie = sortL_cookie.split("_");
		var sortL_html = "";
		//alert(sort_cookie.length);
		for (var b = 0; b < (sortL_cookie.length - 1); b++){
			if(b==0){
			}
			else{
				sortL_html = $("#itemL_"+sortL_cookie[b]).html();
				//alert(sort_html);
				$("#itemL_"+sortL_cookie[b]).remove();
				//alert(sort_html);
				$(".sortable_left").append("<div class='sort_left' id='itemL_"+sortL_cookie[b]+"'>"+sortL_html+"</div>");
			}
		}
	}
	
	$("#username[type=text]").focus();
	

	$("h2.poll").prepend( "<span class='close ui-icon ui-icon-closethick' id='pollC' style='float:right; cursor:pointer;'></span><span class='toggle ui-icon ui-icon-minusthick' id='poll' style='float:right; cursor:pointer;'></span>")
	$("h2.shoutbox").prepend( "<span class='close ui-icon ui-icon-closethick' id='shoutboxC' style='float:right; cursor:pointer;' ></span><span class='toggle ui-icon ui-icon-minusthick' id='shoutbox' style='float:right; cursor:pointer;' ></span><span class='ui-icon ui-icon-gripsmall-diagonal-se' id='shoutboxALL' style='float:right; cursor:pointer;' ></span>")

	$("h2.torr").prepend( "<span class='close ui-icon ui-icon-closethick' id='torrC' style='float:right; cursor:pointer;'></span><span class='toggle ui-icon ui-icon-minusthick' id='torr' style='float:right; cursor:pointer;'></span>")
	$("h2.request").prepend( "<span class='close ui-icon ui-icon-closethick' id='requestC' style='float:right; cursor:pointer;'></span><span class='toggle ui-icon ui-icon-minusthick' id='request' style='float:right; cursor:pointer;'></span>")
	
	if($.cookie('ui-shoutbox') == "hide"){$( "table#shoutbox_tbl" ).hide();	$("span#shoutbox").addClass("ui-icon-plusthick").removeClass("ui-icon-minusthick");	$("h2.shoutbox").addClass("ui-corner-all").removeClass("ui-corner-top");}
	else if($.cookie('ui-shoutbox') == "show"){	$( "table#shoutbox_tbl" ).show();	$("span#shoutbox").addClass("ui-icon-minusthick").removeClass("ui-icon-plusthick");	$("h2.shoutbox").addClass("ui-corner-top").removeClass("ui-corner-all");}
	else if($.cookie('ui-shoutbox') == "close"){	$("h2.shoutbox, table#shoutbox_tbl").hide(); }
	if($.cookie('ui-poll') == "hide"){$( "table#poll_tbl" ).hide();	$("span#poll").addClass("ui-icon-plusthick").removeClass("ui-icon-minusthick");	$("h2.poll").addClass("ui-corner-all").removeClass("ui-corner-top");}
	else if($.cookie('ui-poll') == "show"){	$( "table#poll_tbl" ).show();	$("span#poll").addClass("ui-icon-minusthick").removeClass("ui-icon-plusthick");	$("h2.poll").addClass("ui-corner-top").removeClass("ui-corner-all");}
	else if($.cookie('ui-poll') == "close"){	$("h2.poll, table#poll_tbl").hide(); }
	
	if($.cookie('ui-torr') == "hide"){
		$( "table#torr_tbl" ).hide();
		$("span#torr").addClass("ui-icon-plusthick").removeClass("ui-icon-minusthick");
		$("h2.torr").addClass("ui-corner-all").removeClass("ui-corner-top");
	}
	else if($.cookie('ui-torr') == "show"){
		$( "table#torr_tbl" ).show();
		$("span#torr").addClass("ui-icon-minusthick").removeClass("ui-icon-plusthick");
		$("h2.torr").addClass("ui-corner-top").removeClass("ui-corner-all");
	}
	else if($.cookie('ui-torr') == "close"){
		$("h2.torr, table#torr_tbl").hide();
	}
	
	if($.cookie('ui-request') == "hide"){
		$( "table#request_tbl" ).hide();
		$("span#request").addClass("ui-icon-plusthick").removeClass("ui-icon-minusthick");
		$("h2.request").addClass("ui-corner-all").removeClass("ui-corner-top");
	}
	else if($.cookie('ui-request') == "show"){
		$( "table#request_tbl" ).show();
		$("span#request").addClass("ui-icon-minusthick").removeClass("ui-icon-plusthick");
		$("h2.request").addClass("ui-corner-top").removeClass("ui-corner-all");
	}
	else if($.cookie('ui-request') == "close"){
		$("h2.request, table#request_tbl").hide();
	}
	
	$("span.toggle").click(function() {
		var ico_id = $( this ).attr("id");		
		$( this ).toggleClass( "ui-icon-minusthick" ).toggleClass( "ui-icon-plusthick" );
		$( "table#"+ico_id+"_tbl" ).toggle();
		if($( this ).attr("class") == "toggle ui-icon ui-icon-plusthick"){
			var cookie_val = "hide";
			$("h2."+ico_id).toggleClass( "ui-corner-all" ).toggleClass( "ui-corner-top" );
		}
		else if($( this ).attr("class") == "toggle ui-icon ui-icon-minusthick"){
			var cookie_val = "show";
			$("h2."+ico_id).toggleClass( "ui-corner-top" ).toggleClass( "ui-corner-all" );
		}
		$.cookie('ui-'+ico_id, cookie_val, { expires: 366});
	});
	$("span.close").click(function() {
		var ico_id = $( this ).attr("id");
		if(ico_id == "pollC") ico_id = "poll";
		else if(ico_id == "shoutboxC") ico_id = "shoutbox";
		else if(ico_id == "torrC") ico_id = "torr";
		else if(ico_id == "requestC") ico_id = "request";
		$("h2."+ico_id+", table#"+ico_id+"_tbl").hide();
		$.cookie('ui-'+ico_id, "close", { expires: 366});
	});
	$("span#shoutboxALL").click(function() {
		var shoutALL = $.cookie('ui-shoutALL');
		var shoutALL_val = "";
		if(shoutALL == 1){
			shoutALL_val = 0;
		}
		else{
			shoutALL_val = 1;
		}
		$.cookie('ui-shoutALL', shoutALL_val, { expires: 366});
		window.location.reload();
	});
	$(".spoiler_btn").toggle(function() {
  	$(this).val("Hide spoiler");
	}, function() {
  	$(this).val("Show spoiler");
	});
  $("ul.ui-autocomplete li:empty").hide();
});

