
function loadmp3 (t_id,f_id) {
	var player_source = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="34" height="34"><param name="movie" value="'+config_site+'images/player.swf?ver=2.5" /><param name="quality" value="high" /><param name="FlashVars" value="torrent_id='+t_id+'&file_id='+f_id+'"><embed src="'+config_site+'images/player.swf?ver=2.5" FlashVars="torrent_id='+t_id+'&file_id='+f_id+'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="34" height="34" ></embed></object>';
	
	$(".bt_active").html ('');
	$(".bt_active").animate ({width:"16px",height:"16px"},500);
	$(".name_active").animate ({paddingTop:"0px"},1000);
	$(".bt_active").attr ('class','bt');
	$(".name_active").attr ('class','name');
	
	$("#mp3_loader_"+f_id+">.bt").attr ('class','bt_active');
	$("#mp3_loader_"+f_id+">.name").attr ('class','name_active');
	$("#mp3_loader_"+f_id+">.bt_active").animate ({width:"34px",height:"34px"},500);
	$("#mp3_loader_"+f_id+">.name_active").animate ({paddingTop:"9px"},1000);
	$("#mp3_loader_"+f_id+">.bt_active").html (player_source);

}

function toggle_elem (elem,par) {
	if ($(elem).css('display')=='none') $(elem).css('display','block'); else $(elem).css('display','none');
	$(par).toggleClass('open');
}

function toggle_tree (num) {
	$('#file_tree_'+num).slideToggle(200);
	$('#toggle_bt_'+num).toggleClass('folder_open');
}


function loadFile (id,parent,elem) {
	$.ajax({
		url: '/module/torrent/file.php',
		type: "POST",
		data: 'ajax=1&id='+id+'&parent='+parent,
		cache: false,
		beforeSend: function () {$(elem).html('<em>Loading....</em>');},
		success: function(html){
			$('#file_tree').html(html);
		}
	});
}



function toggle_tab (main,s1,s2,tab) {
	
	if ($(tab).hasClass('active')) return;
	
	if ($(main+' '+s1).css('display')=='none') 
	{$(main+' '+s2).css('display','none');$(main+'>'+s1).css('display','block');}
	  else 
	{$(main+' '+s2).css('display','block');$(main+'>'+s1).css('display','none');}
	
	$(main+' h4 a').each (function () {
		$(this).removeClass('active');
	});
	
	$(tab).addClass('active');
	
}

function tab_ui (target) {
	
		// When li link is clicked
		$(target+" li").click(function () {
			
			if ($(this).hasClass ('active')) return false;
			
			//Find displayed contentn
			var current_show = $(target+" li.active a").attr("title");
			
			// switch all tabs off
			$(target+" li.active").removeClass("active");
			
			// switch this tab on
			$(this).addClass("active");
			
			// slide all content up
			//$('#'+current_show+'_holder').slideUp();
			
			// slide this content down
			var new_show = $(target+" li.active a").attr("title");
			$('#'+current_show+'_holder').fadeOut ('fast',function () {$('#'+new_show+'_holder').fadeIn ('fast');});
		  
		  	return false;
		});
}

function tab_ui_link (target) {
	
		// When a link is clicked
		$(target+" a.tab").click(function () {
			
			if ($(this).hasClass ('active')) return false;
			
			// switch all tabs off
			$(target+" .active").removeClass("active");
			
			// switch this tab on
			$(this).addClass("active");
			
			// slide all content up
			$(target+" .content").slideUp();
			
			// slide this content up
			var content_show = $(this).attr("title");
			$(target+" ."+content_show).slideDown();
		  
		  	return false;
		});
}



function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function verticalCenter(group) {
	group.each(function() {
		$(this).load ( function () {
			var thisHeight = $(this).height();
			var parentHeight = $(this).parent().height();

			if(thisHeight<parentHeight) {
				$(this).css('marginTop',((parentHeight-thisHeight)/2)+'px');
			}
		});
	});
}


