(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

$('document').ready(function () {
    // Preload
    $.preLoadImages(
        '/images/tag-erosseg1.png', '/images/tag-erosseg2.png', '/images/tag-erosseg3.png',
        '/images/tag-meret1.png', '/images/tag-meret2.png', '/images/tag-meret3.png', '/images/tag-meret4.png',
        '/images/tag-tudasszint1.png', '/images/tag-tudasszint2.png', '/images/tag-tudasszint3.png', '/images/tag-tudasszint4.png',
        '/images/logo_4drops.hover.png', '/images/loading.gif'
    );

    // Make login fields empty on focus
    var default_values = Array();
    $("input[name='signin[username]'], input[name='signin[password]'], input#msearch").each(function () {
        if (!default_values[$(this).attr('id')]) {
            default_values[$(this).attr('id')] = $(this).val();
        }
    }).focus(function () {
        if ($(this).val() == default_values[$(this).attr('id')]) {
            $(this).val('');
        }
    }).blur(function () {
        if ($(this).val() == '') {
            $(this).val(default_values[$(this).attr('id')]);
        }
    });

    $('#header ul li').hover(
        function () {
            $('li.info', $(this).parent()).text($('a', $(this)).attr('title'));
        },
        function () {
            $('li.info', $(this)).text('info');
        }
    );  
    
    $('#hearing-aid-form div.tag:lt(3)').each(function () {
        var div = $(this);
        $('li', div).each(function (i) {
            $(this).hover(
                function () {
                    div.addClass('active' + (i+1));
                },
                function () {
                    div.removeClass('active' + (i+1));
                }
            );
        });
        $('input:checkbox', div).change(function () {
            var index = $('input:checkbox', div).index($(this)); 
            if ($(this).is(':checked')) {
                div.addClass('selected' + (index+1));
            } else {
                div.removeClass('selected' + (index+1));
            } 
        });
        $('input:checkbox', div).each(function () {
            var index = $('input:checkbox', div).index($(this));
            if ($(this).is(':checked')) {
                div.addClass('selected' + (index+1));
            }
        }); 
    });
    
    $('div.powered img').hover(
        function () {
            $(this).attr('src', '/images/logo_4drops_hover.png');
        },
        function () {
            $(this).attr('src', '/images/logo_4drops.png');
        }
    );

    $('a.barrier').click(function () {
        if ($.cookie('barrier') == 'true') {
            $.cookie('barrier', false, {path: 'http://'+location.host + '/'});
        } else {
            $.cookie('barrier', true, {path: 'http://'+location.host + '/'});
        }
    });
/*
    $('#tagSearch input[type=checkbox]').click(function () {
        //alert("hello");
        //$('#tagSearch').submit();
        
        $('#hearing-aid').html('<center><img src="/images/loading.gif" alt="" title="" /></center>');
       $.ajax({
            type: 'post',
            url: $('#tagSearch').attr(''),
            //context: $(this).parents('tr').children('td:eq(2)'),
            success: function (res) {
                $('#content').html(res);
            },
            error: function (res) {
                alert('Error');
            },
            complete: function () {
                $(this).removeClass('loading');
            }
        });
    });
*/
});


function postTags() {
//   $('#tagSearch').submit();

    var token = $('#tagSearch input[name=tags[_csrf_token]]').val();
    var values={};
    var str = "";
    $('#tagSearch input').each(function(i, el) {
       values[el.name] = $(el).val();
       if ($('input[value='+$(el).val()+']').attr('checked'))
        str += el.name + "=" + $(el).val() + "&";
    });
//    alert(atr);


   $('#hearing-aid').html('<center><img src="/images/loading.gif" alt="" title="" /></center>');
   $.ajax({
        type: 'post',
        url: $('#tagSearch').attr(''),
        //context: $(this).parents('tr').children('td:eq(2)'),
        async: false,
        data: str + 'tags[_csrf_token]='+token,
        success: function (res) {
            $('#hearing-aid').html($('.hearing-aid-index', res));
        },
        error: function (res) {
            alert('Error');
        },
        complete: function () {
            $(this).removeClass('loading');
        }
    }); 
}
