/*
	vgmTorrents JavaScript functions
*/

function vgmDeleteConfirm(el,tid,nonce) {
	jQuery('#tor-actions').fadeOut('200',function() {
		jQuery(el).html('(really delete? <a href="/tor/'+tid+'/" class="delete-tor-no">no</a> &mdash; <a href="/tor/'+tid+'/dodelete/?_wpnonce='+nonce+'">yes</a>)');
		jQuery('.delete-tor-no').click(function() { return vgmDeleteLink(el,tid,nonce); });

		jQuery('#tor-actions').fadeIn('200');
	});

	return false;
}

function vgmDeleteLink(el,tid,nonce) {
	jQuery('#tor-actions').fadeOut('200',function() {
		jQuery(el).html('(<a href="/tor/'+tid+'/delete/" class="delete-tor-confirm">delete</a>)');
		jQuery('.delete-tor-confirm').click(function() { return vgmDeleteConfirm(el,tid,nonce); });

		jQuery('#tor-actions').fadeIn('200');
	});

	return false;
}

function vgmToggleFileDetails(el,tid,details) {
	if(el.showing) {
		jQuery(details).slideUp("slow",function() {
			jQuery("#details-title").fadeOut(500,function() {
				jQuery(el).html('show file details');
				jQuery(details).attr("style","display: none");
				jQuery("#details-wrapper").attr("style","display: none");
				jQuery("#details-title").attr("style","display: none");

				el.showing=!el.showing;
			});
		});
	}
	else {
		jQuery(details).attr("style","display: block");
		jQuery.scrollTo("#details", 1000, { queue: true, onAfter: function() { vgmHandleShowing(el,tid,details); } });
	}

	return false;
}

function vgmHandleShowing(el,tid,details) {
	jQuery("#details-title").fadeIn(1500);

	jQuery("#details-actions").append(ajax);
	jQuery(ajax).fadeIn("500");

	jQuery("#details-wrapper").load(vgmConst.templateUrl+'/vgm-ajax.php',{action:'show_torrent_info',id:tid},
		function() {
			jQuery(ajax).fadeOut("1000");

			jQuery("#details-wrapper").slideDown("slow",function() {
				jQuery(el).html('hide file details');
				jQuery(el).attr("href","/tor/"+tid);
				jQuery(".hide-details").click(function() {
					return vgmHideFileDetails(el,"#details");
				});

				jQuery.scrollTo("#details", 500);

				el.showing=!el.showing;
			});
		}
	);
}

function vgmHideFileDetails(el,details) {
	jQuery("#details-wrapper").slideUp("slow",function() {
		jQuery(el).html('show file details');
		jQuery("#details-title").fadeOut(500,function() {
			jQuery(details).attr("style","display: none");
			jQuery("#details-wrapper").attr("style","display: none");
			jQuery("#details-title").attr("style","display: none");

			el.showing=!el.showing;
		});
	});

	return false;
}

function vgmToggleSearch(el) {
	var link=jQuery(el);

	if(el.text=="advanced search")
	{
		link.fadeOut("slow",function() {
			link.text("simple search").fadeIn("slow",function() {
				el.advanced=true;
			});
			jQuery("#browse-params-container").fadeIn("slow");
		});
	}
	else
	{
		link.fadeOut("slow",function() {
			link.text("advanced search").fadeIn("slow",function() {
				el.advanced=false;
			});
			jQuery("#browse-params-container").fadeOut("slow");
		});
	}

	return false;
}