function headerfix2 () {
	
	if (typeof(search_keyword) != "undefined") {var box_key = search_keyword} else {var box_key = 'Type in your keyword here...';}
	
	var se_header_html="";
	se_header_html += "<ul class=\"cont\"><li class=\"menu\"><a href=\"\/\" class=\"menu_a\">Menu<\/a><div class=\"dropbox\"><div class=\"clmn\"><p>categories<\/p><a href=\"\/movies\/index.html\">Movies<\/a><a href=\"\/music\/index.html\">Music<\/a><a href=\"\/tv\/index.html\">Tv Shows<\/a><a href=\"\/apps\/index.html\">Applications<\/a><a href=\"\/games\/index.html\">Games<\/a><a href=\"\/books\/index.html\">Books<\/a><a href=\"\/videos\/index.html\">Videos<\/a><a href=\"\/animes\/index.html\">Animes<\/a><a href=\"\/others\/index.html\" rel=\"nofollow\">Others<\/a><\/div><div class=\"clmn\"><p>even more<\/p><a href=\"\/recent.html\">Most recent<\/a><a href=\"\/popular.html\">Top 100<\/a><a href=\"\/browse\/index.html\">Browse all<\/a><hr noshade=\"noshade\" size=\"1\" \/><a href=\"\/recent_search.html\">Latest searches<\/a><a href=\"\/cloud.html\">Search cloud<\/a><hr noshade=\"noshade\" size=\"1\" \/><a href=\"\/feeds\/index.html\" rel=\"nofollow\">Rss Feeds<\/a><a href=\"\/trends\/\">Trends<\/a><\/div><div class=\"clmn\" style=\"border:none\"><p>Fenopy<\/p><a href=\"\/tools.html\" rel=\"nofollow\">Tools<\/a><a href=\"http:\/\/blog.fenopy.eu\">Blog<\/a><a href=\"\/info\/faq.html\" rel=\"nofollow\">Help<\/a><\/div><\/div><\/li><li class=\"search\"><form class=\"search\" method=\"get\" action=\"\/\"><input type=\"text\" name=\"keyword\" class=\"k autocomplete\" value=\""+box_key+"\" alt=\"all\" onclick=\"if (this.value=='Type in your keyword here...') this.value=''\" id=\"header_box\"\/><button type=\"submit\" class=\"small\"><span>SEARCH<\/span><\/button><\/form><\/li><\/ul>";

	if($.browser.msie && $.browser.version<7) var msie6 = true;

	if (!msie6) {
		var top = 105;
		$(window).scroll(function (event) {
			// what the y position of the scroll is
			var y = $(this).scrollTop();
			
			// whether that's below the form
			if (y >= top) {
				// if so, Slide down
				$('#se_header').html(se_header_html);
				$('#se_header').slideDown(200);
			} else {
				// otherwise slide up
				$('#se_header').slideUp(200);
			}
		});
	} 
}



function add_social () {

	/*var social_html = '<div class="tt"><strong>Share</strong></div><div class="fb"><div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=207321302649861&amp;xfbml=1"></script><fb:like href="'+page_url+'" send="true" layout="button_count" width="30" show_faces="false" font="lucida grande"></fb:like></div> <div class="gp"><g:plusone size="medium" href="'+page_url+'"></g:plusone><script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script></div> <div class="tw"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="'+page_url+'" data-counturl="'+page_url+'">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div> <div class="su"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1"></script></div> <div class="rd"><script type="text/javascript" src="http://www.reddit.com/static/button/button1.js"></script></div>';	
	*/
	$('#share_box .gp').html('<g:plusone size="medium" href="'+(page_url)+'"></g:plusone><script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>');
	$('#share_box .tw').html('<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-url="'+encodeURIComponent(page_url)+'" data-counturl="'+encodeURIComponent(page_url)+'">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>');
	$('#share_box .su').html('<iframe src="http://www.stumbleupon.com/badge/embed/1/?url='+encodeURIComponent(page_url)+'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:74px; height: 18px;" allowTransparency="true"></iframe>');
	$('#share_box .rd').html('<iframe src="http://www.reddit.com/static/button/button1.html?width=120&amp;url='+encodeURIComponent(page_url)+'" frameborder="0" height="22" scrolling="no" width="120"></iframe>');
	$('#share_box .fb').html('<iframe src="http://www.facebook.com/plugins/like.php?app_id=103855643049156&amp;href='+encodeURIComponent(page_url)+'&amp;send=false&amp;layout=button_count&amp;width=80&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=lucida+grande&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:21px;" allowTransparency="true"></iframe>');
	
}


