function wait_img() {
	return '<br><img src="images/indicator.gif" width=15 height=15><br><br>';
}

var monit_dialogs=new Array();

monit_dialogs['limit']='Przekroczyles limit plikow do pobrania';

var sel_MP=0;
var sel_OFS=0;
var sel_LIM=0;
var cur_dwnl=0;


function ch_section_ofs(idsec,ofs) {
	sel_OFS=ofs;
	ch_section(idsec);
	sel_OFS=0;
	return false;
}

function ch_library() {
	if ($('#repeBGcontent').length) {
		$('#repeBGcontent').html(wait_img());
	}
	$('#repeBGcontent').load('ax.php?op=sections_menu&id_lib='+$('#jumpMenu').val(), function() {ch_section(0)});
	return false;

}


function bulk_recountHtmlUpdate(s1, s2, s3) {
	$('#bulkcount_pckcnt').html(s1);
	$('#bulkcount_fcnt').html(s2);
	$('#bulkcount_scnt').html(s3);
}

function bulk_recount() {

	$.ajax({
		url:'ax.php',
		type:'GET',
		data:({op:'bulkrecount'}),
		success:function(resp) {
			var warsy = resp.split('|');
			bulk_recountHtmlUpdate(warsy[0],warsy[1],warsy[2]);
		}
	});
}

function sel_file_out(id) {
	sel_file(id);
	$('#selfileli_'+id).remove();
}

function sel_file(id) {

	$.ajax({
		url:'ax.php',
		type:'POST',
		data:({op:'sel_file','id':id}),
		success:function(resp) {
			if (resp=='exceed') {
				alert('You have exceeded your download limit!')
			} else {
				
				var warsy = resp.split('|');
				if ($('#imgf_'+id).length) {
					if (warsy[0]=='on') {
						$('#imgf_'+id).attr('src','images/sampleSelect_on.gif');
					} else {
						$('#imgf_'+id).attr('src','images/sampleSelect_off.gif');
					}
				}				
				bulk_recountHtmlUpdate(warsy[1],warsy[2],warsy[3]);

			}
		}
	});

	return false;

}

function select_all() {
	if ($('#thissiteids').length) {
		var tmp = $('#thissiteids').val().split(',');
		if (tmp.length>0) {
			for (i=0; i<tmp.length; i++) {
				if (tmp[i]!='') $('#imgf_'+tmp[i]).attr('src','images/sampleSelect_on.gif');
			}
			$.ajax({
				url:'ax.php',
				data:({
					op:'selectall',
					ids:$('#thissiteids').val()}
				),
				success:function(resp) {
					var warsy = resp.split('|');
					bulk_recountHtmlUpdate(warsy[0],warsy[1],warsy[2]);
				}
			});
		}
	}
	return false;
}

function deselect_all() {

	if ($('#thissiteids').length) {
		var tmp = $('#thissiteids').val().split(',');
		if (tmp.length>0) {
			for (i=0; i<tmp.length; i++) {
				if (tmp[i]!='') $('#imgf_'+tmp[i]).attr('src','images/sampleSelect_off.gif');
			}
			
			$.ajax({
				url:'ax.php',
				data:({op:'deselectall',ids:$('#thissiteids').val()}),
				success:function(resp) {
					var warsy = resp.split('|');
					bulk_recountHtmlUpdate(warsy[0],warsy[1],warsy[2]);
				}
			});
			
		}
	}
	return false;
	
}

function clear_selection_out() {
	clear_selection();
	ch_site('currentdown');
}

function clear_selection() {

	if ($('#thissiteids').length) {
		var tmp = $('#thissiteids').val().split(',');
		if (tmp.length>0) {
			for (i=0; i<tmp.length; i++) {
				if (tmp[i]!='') {
					$('#imgf_'+tmp[i]).attr('src','images/sampleSelect_off.gif');
				}
			}
		}
	}
	$.ajax({
		url:'ax.php',
		data:({'op':'unsetall'}),
		success:function(resp){
			var warsy = resp.split('|');
			bulk_recountHtmlUpdate(warsy[0],warsy[1],warsy[2]);
		}
	});

	return false;
}

