$(document).ready(function()
{
	$('body').browserDetection();
    initMenuSelectedBackground();
    fixFooterPosition();
});

function initMenuSelectedBackground()
{
    $('.menu-selected-background').each(function()
    {
        var $background = $(this);
        var parentWidth = $background.parent().width();
        var $image = $background.find('img');
        var padding = 12;
        
        if ($image.attr('src').indexOf('submenu') != -1)
        {
            padding = 7;
        }

        $image.width(parentWidth + padding);
    });
}

function formDefaultInput($input)
{
    var defaultValue = $input.val();
        $input.data('default', defaultValue);

    $input
        .focus(function()
        {
            if ($input.val() == defaultValue)
            {
                $input.val('');
            }
        })
        .blur(function()
        {
            if ($input.val() == '')
            {
                $input.val(defaultValue);
            }
        });
}


function initRhymeItems()
{
    var $items = $('div.rhyme-item');
    initShare($items);
        $items.filter(':not(.has-voted)').find('div.vote')
              .hover(
                function()
                {
                    var $parent = $(this).closest('.rhyme-item');
                    if (!$parent.hasClass('has-voted'))
                    {
                        $(this).addClass('vote-hover');
                    }
                },
                function()
                {
                    $(this).removeClass('vote-hover');
                }
              )
              .click(createRhymeVote);
}

function createRhymeVote()
{
    var $rhyme = $(this).closest('.rhyme-item, .teaser-rhyme-item');

    $('#vote-popup-ok-link').unbind('click')
                             .click(function()
                             {
                                popup.close();
                                doVoteRequest($rhyme);
                             });

    popup.open('vote-popup');
}

function doVoteRequest($rhyme)
{
    var rhymeId = $rhyme.find('div.metadata .rhyme-id').text();

    var $vote = $rhyme.find('div.vote');
        $vote.children('.loader').show();
        $vote.children('.vote-content').hide();

    $.post(websiteUrl + 'service/rhyme/vote', {rhyme_id: rhymeId}, function(response)
    {
        $rhyme.find('div.vote').unbind('click');
        $rhyme.addClass('has-voted')
              .find('div.vote-count')
              .html(response.count);
        hasVotedBefore = true;

        $vote.children('.loader').hide();
        $vote.children('.vote-content').show();

    }, 'json');
}

function fixFooterPosition()
{
   var canvasHeight = $('#canvas').height();
   var windowHeight = $(window).height();

   if (canvasHeight < windowHeight)
   {
      var difference = windowHeight - canvasHeight;
      var contentHeight = $('#content').height();
      $('#content').height(contentHeight + difference);
   }
}