function addfriend (id) {
	
		if (!user_login) {auth_box('login'); return;}
	
		$.ajax({
		url: config_site+'/module/user/add.php',
		type: "POST",
		data: 'id='+id,
		beforeSend: function () {TINY.box.show("Please wait...",0,500,0,1);},
		success : function (data) {
					TINY.box.show(data,0,500,0,1,3);
				}
		});
}

function loadstream (step,tab,user) {
	$.ajax({
		url: config_site+'module/user/stream.php',
		type: "GET",
		data: 'step='+step+'&tab='+tab+'&user_stream='+user,
		cache: false,
		beforeSend: function () {$('#older_act_bt').html ('Loading...');},
		success: function(html){
			$('#older_act_bt').fadeOut('slow',function () {
				$('#older_act_bt').remove();
				$('#users .stream').append('<p style="border-top:2px solid #000; margin:3px 0px;"></p>'+html);
			});
		}
	});
}



function loadcomment (start,id) {
	$.ajax({
		url: config_site+'module/comment/inline.php',
		type: "GET",
		data: 'id='+id+'&start='+start+'&inline=1',
		cache: false,
		beforeSend: function () {$('#older_act_bt').html ('Loading...');},
		success: function(html){
			$('#older_act_bt').fadeOut('slow',function () {
				$('#older_act_bt').remove();
				$('#comments').append('<p style="border-top:2px solid #000; margin:3px 0px;"></p>'+html);
			});
		}
	});
}

////////////////////////////////////////////////////////////////////////////////////
/// Submitting system
////////////////////////////////////////////////////////////////////////////////////


//Submit comment
function submitcomment (comment_id) {

	if (comment_id==0) var post_data =  $('#comment_form').serialize();
	else var post_data =  $('#comment_form_'+comment_id).serialize();
	
	
	$.ajax({
		url: config_site+'module/submitter/submitter.php',
		type: "POST",
		data: post_data,
		cache: false,
		beforeSend: function () {TINY.box.show("Please wait...",0,300,0,1);},
		success: function(html){
			//TINY.box.hide();
			if (!html) {TINY.box.show("<span style='color:#FF0000'>You posted this comment already.</span>",0,300,0,1,5); return false;};
			if (html != 'captcha') {
				TINY.box.hide();
				if (comment_id==0) {$('#comment_form').html(html);}
				else {$('#comment_form_'+comment_id).html(html);}
			} else {
				load_captcha (Array('submitcomment',comment_id));
			}
		}
	});
	
	return false;
}



//Submit comment rating
function comment_vote (id,up,down) {
	
	$('#comment_row_'+id+' .pl').fadeOut('fast');
	$('#comment_row_'+id+' .mn').fadeOut('fast');


	$.ajax({
		url: config_site+'module/submitter/submitter.php',
		type: "POST",
		data: 'mod=rate_comment&id='+id+'&up='+up+'&down='+down,
		cache: false,
		success: function(html){
			//TINY.box.hide();
			if (!html) {TINY.box.show("<span style='color:#FF0000'>You rated this comment already.</span>",0,300,0,1,3); return false;};
			if (html != 'captcha') {
				if (up==1) {
					var rate = parseInt($('#comment_rate_'+id).html());
					rate = rate+1;
					$('#comment_rate_'+id).html(rate);
					
				} else if (down==1) {
					var rate = parseInt($('#comment_rate_'+id).html());
					rate = rate-1;
					$('#comment_rate_'+id).html(rate);
				}
			} else {
				load_captcha (Array('comment_vote',id+','+up+','+down));
			}
		}
	});
	
	return false;

}