function ch_section2(idsec) {
if ($('#files_ofs').length) var sel_LIM=$('#files_ofs').val();
$('#repeBGS').html(wait_img());
$('#repeBGS').load('ax.php?op=files_menu&id_sec='+idsec+'&id_lib='+$('#jumpMenu').val()+'&ofs='+sel_OFS+'&limit='+sel_LIM);
}

function ch_section(idsec) {
	if ($('#files_ofs').length) var sel_LIM=$('#files_ofs').val();
	$('#repeBGS').html(wait_img());
	$.ajax({
		url:'ax.php',
		dataType:'json',
		type:'POST',
		data:({
			'op':'files_menu',
			'id_sec':idsec,
			'id_lib':$('#jumpMenu').val(),
			'ofs':sel_OFS,
			'limit':sel_LIM
		}),
		success:function(re) {
			$('#repeBGS').html(re.html);
//			$('#SamplesPanel').css('background','url('+re.background+')');
			if (re.background) {
				$('#repeBGS').css('background','#000 url('+re.background+') no-repeat 0px 0px');
			} else  {
				$('#repeBGS').css('background','none');
			}
		}
	});
//	$('#repeBGS').load('ax.php?op=files_menu&id_sec='+idsec+'&id_lib='+$('#jumpMenu').val()+'&ofs='+sel_OFS+'&limit='+sel_LIM);
	
	return false;
}


function playsample(fn) {
	var ison='true';
	if (sel_MP!=0) {
		if ($('#hp_'+sel_MP).length) $('#hp_'+sel_MP).attr('src','images/listenBtn.png');
	}
	
	var fn2='';
	
	if (sel_MP==fn) {
		ison='false';
		sel_MP='';
		fn='';
	}
	
	$('#musicPlayer').load('ax.php?op=musicplayer&ison='+ison+'&fn='+fn, function () {
		$('#musicPlayer').show();
		$('#hp_'+fn).attr('src','images/listenBtn_on.png');
		sel_MP=fn;
	});
	
	return true;
}

function simpleplay(fn) {
	
	$('#musicPlayer').load('ax.php?op=simpleplayer&fn='+fn, function () {
		$('#musicPlayer').show();
		$('#hp_'+fn).attr('src','images/listenBtn_on.png');
		sel_MP=fn;
	});
	
}


function ch_site(sid) {

	if ($('#files_ofs').length) sel_LIM=$('#files_ofs').val();

	if ($('#repeBGS').length) {
		$('#repeBGS').html(wait_img());
	}
	
	$('#repeBGS').load('ax.php?op=getsite&sid='+sid+'&ofs='+sel_OFS+'&limit='+sel_LIM);

	return void(0);

}

function ch_site_ofs(sid, ofs) {
	sel_OFS=ofs;
	ch_site(sid);
	sel_OFS=0;
	return false;
}

function get_left_sr() {
	$('#repeBGcontent').html(wait_img());

	$('#repeBGcontent').load('ax.php?op=left_search&q='+escape($('#libSearch_input').val()), function() {
		get_content_sr($('#firstlib').val(), $('#firstsec').val());		
	});

	return false;
}

function get_new_files() {
	$('#repeBGcontent').html(wait_img()).load('ax.php?op=newfiles',function() {
		get_content_new_files($('#firstlib').val(), $('#firstsec').val());
	});
	return false;
}

function get_content_new_files(idlib, idsec) {
	$('#repeBGS').html(wait_img()).load('ax.php?op=right_newfiles&idlib='+idlib+'&idsec='+idsec);
	return false;
}


function get_content_sr(idlib, idsec) {
	$('#repeBGS').html(wait_img()).load('ax.php?op=content_search&idlib='+idlib+'&idsec='+idsec+'&q='+escape($('#qstring').val()));
	return false;
}

