$(document).ready(function()
{
    if (typeof(yapyap) == 'undefined')
    {
        if (typeof(console) != 'undefined' && typeof(console.log) == 'function')
        {
            console.log("Warning: yapyap.ui.js was included without or before yapyap.js");
        }
        yapyap = {};
    }

    if (typeof(yapyap.ui) == 'undefined')
    {
        yapyap.ui =
        {
            templates:
            {
                get: function (locator)            
                // Returns text string with html/CDATA template
                {
                    return yapyap.utils.string.trim($(locator).getCDATA());
                },

                fill: function (template, data)
                {
                    for (key in data)
                    {
                        template = template.replace(new RegExp('@' + key, 'g'),
                                yapyap.utils.string.htmlen_noQuote(data[key]));
                    }
                    return template;
                }
            },

            utils:
            {
                refreshUpdatesCounter: function ()
                {
                  yapyap.links.updates.requestUpdatedContactsCount(
                      function (response)
                      {
                          if (response != 0)
                          {
                              $('#connections_counter').html(response).parent().show();
                          }
                          else
                          {
                            $('#connections_counter').html('').parent().hide();
                          }
                      },
                      function (errorResponse, errorType)
                      {
                          // no extra error handling needed
                          // TODO@gbezyuk: common interface for N times reatempting
                      }
                  );
                },

                center: function (elementLocator, horizontal, vertical, parentLocator)
                {
                    var $node = $(elementLocator);
                    var $parent = parentLocator ? $(parentLocator) : $node.parent();

                    if (horizontal)
                    {
                        var pw = $parent.width();
                        $node.css('position', 'relative');
                        var npw = (pw - $node.width())/2;
                        $node.css('left', (npw > 0 ? npw : 0) + 'px');
                    }

                    if (vertical)
                    {
                        var ph = $parent.height();
                        $node.css('position', 'relative');
                        var nph = (ph - $node.height())/2; 
                        $node.css('top', (nph > 0 ? nph : 0) + 'px');
                    }
                },


                

                fitImage: function ($img, containerWidth, containerHeight, fillFlag)
                {
                    if (typeof(fillFlag) == 'undefined')
                    {
                        fillFlag = true;
                    }
                    var image =
                    {
                        w: $img.width(),
                        h: $img.height(),
                        //p: $img.width() / $img.height(),
                        scale: function (k)
                        {
                            image.w *= k;
                            image.h *= k;
                        }
                    };
                    var container =
                    {
                        w: containerWidth,
                        h: containerHeight
                        //p: containerWidth / containerHeight
                    };

                    var log = {
                        originalW: image.w,
                        originalH: image.h,
                        //imageP: image.p,
                        containerW: container.w,
                        containerH: container.h,
                       // containerP: container.p,
                        fillFlag: fillFlag
                    };

                    var k = 1;
                    if (image.w > image.h)
                    {
                        if (image.w > container.w)
                        {
                            if ( ! fillFlag)
                            {
                                k = container.w / image.w;
                                log.transformCase = '1';
                            }
                            else
                            {
                                k = container.h / image.h;
                                log.transformCase = '2';
                            }
                        }
                        else
                        {
                            if (fillFlag)
                            {
                                k = container.w / image.w;
                                log.transformCase = '3';
                            }
                            else
                            {
                                log.transformCase = '4';
                            }
                        }
                    }
                    else
                    {
                        if (image.h > container.h)
                        {
                            if (fillFlag)
                            {
                                k = container.w / image.w;
                                log.transformCase = '5';
                            }
                            else
                            {
                                k = container.h / image.h;
                                log.transformCase = '6';
                            }
                        }
                        else
                        {
                            if (fillFlag)
                            {
                                k = container.h / image.h;
                                log.transformCase = '7';
                            }
                            else
                            {
                                log.transformCase = '8';
                            }
                        }
                    }
                    image.scale(k);
                    image.margins =
                    {
                        left: (container.w - image.w) / 2,
                        top: (container.h - image.h) / 2
                    };

                    log.k = k;
                    log.newW = image.w;
                    log.newH = image.h;
                    log.marginLeft = image.margins.left;
                    log.marginTop = image.margins.top;

                    yapyap.log('debug', 'fitImage', log);
                    
                    $img
                        .width(image.w ? image.w : container.w)
                        .height(image.h ? image.h : container.h)
                        .css('margin-top', image.margins.top + 'px')
                        .css('margin-left', image.margins.left + 'px');
                }
            }
        };
    }
});