//Show reply box
function comment_reply (comment_id,id) {
	
	if ($('form#comment_form_'+comment_id).length > 0) return false;
	
	var reply_html = '<div class="comment_reply"><form action="" method="post" id="comment_form_'+comment_id+'"><ul class="ul_forms"><li><textarea id="torrent_comment_area_'+comment_id+'" name="text"></textarea></li><input type="hidden" name="id" value="'+id+'" /><input type="hidden" name="comment_id" value="'+comment_id+'" /><input type="hidden" name="mod" value="comment" /><li style="background-color:#FFF;"><button class="small" type="submit"><span>SUBMIT</span></button></li></ul></form></div>';
	
	$('#comment_row_'+comment_id).after (reply_html);
	
	$('form#comment_form_'+comment_id).submit (function () {submitcomment(comment_id); return false;});
	$('#torrent_comment_area_'+comment_id).focus();
	
}

var rate_already;
//Submit torrent rating
function rate_torrent (id,elem) {
	
	if (rate_already==1) return false;
	
	
	$(elem).html('Like +1');
	
	$.ajax({
		url: config_site+'module/submitter/submitter.php',
		type: "POST",
		data: 'mod=rate_torrent&id='+id+'&up=1',
		cache: false,
		success: function(html){
			//TINY.box.hide();
			if (html == 'captcha') {
				load_captcha (Array('rate_torrent',id));
			} else {
				rate_already=1;
				$('ul.social_bt').html(html);
			}
		}
	});
	
	return false;

}



//Submit torrent fake report
var reported_already;
function report_fake (id,elem) {
	
	if (reported_already==1) return false;
	
	
	$(elem).html('Fake +1');
	
	$.ajax({
		url: config_site+'module/submitter/submitter.php',
		type: "POST",
		data: 'mod=report_fake&id='+id,
		cache: false,
		success: function(html){
			//TINY.box.hide();
			if (html == 'captcha') {
				load_captcha (Array('report_fake',id));
			} else {
				reported_already=1;
				$('ul.social_bt').html(html);
			}
		}
	});
		
}


function add_rss (id,elem) {
	
	if (user_login>1) {
		
	$(elem).html('Added');
		
	$.ajax({
		url: config_site+'module/submitter/submitter.php',
		type: "POST",
		data: 'mod=add_rss&id='+id,
		cache: false,
		success: function(html){
			return true;
		}
	});
	} else {
		auth_box ('login');
	}
}


function add_history (id) {

	if (user_login>1) {
		setTimeout (function () {
		$.ajax({
			url: config_site+'module/submitter/submitter.php',
			type: "POST",
			data: 'mod=add_history&id='+id,
			cache: false,
			success: function(html){
				return true;
			}
		});},5000);
	} 
}



function loadrate (up,down) {
	$.ajax({
		url: config_site+'module/torrent/rate.php',
		type: "POST",
		data: 'up='+up+'&down='+down+'&id='+torrent_id,
		cache: false,
		//beforeSend: function () {$('#comment_form').html ('<div class="ajax_loading"></div>');},
		success: function(html){
			$('#vote_box').html (html);
		}
	});
	
}


//Captcha functions
function load_captcha (actions) {
	
	$.ajax({
		url: config_site+'module/captcha.php',
		cache: false,
		success: function(html){
			TINY.box.show(html,0,300,120,1);
			setTimeout ( function () {
				$('#captcha_answer').focus ();
				$('#answer_captcha').click (function () {answer_captcha(actions);});
			}, 2000);
		}
	});
	
}

