var is_ajax_upload = 0;

$(document).ready(function(){

	if ($('#news_upload').length>0){
		new AjaxUpload('#news_upload', {
			action: 'scripts/scr_player_news_photo.php',
			name: 'news_photo',
			autoSubmit: true,
			responseType: false,
			onChange: function(file, extension){},
			onSubmit : function(file ,extension){
				is_ajax_upload = 1;
				extension = extension.toLowerCase();
				if (extension && /^(jpg|jpeg)$/.test(extension)){
					$('#profile_news_photo_info').html('Uploading ' + file);	
				} else {
					$('#profile_news_photo_info').html('Nur .jpg; .jpeg; .jpe Dateien. Danke.');
					return false;				
				}
		
			},
			onComplete: function(file, response) {
				is_ajax_upload = 0;
				var r = response.split('|');
				if (r[0]==0){
					$('#profile_news_photo_info').html(r[1]);
				}
				else{
					$('#profile_news_photo_info').html(r[1].substr(0,27)+(r[1].length>27?'...':''));
					$('#news_photo').val(r[0]);
					$('#news_photo_w').val(r[2]);
					$('#news_photo_h').val(r[3]);
					$('#news_photo_x1').val(r[4]);
					$('#news_photo_y1').val(r[5]);
					$('#news_photo_x2').val(r[6]);
					$('#news_photo_y2').val(r[7]);
					$('#news_preview').show();
					$('#news_upload').html('Change');
				}
				
			}
		});
	}

	$('#news_preview').live('click',function(){
		var image = $('#news_photo').val();
		var my_id = '';
		var uimg_width = $('#news_photo_w').val();
		var uimg_height = $('#news_photo_h').val();
		var uimg_x1 = $('#news_photo_x1').val();
		var uimg_y1 = $('#news_photo_y1').val();
		var uimg_x2 = $('#news_photo_x2').val();
		var uimg_y2 = $('#news_photo_y2').val();
		open_uploader();
		$('#uploader').load('include/uploader.php',{image:image,type:'btn_n_photo',id:my_id,uimg_width:uimg_width,uimg_height:uimg_height,uimg_x1:uimg_x1,uimg_y1:uimg_y1,uimg_x2:uimg_x2,uimg_y2:uimg_y2},function(){
			$('#uploader').show();
		});
		return false;
	});

	if ($('#pphoto_upload').length>0){
		new AjaxUpload('#pphoto_upload', {
			action: 'scripts/scr_player_photo_upload.php',
			name: 'pphoto_photo',
			autoSubmit: true,
			responseType: false,
			onChange: function(file, extension){},
			onSubmit : function(file ,extension){
				is_ajax_upload = 1;
				extension = extension.toLowerCase();
				if (extension && /^(jpg|jpeg)$/.test(extension)){
					$('#pphoto_upload_info').html('Uploading ' + file);	
				} else {
					$('#pphoto_upload_info').html('Nur .jpg; .jpeg; .jpe Dateien. Danke.');
					return false;				
				}
		
			},
			onComplete: function(file, response) {
				is_ajax_upload = 0;
				var r = response.split('|');
				if (r[0]==0){
					$('#pphoto_upload_info').html(r[1]);
				}
				else{
					$('#pphoto_upload_info').html(r[1].substr(0,27)+(r[1].length>27?'...':''));
					$('#pphoto_photo').val(r[0]);
					$('#pphoto_photo_w').val(r[2]);
					$('#pphoto_photo_h').val(r[3]);
					$('#pphoto_photo_x1').val(r[4]);
					$('#pphoto_photo_y1').val(r[5]);
					$('#pphoto_photo_x2').val(r[6]);
					$('#pphoto_photo_y2').val(r[7]);
					$('#pphoto_preview').show();
					$('#pphoto_upload').html('Change');
				}
				
			}
		});
	}

	$('#pphoto_preview').live('click',function(){
		var image = $('#pphoto_photo').val();
		var my_id = '';
		var uimg_width = $('#pphoto_photo_w').val();
		var uimg_height = $('#pphoto_photo_h').val();
		var uimg_x1 = $('#pphoto_photo_x1').val();
		var uimg_y1 = $('#pphoto_photo_y1').val();
		var uimg_x2 = $('#pphoto_photo_x2').val();
		var uimg_y2 = $('#pphoto_photo_y2').val();
		open_uploader();
		$('#uploader').load('include/uploader.php',{image:image,type:'btn_p_photo',id:my_id,uimg_width:uimg_width,uimg_height:uimg_height,uimg_x1:uimg_x1,uimg_y1:uimg_y1,uimg_x2:uimg_x2,uimg_y2:uimg_y2},function(){
			$('#uploader').show();
		});
		return false;
	});

	if ($('#user_avatar').length>0){
		if (!$('#user_avatar').hasClass('rl')){
			new AjaxUpload('#user_avatar', {
				action: 'scripts/scr_user_avatar.php',
				name: 'user_avatar',
				autoSubmit: true,
				responseType: false,
				onChange: function(file, extension){
					if (is_ajax_upload==1){
						show_alert('alert','<br />Upload in progress...',0);
						return false;
					}
				},
				onSubmit : function(file ,extension){
					is_ajax_upload = 1;
					extension = extension.toLowerCase();
					if (extension && /^(jpg|jpeg)$/.test(extension)){
						$('#user_avatarh strong').addClass('loader');
						$('#user_avatarh strong img').fadeOut();
					} else {
						show_alert('alert','<br />Nur .jpg; .jpeg; .jpe Dateien. Danke.',0);
						is_ajax_upload = 0;	
						return false;				
					}
			
				},
				onComplete: function(file, response) {
					is_ajax_upload = 0;
					if (response==0){
						$('#user_avatarh strong').fadeIn(function(){
							$('#user_avatarh strong').removeClass('loader');
						});
						show_alert('alert','Image could not be uploaded.<br />Try again later.',0);
					}
					else{
						$('#user_avatarh strong img').attr('src','images/avatar/'+response);
						$('#user_avatarh strong img').fadeIn(function(){
							$('#user_avatarh strong').removeClass('loader');
						});
					}					
				}
			});
		}
	}
});