function get_search_result() {
	if ($('#libSearch_input').val().length>=3) {
		$('#jumpMenu').hide();
		get_left_sr();
	} else alert('Minimum 3 characters!');
	
	return false;
}

function get_download_exceed() {
	if ($('#download_remaining').length) {
		$.ajax({
			url:'ax.php',
			data:({'op':'download_exceed'}),
			success:function(resp) {
				alert(resp);
				$('#download_remaining').val(resp);
			}
		});
	}
}

function get_file(id) {
	get_download_exceed();
	if (($('#download_remaining').length && parseInt($('#download_remaining').val())>0) || !$('#download_remaining').length) {
		$('#SampleLib'+id).html('Downloading...');
		location.href='download.php?id='+id;
	} else {
		alert('You have exceeded your download limit!');
	}
}


function cf_submit() {
	if ($('#cf_message').val()=='') {
		alert('Message is Required Field');
		return false;
	}

	var $cftitle=$('#cf_title').val();
	var $cfmessage=$('#cf_message').val();
	var $cfphone=$('#cf_phone').val();

	$('#repeBGS').html(wait_img()).load('ax.php?op=cf_send&cf_title='+escape($cftitle)+'&cf_phone'+escape($cfphone)+'&cf_message='+escape($cfmessage));

	return false;
	
}

function generatePackagesFromCS() {
	$('#repeBGS').html(wait_img());
	$.ajax({
		url:'ax.php',
		data:({'op':'getsite','sid':'generatepackagefromcs'}),
		success:function(resp) {

			$('#repeBGS').html(resp);
			bulk_recount();
			if ($('#onstart').length) {
				var s=$('#onstart').val().split(',');
				generatezipfile(s[0],s[1]);
			}
			
		}
		
	});
}




function generatezipfile(pack, part) {
	$('#wait_'+pack+'_'+part).show();
	
	$.ajax({
		url:'ax.php',
		data:({'op':'generatezipfile','pack':pack,'part':part}),
		success:function(resp) {

			if (resp=='') {
				alert('Server not responding');
				return false;
			}

			var resp2=resp.split('|');

			if (resp2[0]=='ok') {
				$('#wait_'+pack+'_'+part).hide();
				$('#dwnlbtn_'+pack+'_'+part).show();
				$('#delbtn_'+pack+'_'+part).show();
				$('#delbtn_'+pack+'_'+part).click(function() {
					deletezipfile(pack, part);
				});
				$('#dwnlbtn_'+pack+'_'+part).click(function () {
					location.href=resp2[1] 
				});
				
			}

			if (resp2[0]=='error') alert(resp2[1]);
			
		}
		
	});
	
	return false;
}

function deletezipfile(pack, part) {
	if (confirm('Are you sure?')) {
		$('#wait_'+pack+'_'+part).show();
		$('#delbtn_'+pack+'_'+part).hide();
		$('#dwnlbtn_'+pack+'_'+part).hide();
		$.ajax({
			url:'ax.php',
			type:'POST',
			data:({'op':'deletezipfile','pack':pack,'part':part}),
			success:function (resp) {
				
					if (resp=='') {
						alert('Server not responding');
						return false;
					}
					var resp2=resp.split('|');
					if (resp2[3]=='nofile') {
							if (parseInt(resp2[1])!=pack) {
								$('#package_'+pack).fadeOut();
							} else {
								$('#package_'+pack+'_'+part).fadeOut();
							}
						return false;
					}
					if (resp2[0]=='ok') {
						$('#wait_'+pack+'_'+part).hide();
						$('#dwnlbtn_'+pack+'_'+part).hide();
						$('#delbtn_'+pack+'_'+part).hide();
						if (resp2[1]!='' && resp2[2]!='') {

							$('#activeparts_'+resp2[1]+'_'+resp2[2]).css('color','#ffffff');
							$('#package_'+resp2[1]+'_'+resp2[2]).css('color','#ffffff');
							$('#queued_'+resp2[1]+'_'+resp2[2]).hide();

							if (parseInt(resp2[1])!=pack) {
								$('#package_'+pack).fadeOut();
							} else {
								$('#package_'+pack+'_'+part).fadeOut();
							}
							alert(resp2[3]);
							if (resp2[3]!='nofile') generatezipfile(resp2[1], resp2[2]);

						} else {
							$('#package_'+pack).fadeOut();
							$('#repeBGS').html('<p>You dont have any packages</p>');
						}
					}
					if (resp2[0]=='error') alert(resp2[1]);
				}
				
		});
	}
	return false;
}