function answer_captcha (actions) {
	$.ajax({
		url: config_site+'module/captcha.php',
		type: "POST",
		data: 'answer='+$('#captcha_answer').val(),
		cache: false,
		beforeSend: function () {TINY.box.show("Please wait...",0,300,0,1,3);},
		success: function(html){
			
			if (html==1) {
					TINY.box.hide ();
					eval (actions[0]+'('+actions[1]+')');
			} else {
				
				TINY.box.show(html,0,300,120,1);
				setTimeout ( function () {
					$('#captcha_answer').focus ();
					$('#answer_captcha').click (function () {answer_captcha();});
				}, 2000);
			}
			
		}
	});
}

////////////////////////////////////////////////////////////////////////////////////
/// Auth System
////////////////////////////////////////////////////////////////////////////////////

function process_auth (method,from_box) {
	
	if (method == 'login') {var data = $('#forms .login').serialize();var p_url = '/login.html';};
	if (method == 'register') {var data = $('#forms .register').serialize();var p_url = '/register.html';};
	if (method == 'recover') {var data = $('#forms .forget').serialize();var p_url = '/recover.html';};
	
	$.ajax({
		url: config_site+'module/forms/auth.php',
		type: "POST",
		data: data,
		cache: false,
		beforeSend: function () {TINY.box.show("Please wait...",0,500,0,1);},
		success: function(html){
			if (html == 1) {
				if (method != 'recover') {
					TINY.box.show("Logged in...",0,500,0,1);
					window.location.reload();
				} else {
					TINY.box.show("A new password has been sent to your email.",0,500,0,1);
				}
			} else {
				if (!from_box) {TINY.box.show(html,0,500,0,1,5);} else {window.location.href=p_url+'?err=1'; return;}
			}
		}
	});
	
}



function auth_box (method) {
	
	if (method=='login')  var data = 'action=login';
	if (method=='register')  var data = 'action=register';
	
	TINY.box.show(config_site+'template/forms.php?inline=1&'+data,1,768,260,1);
}


function verify_email () {
	TINY.box.show(config_site+'module/user/verify.email.php',1,768,260,1);
}

////////////////////////////////////////////////////////////////////////////////////
/// 
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
/// Torrent page
////////////////////////////////////////////////////////////////////////////////////

function checkedit () {
	if ((user_login && torrent_user && torrent_user==user_login) || user_login==2) {
		$('#torrent dt.edit').append(' <a href="/edit/'+torrent_id+'.html" style="font-style:italic; background-color:#FFEFEF; font-weight:bold;">&nbsp;Edit this torrent&nbsp;</a>');
	}
}

function menu_verified () {
	$("#menu a.active").removeClass("active");
	$('#verified_menu_item').addClass("active");
}


function update_scrape_box (torrent_time) {
	$('#torrent .general .st .upd span').html('('+showago (torrent_time)+')');
}

function update_trackers () {

	$.ajax({
		url: config_site+'module/torrent/tracker.php',
		type: "POST",
		data: 'id='+torrent_id,
		cache: false,
		beforeSend: function () {$('#trackers_holder').html('<h4>Updating trackers... It might take up to one minute.</h4><div class="ajax_loading"></div>')},
		success: function(html){
			
			var newhtml = html.split(":SPACE:"); 
			var stathtml = newhtml[1].split("::");
		
			if (parseInt(stathtml[0])>=0) $('#torrent .general .st .sd').html(stathtml[0]+'<span>seeders</span>');
			if (parseInt(stathtml[1])>=0) $('#torrent .general .st .lc').html(stathtml[1]+'<span>leechers</span>');
			$('#trackers_holder').html (newhtml[0]);
			update_button.removeClass('upd2').addClass('upd').html('Updated');
		}
	});
	
	
}

