var lastupload="";
var suberror=0;
jQuery(function(){
var ul = jQuery('#upload_ul ul');
jQuery('#drop a').click(function(){
// Simulate a click on the file input button
// to show the file browser dialog
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
jQuery('#upload').find('#upl').click();
});
// Initialize the jQuery File Upload plugin
jQuery('#upload').fileupload({
///This element will accept file drag/drop uploading
dropZone: jQuery(''),
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 5000000, // 5 MB
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
// kill any error messages
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
// kill display of previously uploaded ad
//if(jQuery('#display_ad_container').length !=0){
//}
var tpl = jQuery('
');
// tplerror not used
var tplerror=jQuery('
');
// regex for gif png jpe?g
var thefile=data.files[0].name.match(/(\.|\/)(gif|jpe?g|png)$/i);
var doerror=false;
// not gif jpg or png file
if(null == thefile)
{
tplerror.find('p').text(data.files[0].name) // png jpg gif files only
.append('' + formatFileSize(data.files[0].size) + 'Error: file type not allowed!');
doerror=true;
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error: image must be png, jpg, or gif ');
});
return false;
}
// file exceeds 5MB
if(parseFloat(data.files[0].size) > 5000000) // file too big
{
doerror=true;
tplerror.find('p').text(data.files[0].name)
.append('' + formatFileSize(data.files[0].size) + 'Error: file too big!');
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error: file too big!');
});
return false;
}
if(doerror) // should not be called any longer
{
data.context = tplerror.appendTo(ul);
tplerror.find('span').click(function(){
tplerror.fadeOut('slow',function(){
tplerror.remove();
});
});
return false;
}
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('' + formatFileSize(data.files[0].size) + '');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);//context depreciate;
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
});
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
jQuery('#adlink').val("");
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded / data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
},
complete: function(e,data){
//alert(data);
},
done: function (e, data) {
//alert(data.result);
var is_json = true;try {var obj = jQuery.parseJSON(data.result);} catch(err) {is_json = false;}
if(is_json){
var obj = jQuery.parseJSON(data.result);
if(obj.status=="success")
{
var iurl=obj.imgurl;
var iwidth=obj.iwidth;
var iheight=obj.iheight;
var upadid=obj.adid;
var iname=obj.iname;
var oext=obj.extension;
var hcid=obj.hcid;
var aimg=iurl+'/'+iname+'_full.'+oext+'?id='+hcid+'';
var timg=iurl+'/'+iname+'_thumbnail.'+oext+'?id='+hcid+'';
var oimg=iurl+'/'+iname+'.'+oext+'?id='+hcid+'';
var nslides=theslider.numSlides;
theslider.appendSlide('');
theslider.goTo(nslides);
var iimage='';
var viewimage=iimage;
//jQuery('.royalSlider').royalSlider('appendSlide', '', 1);
//jQuery('#img_preview').html(viewimage).hide().fadeIn(1000);
jQuery('#sub_success').css('display','block');
if(obj.badurl==1)
{
//jQuery('#upload_error').fadeIn('slow',function(){
//jQuery(this).html("Upload Successful. Warning: Please double check the url you provided to make sure it's not broken and correct as required.");
//});
}
} //end status eq success
else
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html(obj.status);
});
jQuery('#upload_ul ul li:first').remove();
}//end of status not eq success
} //end of is_json;
else
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error processing request. Please try again. If the error persists, please contact bateworld customer satisfaction !');
});
jQuery('#upload_ul ul li:first').remove();
}
data.jqXHR;
} // end of done
});
// Prevent the default action when a file is dropped on the window
jQuery(document).on('drop dragover', function (e) {
e.preventDefault();
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
});