// Notification system
Notificate = {
    error: function(message, timeout) {
        if (typeof message == 'undefined') {
            message = "System error!";
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 0;
        }
    
        $.achtung({
            timeout: timeout,
            message: message,
            className: 'achtungFail',
            icon: 'ui-icon-alert'
        });
    },
    success: function(message, timeout) {
        if (typeof message == 'undefined') {
            message = "Operation completed!";
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 5;
        }
    
        $.achtung({
            timeout: timeout,
            message: message,
            className: 'achtungSuccess',
            icon: 'ui-icon-check'
        });
    },
    info: function(message, timeout) {
        if (typeof message == 'undefined') {
            return;
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 10;
        }
    
        $.achtung({
            timeout: timeout,
            message: message,
            className: 'achtungInfo',
            icon: 'ui-icon-info'
        });
    },
    custom: function(message, timeout, settings) {
        if (typeof message == 'undefined') {
            message = "Notification";
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 5;
        }
    
        settings = $.extend({
            timeout: timeout,
            message: message            
        }, settings);
        
        $.achtung(settings);
    }
};

// Bookmark
$(document).ready(function() {
  $(".add-favourites").jBrowserBookmark();
});

// AJAX-FORM
jQuery(function($) {
    function initForm(hash) {
        var width = $(hash.w).width();
        $(hash.w).css('margin-left', -Math.round(width/2) + "px");
        $(hash.w).css('width', width + "px");
        
        $('form', hash.w).ajaxForm({
            method: 'POST',
            dataType: 'json',
            onSuccess: function (data, form) {
                $(hash.w).jqmHide();
            }
        });
    }

    function onHide(hash) {
        $(hash.w).hide();
        $(hash.o).remove();
    }

    function hideForm() {
        $('#ajax-form').jqmHide();
        return false;
    }

    if ($('#ajax-form').length == 0) {
        $('<div></div>').attr('id', 'ajax-form').appendTo('body');
    }

    $('#ajax-form').jqm({
        modal: true,
        ajax: "@href",
        ajaxText: 'Loading...',
        toTop: true,
        onLoad: initForm,
        onHide: onHide
    });

    $('#ajax-form .close-button').live('click', hideForm);
    $('#ajax-form button.cancel').live('click', hideForm);
    $('#ajax-form .cancel').live('click', hideForm);
    $(".jqmOverlay").live('click',hideForm);
    
    $('a.action-ajax-form').live('click', function() {
        $('#ajax-form').jqmShow(this);
        return false;
    });
   
    $('#ajax-form .content a.submit').live('click', function() {
        var form = $(this).parents('form');
        
        form.ajaxSubmit({
            success: function() {
                var fbcontent = form.parents('.fbcontent');
                $('.fbcontent-main', fbcontent).fadeOut('fast');
                $('.fb-success-submit', fbcontent).fadeIn('fast');
                setTimeout('$("#ajax-form").jqmHide();', 2000);
                if ($(form).attr('class')=='business_card_edit_form') {
                    location.reload(true);
                }
                
            }
        });

        return false;
    });
    $('#ajax-form a.submit').live('click', function() {
        var form = $(this).parents('form');
        form.ajaxSubmit({
            success: function() {
                var fbcontent = form.parents('.fbcontent');
                
                $('.fbcontent-main', fbcontent).fadeOut('fast');
                $('.fb-success-submit', fbcontent).fadeIn('fast');
                setTimeout('$("#ajax-form").jqmHide();', 2000);
                
            }
        });

        return false;
    });
    
});

function myFancybox(params) {
    var content = (params.content != null) ? params.content : ''; 
    var linkAdobe = (params.linkAdobe != null) ? params.linkAdobe : '';
    var swfObj = (params.swfObj != null) ? params.swfObj : '';
    var presentationId = (params.presentationId != null) ? params.presentationId : '';

    $.fancybox(
            '<div id="sp-fancy-player" style="background: url(/img/no_flash.png) no-repeat 0 0; width: 932px; height: 367px;"><p class="noflash">' + linkAdobe + '</p></div><div class="sp-fancy">' + content + '<a class="spf-close" onclick="$.fancybox.close()"/></a></div>',
            {
                'autoDimensions'     : false,
                'autoScale'          : false,
                'width'              : 932,
                'height'             : 400,
                'transitionIn'       : 'none',
                'transitionOut'      : 'none',
                'showCloseButton'    : false,
                'enableEscapeButton' : false,
                'hideOnContentClick' : false,
                'overlayColor'       : '#000000',
                'overlayOpacity'     : '0.8',
                'centerOnScroll'     : 'true',
                'onComplete'  : function() {
                    if (swfObj != '') { 
                        eval(swfObj.replace(/__ID__/, presentationId));
                    }
                }
    });
}