////////////////////////////////////////////////////////////////////////////////////
/// 
////////////////////////////////////////////////////////////////////////////////////


function add_ads_300_250 () {
	$('#torrent .file_ads').append ('<div id="ads_300_250"><IFRAME FRAMEBORDER=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO WIDTH=300 HEIGHT=250 SRC="http://ad.adperium.com/st?ad_type=iframe&ad_size=300x250&section=1554200"></IFRAME></div>');
}

function add_ads_728_90 () {
	//$('.wide_ads').append ('<div id="ads_728_90"><IFRAME FRAMEBORDER=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO WIDTH=728 HEIGHT=90 SRC="http://ad.adperium.com/st?ad_type=iframe&ad_size=728x90&section=1554200"></IFRAME></div>');
	$('.wide_ads').append ('<div id="ads_728_90"><iframe src="http://optimized.by.vitalads.net/serve/5f0f07462c89a9164a20d316340d5660/?type=2" width="728" height="90" marginheight="0" marginwidth="0" scrolling="no" border="0" style="border:none;" frameborder="0"></iframe></div>');
}

/*
function add_ads_300_250 () {
	$('#torrent .file_ads').append ('<div id="ads_300_250"><IFRAME FRAMEBORDER=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO WIDTH=300 HEIGHT=250 SRC="http://fenopy.eu/module/promo/perium_iframe.html"></IFRAME></div>');
}

function add_ads_728_90 () {
	$('.wide_ads').append ('<div id="ads_728_90"><IFRAME FRAMEBORDER=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO WIDTH=728 HEIGHT=90 SRC="http://fenopy.eu/module/promo/perium_iframe_728.html"></IFRAME></div>');
}
*/


function searchjetdl () {
	var keyword = $('#header .search .k').val ();
	//var newWindow = window.open('http://jetdl.com/?keyword='+keyword, '_blank');
	var newWindow = window.open('http://bittrend.com/?keyword='+keyword, '_blank');
	newWindow.focus();
	return false;
}

function add_download_link_table () {
	$('td.jsDlLink').each (function () {
		var torrent_id = parseInt($(this).attr('id').replace ('t_',''));
		$(this).click (function () {window.location.href='/include/download.torrent.php?simple=1&id='+torrent_id;});
	});
}

function download_torrent (id) {
	window.location.href='/torrent/AJAX/'+id+'/download.torrent'; return;
}

function showago (stamp) {
	
	var offset = Math.round(new Date().getTime() / 1000)-stamp ;
	var ago;
	
	if (offset < 60) {
	
		ago = Math.round (offset)+" second ago";
	
	} else if (offset >(60-1) && offset < 3600) {
	
		ago = Math.round (offset/(60));
		ago = ago+' minute ago';
	
	} else if (offset > (3600-1) && offset < 24*3600) {
	
		ago = Math.round (offset/(3600));
		ago = ago+' hour ago';
	
	} else if ( offset > (24*3600-1) && offset < 24*3600*7) {
	
		ago = Math.round (offset/(24*3600));
		ago = ago+' day ago';
	
	} else if ( offset > (24*3600*7) && offset < 24*3600*30) {
	
		ago = Math.round (offset/(24*3600*7));
		ago = ago+' week ago';
	
	} else if ( offset > (24*3600*30) && offset < 24*3600*365) {
	
		ago = Math.round (offset/(24*3600*30));
		ago = ago+' month ago';
	
	} else {
		ago = Math.round (offset/(24*3600*365));
		ago = ago+' year ago';
	}

	return ago;
}


function set_up_auto_complete () {
	
	$('input.autocomplete').each (
		function () {
			$(this).autocomplete("/module/autocomplete/search.php", { minChars:3,delay:400,cacheLength:50,matchSubset:1,maxItemsToShow:10,extraParams: {'target':$(this).attr('alt')} });
			$(this).click (function () {$(this).css('color','#000');});
		}
	)
	
	
}



