$.getID = function(s) {
    return s.split('-').pop();
};
$.fn.getID = function() {
    return $.getID(this.attr('id'));
};

$.fn.fadeHighlight = function() {
    
    $(this).show().fadeOut(3000);

}

$.fn.deleteImage = function(){
     $(this).click(function() {
       
         var parent = $(this).parent("div").parent("div");
         var file = $(this).prev('input').val();
         var url = $(this).attr('href');
         
         if(file == 'undefined' || file == undefined) {
             return false;
         }

         $("#dialog").dialog({
		bgiframe: true,
		resizable: false,
		height: 200,
		modal: true,
		buttons: {      
                                'Ei': function() {
					$(this).dialog('destroy');
				},
                                'Jah': function() {
					parent.remove();

                                         $.ajax({
                                            type: 'GET',
                                            url: url,
                                            data: 'path=' + file + '&folder[]=screen&folder[]=thumb'
                                        });
                                        $(this).dialog('destroy');
                                        $("#total").html(parseInt($("#total").html()) - 1);
				}
			}
		});
                return false;
	});
}

$.fn.removeProductAttribute = function() {
        $(this).click(function() { 
            var parent = $(this).parent("div");
            parent.remove();

             $.ajax({
                  type: 'GET',
                  url: $(this).attr('href')
             });
            return false;
        });
        return false;
}

$.fn.addProductAttribute = function() {
    $(this).click(function() {
        var that = this;
             $.ajax({
                  type: 'GET',
                  url: $(this).attr('href'),
                  dataType: 'json',
                  success: function(data) {
                      $(that).before(data.render);
                      $('#product-attribute-' + data.id).find(".remove-attribute").removeProductAttribute();
                  }
             });
            return false;
        });
}

$.fn.submitForm = function(uploadifyID, queueID) {
    $(this).submit(function() {
         $(this).after('<div><div id="dialog" title="Palun kannatust"><p>Palun kannatust. Salvestan sisu.</p></div>');
         $("#dialog").dialog({
			bgiframe: true,
			height: 140,
			modal: true,
                        closeOnEscape: false
		});
                
         if($('#' + queueID).html() == '') {
           return true;
         }
         
         $(this).append('<input type="hidden" name="isAjax" value="1" />');
         $(this).ajaxSubmit({
            success: function(data){
             $('#' + uploadifyID).uploadifySettings('folder',data);
             $('#' + uploadifyID).uploadifyUpload();
         }});
         return false;
    });
}

$.fn.toggleMenu = function() {
    $(this).click(function(){
        var ul = $(this).next('ul');
        if(ul.is(':visible')) {
            ul.slideUp();
        }else{
            ul.slideDown();
        }
    });
}

$.fn.removeImportProduct = function() {
    $(this).click(function(){
        $(this).parent('div').remove();
        $.ajax({
            'url' : $(this).attr('href')
        });

        return false;
    });
}

//returns elements for translation selection
$.fn.loadOnClick = function(container) {
    $(this).click(function(){
        $("#" + container).html('');
        $("#" + container).load($(this).attr('href'));
        $('#' + container).show();

        return false;
    });
}

function ajaxdialog(options){
    var defaultOptions = {
        id: 'dialog',
        url: '',
        form: false,
        uploadify: false,
        width: 200,
        height: 200
    };
    
    $.extend(defaultOptions,options);

    $.ajax({
        url: defaultOptions.url,
        success: function(data) {
          $(document.body).append(data);

          //open modal
            $('#' + defaultOptions.id).dialog({
                width: defaultOptions.width,
                height: defaultOptions.height,
                bigframe: true,
                autoOpen: true,
                close: function() {
                    $(this).dialog('destroy');
                    $(this).remove();
                }
            });
            
            if(defaultOptions.uploadify) {
                $("#" + defaultOptions.uploadify).uploadify({
                        'uploader'       : '../swf/uploadify.swf',
                        'script'         : '../uploadify/uploadify.php',
                        'checkScript'    : '../uploadify/check.php',
                        'expressInstall' : '../swf/expressInstall.swf',
                        'cancelImg'      : '../images/cancel.png',
                        'scriptAccess'   : 'always',
                        'queueID'        : 'fileQueue',
                        'buttonText'     : 'Vali fail',
                        'auto'           : false,
                        'multi'          : false,
                        onComplete: function(event,data) {
                           $('#' + defaultOptions.id).dialog('destroy');
                           $('#' + defaultOptions.id).remove();
                           $("#tableView").trigger("reloadGrid");
                        }
                });
            }
            
            if(defaultOptions.form) {
                $("#" + defaultOptions.form).submit(function(){
                    $("#" + defaultOptions.form).ajaxSubmit({success: function(data){
                       if(data != '') {
                        $('#' + defaultOptions.uploadify).uploadifySettings('folder',data);
                        $('#' + defaultOptions.uploadify).uploadifyUpload();
                       }else{
                           $('#' + defaultOptions.id).dialog('destroy');
                           $('#' + defaultOptions.id).remove();
                           $("#tableView").trigger("reloadGrid");
                       }
                    }});
                    return false;
                 });
            }

            return false;
        }
    })
}
