goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
goog.require('goog.userAgent.product.isVersion');
goog.require('goog.userAgent.flash');
goog.require('goog.userAgent.platform');
goog.require('goog.userAgent.adobeReader');


$(function()
{
    if ($.browser.mozilla)
        $("form").attr("autocomplete", "off");

    $('#left-navigation li.catalog > a').bind('click', function(event)
    {
        event.preventDefault();
        var $products = $('ul.products', $(this).closest('li.catalog'));
        if ($products.is(':visible'))
        {
            $('ul.products', $(this).closest('li.catalog')).slideUp('fast');
        }
        else
        {
            $('ul.products:visible').slideUp('fast');
            $products.slideDown('fast');
        }
    });

    $('#navigation  li').bind('mouseenter', function(event)
    {
        var $sub = $('ul', $(this).closest('li')).first();
        $sub.removeClass('hidden');
        $(this).addClass('active');
    });

    $('#navigation  li').bind('mouseleave', function(event)
    {
        var $sub = $('ul', $(this).closest('li')).first();
        $sub.addClass('hidden');
        $(this).removeClass('active');
    });

    bindBookmarkLink();

    $('#feedback-form textarea[name=message]').val($($('#feedback-form textarea[name=message]').val()).text());
    $('#feedback-form.virgin textarea[name=message]').bind('focus', function()
    {
        $(this).unbind('focus').val('');
        $('#feedback-form').removeClass('virgin')
    });

    $('#feedback').tabSlideOut({
        tabHandle: '.handle',                              //class of the element that will be your tab
        pathToTabImage: baseUrl + '/static/images/feedback.png',          //path to the image for the tab *required*
        imageHeight: '124px',                               //height of tab image *required*
        imageWidth: '40px',                               //width of tab image *required*
        tabLocation: 'right',                               //side of screen where tab lives, top, right, bottom, or left
        speed: 300,                                        //speed of animation
        action: 'click',                                   //options: 'click' or 'hover', action to trigger animation
        topPos: '200px',                                   //position from the top
        fixedPosition: true                               //options: true makes it stick(fixed position) on scroll
    });
    $('#feedback').removeClass('hidden');

    $('#feedback-form').bind('submit', function(event)
    {
        event.preventDefault();
        if ($(this).hasClass('virgin') || !$('textarea[name=message]', this).val())
        {
            alert("Veuillez écrire un message avant de l'envoyer.")
        }
        else
        {
            data = {};
            data['message'] = $('textarea[name=message]', this).val();
            data['email'] = $('input[name=email]', this).val();
            var infos = [];
            infos.push('Compte utilisateur: ');
            infos.push(userAccount ? userAccount : 'déconnecté');
            infos.push('\n');
            infos.push(goog.userAgent.PLATFORM);
            if (goog.userAgent.platform.VERSION)
            {
                infos.push(" ");
                infos.push(goog.userAgent.platform.VERSION)
            }
            infos.push('\n');
            var BROWSERS = ['OPERA','IE','FIREFOX','CAMINO','IPHONE','IPAD','ANDROID','CHROME','SAFARI'];
            for (var i in  BROWSERS)
            {
                if (goog.userAgent.product[BROWSERS[i]])
                {
                    infos.push(BROWSERS[i]);
                    infos.push(" ");
                    infos.push(goog.userAgent.product.VERSION);
                    infos.push('\n');
                }
            }
            infos.push("Résolution de l'écran: ");
            infos.push(screen.width);
            infos.push("x");
            infos.push(screen.height);
            infos.push('\n');
            infos.push("Flash: ");
            infos.push(goog.userAgent.flash.HAS_FLASH ? goog.userAgent.flash.VERSION : "non");
            infos.push('\n');
            infos.push("PdfReader: ");
            infos.push(goog.userAgent.adobeReader.HAS_READER ? goog.userAgent.adobeReader.VERSION : "non");
            infos.push('\n');
            infos.push("Page courante: ");
            infos.push(window.location);

            data["browserInfos"] = infos.join('');
            $.post(baseUrl + "/pub/feedback/ajax/submit", data, function(response)
            {
                if (response.success)
                {
                    alert('Votre message a bien été envoyé.')
                }
                else
                {
                    alert('Il y a eu un problème dans la transmission du message, veuillez réessayer plus tard.');
                }
                $('#feedback .handle').trigger('click');
            });
        }
    });
});

function bindBookmarkLink()
{
    $("a#add_flyprint_bookmark").bind('click keyup', function(event)
    {
        $(this).blur();
        if ($.browser.safari) alert("Press CTRL + D or CMD + D");
        else window.external.AddFavorite("http://www.flyprint.be", "Flyprint");
        return false;
    });
}