function check_uploadform() {

	if ($('#upload_filefield').val()=='') {
		alert('Select file from your computer');
		return false;
	}

	if ($('#upload_title').val()=='') {
		alert('Fill title field');
		return false;
	}

	if ($('#description').val()=='') {
		alert('Fill description field')
		return false;
	}

	if ($('#keywords').val()=='') {
		alert('Fill keywords field')
		return false;
	}

	if (!$('#upload_acceptance').is(':checked')) {
		alert('You have to accept the terms and conditions');
		return false;
	}
	
	return true;
}

function ud_highlight(el) {
	$('#userdata_catlist li').removeClass('on');
	$(el).addClass('on');
}


function upload_switch_submit() {
	if ($('#upload_acceptance').is(':checked')) {
		$('#submit_upload').removeAttr('disabled');
	} else {
		$('#submit_upload').attr('disabled','disabled');
	}
}


function ax_addcomment(idnews) {
	$.ajax({
		url:'ax.php',
		data:({op:'ax_addcomment',message:$('#cmt_message').val(),'idnews':idnews}),
		success:function(resp){
			if (resp!='') {
				var re = $.parseJSON(resp);
				$('#comments').prepend(
					$('<strong>'+re.username+'</strong> <em>'+re.data+'</em><br/><quote>'+re.message+'</quote><br/><br/>')
					);
				$('#comments_form').slideToggle();
				$('#cmt_message').val('');
			}
		}
	});
}
function vote(vote, file_id){
  $.ajax({
		url:'ax.php',
		data:({op:'ax_vote','vote':vote,'file_id':file_id}),
		success:function(resp){
			if (resp!='') {
			 var re = $.parseJSON(resp);
       alert(re.msg);	
			}
		}
	});
}

var chatnotice = {
	btnopacity:1,
	blink:0,
	blinkbtn:function() {
		if (this.blink==1) {
			if (this.btnopacity==1) {
				this.btnopacity=0.5;
			} else {
				this.btnopacity=1;
			}
			$('#chatbtn').animate({opacity:this.btnopacity},500);
			setTimeout(function (){chatnotice.blinkbtn()},500);
		}
	},
	check:function() {

		$.ajax({
			url:'chat/index.php',
			dataType:'json',
			data:({
				's':'wbtchat',
				'op':'ax_silentretrive'
			}),
			success:function(re) {
				var blink=0;
				var showtext = '';
				if (re) {
					if ($(re.users.add).length) {
						showtext = 'Login: ';
						for (id in re.users.add) {
							showtext += re.users.add[id] +', ';
						}
						showtext +=' avaliable on chat.<br/>';
					}
					if ($(re.users.del).length>0) {
						showtext += 'Chat: Logout - ';
						for (id in re.users.del) {
							showtext += re.users.del[id]+', ';
						}
						showtext += '<br/>';
					}
					if (re.msg.length>0) {
						showtext += 'Someone send private message to you on <a href="index.php?s=chat">chat</a>...';
					}
					if ($(re.any_msg).length) {
						if (chatnotice.blink!=1) {
							chatnotice.blink=1;
							chatnotice.blinkbtn();
						}
					}
					if (showtext!='') $.noticeAdd({text:showtext});
				}
				setTimeout(function (){chatnotice.check()},10000);
			}
		});
		
	}
}


