/* BEGIN nav */
function showMenu (id)
{
    if ($(this).hasClass('yuimenubaritemlabel-hassubmenu'))
    {
        $(this).addClass('yuimenubaritemlabel-hassubmenu-selected');
    }
    $('#' + id).parent().siblings('dd').fadeIn(150);
    menu_active[id] = true;
    
}

function clearMenu (id)
{
    $('#' + id).removeClass('yuimenubaritemlabel-hassubmenu-selected').parent().siblings('dd').fadeOut (300);
}
/* END nav */

/* BEGIN twitter */
function setRated()
{
		
    $('.rating_label').each(function ()
    {
        $(this).html('Aanbevolen').css({ 'color': '#999', 'cursor': 'default' }).parent().css('cursor', 'default').parent().css('cursor', 'default');
    });
    
    $('.rating_results').each(function()
    {
        $(this).css({ 'visiblity': 'visible', 'color': '#999', 'cursor': 'default'});
    });
}

function triggerOver (div)
{
    if ($.cookie('rated') == 'true')
    {
        return false;
    }
    
    $(div).find('.rating_results').css('visiblity', 'hidden');
    $(div).find('.rating_label').html('Aanbevelen');
    
}

function triggerOut (div)
{
    
    if ($.cookie('rated') == 'true')
    {
        return false;
    }
    
    $(div).find('.rating_results').css('visiblity', 'visible');
    if ($(div).find('.rating_label').html() != '')
    {
        $(div).find('.rating_label').html('Aanbevolen');
    }
    
}

function makeRequest(article_id)
{
    if ($.cookie('rated') == 'true')
    {
        setRated();
        return false;
    }
    
    $.article_id = article_id;
    
    $.get('/ajax/rating', { id: $.article_id}, function(data)
    {
        if (data != 'undefined')
        {
            var oResults = eval ("(" + data + ")");
            
            $('.rating_results').each(function()
            {
                $(this).html(oResults.rating + 'x');
            });
            
            /* we want an expiry of 15 mins, so 1 day / 24 hours / 4 segments equals 15 mins */
            $.cookie ('rated', 'true', { expires: 1 / 24 / 4, path: '/article/' + $.article_id });
            
            setRated();
        }
    });
}
/* END twitter */

/* BEGIN poll */
function loadPoll(poll_id, tpl)
{
    $('#poll_container').load('/ajax/loadpoll', { id: poll_id, tpl: tpl});
}

function vote (poll_id)
{
    var answers = '';
    $('#poll_frm').find('input:checked').each(function()
    {
        answers += $(this).val() + ',';
    });
    
    answers = answers.replace(/,$/, '');
    
    if (answers != '')
    {
        $.get('/ajax/votepoll', { id: poll_id, questions: answers }, function (data)
        {
            if (data != 'true')
            {
                return;
            }
            
            $.cookie ('voted' + poll_id, 'true');
            loadPoll(poll_id, 'results');
            
        });
    }
    else
    {
        alert ('Vote on something!');
    }
    
}

function getPoll()
{

    $.get('/ajax/getpoll', function(data)
    {
        if (data != '')
        {
            var poll_id = eval (data);
            if (poll_id > 0)
            {
                if ($.cookie('voted' + poll_id) == 'true') {
                	loadPoll(poll_id, 'results');
                	return;
                }
                
                $.get('/ajax/canvote', { id: poll_id }, function (data)
                {
                    if (data != 'true')
                    {
                        loadPoll(poll_id, 'results');
                    }
                	else
                	{
                	    loadPoll(poll_id, 'poll');
                	}
                	
                });
    
            }
        }
    });
    
}
/* END poll */

/* BEGIN campaign poll */
function loadCampaignPoll(poll_id, tpl)
{
    $('#poll_container').load('/ajax/load-campaign-poll', { id: poll_id, tpl: tpl});
}

function voteCampaignPoll (poll_id)
{
    var answers = '';
    $('#poll_frm').find('input:checked').each(function()
    {
        answers += $(this).val() + ',';
    });
    
    answers = answers.replace(/,$/, '');
    
    if (answers != '')
    {
        $.get('/ajax/vote-campaign-poll', { id: poll_id, questions: answers }, function (data)
        {
            if (data != 'true')
            {
                return;
            }
            
            $.cookie ('voted-campaign-' + poll_id, 'true');
            loadCampaignPoll (poll_id, 'results');
            
        });
    }
    else
    {
        alert ('Vote on something!');
    }
    
}

function getCampaignPoll()
{

    $.get('/ajax/get-campaign-poll', function(data)
    {
        if (data != '')
        {
            var poll_id = eval (data);
            if (poll_id > 0)
            {
                if ($.cookie('voted-campaign-' + poll_id) == 'true') {
                	loadCampaignPoll(poll_id, 'results');
                	return;
                }
                
                $.get('/ajax/canvote-campaign', { id: poll_id }, function (data)
                {
                    if (data != 'true')
                    {
                        loadCampaignPoll(poll_id, 'results');
                    }
                	else
                	{
                	    loadCampaignPoll(poll_id, 'poll');
                	}
                	
                });
    
            }
        }
    });
    
}
/* END campaign poll */