/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var Site = {};

Site.commentPage = 0;
Site.commentToggled = false;
Site.currentQuestion = 0;

Site.dudes = new Array();
Site.dudes[1] = "Arne Ahlström";
Site.dudes[2] = "Peter Appelquist";
Site.dudes[3] = "Lena Gustafson Wiberg";
Site.dudes[4] = "Kennet Johansson";
Site.dudes[5] = "Thomas Sjöström";
Site.dudes[6] = "Daniel Sundström";
Site.dudes[11] = "Robin Carlander";
Site.dudes[8] = "Tomas Petersson";
Site.dudes[9] = "Jimmy Bergström";
Site.dudes[10] = "Patrik Svensson";
Site.dudes[7] = "Pernilla Dahlquist";
Site.dudes[12] = "Patrik Bergner";
Site.dudes[13] = "Martin Grahn";
Site.dudes[15] = "Jörgen Gladh";
Site.dudes[16] = "Kerstin Högfeldt";
Site.dudes[14] = "Bengt Cederberg";

Site.pics = new Array();
Site.pics[1] = "aik_2_fb.jpg";
Site.pics[2] = "brommapojkarna_1_fb.jpg";
Site.pics[3] = "djurgarden_1_fb.jpg";
Site.pics[4] = "elfsborg_2_fb.jpg";
Site.pics[5] = "gais_1_fb.jpg";
Site.pics[6] = "gefle_2_fb.jpg";
Site.pics[11] = "hacken_1_fb.jpg";
Site.pics[8] = "halmstad_2_fb.jpg";
Site.pics[9] = "hammarby_1_fb.jpg";
Site.pics[10] = "helsingborg_2_fb.jpg";
Site.pics[7] = "ifk_1_fb.jpg";
Site.pics[12] = "kalmar_1_fb.jpg";
Site.pics[13] = "malmo_2_fb.jpg";
Site.pics[15] = "orebro_1_fb.jpg";
Site.pics[16] = "orgryte_1_fb.jpg";
Site.pics[14] = "trelleborg_2_fb.jpg";

PushFacebook = function(sid)
{
    FB.ensureInit(function () {
        FB.Connect.streamPublish('',{
            'name':'Svenska Supporterlaget',
            'href':'http://www.supporterlaget.se',
            'description':Site.dudes[sid]+' är min favorit i Svenska Supporterlaget!',
            'media':[{
                'type':'image',
                'src':'http://www.supporterlaget.se/images/candidates/'+Site.pics[sid],
                'href':'http://www.supporterlaget.se'
            }]
        });
    });
    return false;
}

PushTwitter = function(sid)
{
    u='http://www.supporterlaget.se';

    t=document.title;
    w = window.open('http://twitter.com/home?status='+encodeURIComponent('Hejar på '+Site.dudes[sid]+' i supporterlaget! - '+'http://www.supporterlaget.se'),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

PushThumbUp = function(sid)
{
    $.ajax({
        type: 'post',
        url: '/action.php',
        data: 'action=thumbup&thumbup='+sid,
        dataType: 'json',
        success: function(json){
            if(json.result == 'error'){
                alert('Du har redan gett '+Site.dudes[sid]+' tummen upp!');
            }
            else
            {
        //alert('Du gav '+Site.dudes[sid]+' tummen upp!');
        }
        }
    });
    return false;
}

PushMail = function(sid)
{
    $('#clubDudeContactDialog').modal();

    $('#clubDudeContactDialog form').resetForm();

    $('input[name=team]').val(''+sid);
    $('#clubDudeContactName').html(Site.dudes[sid]);
    Cufon.replace('#clubDudeContactDialog');

    return false;
}

Site.klubbenToggle = function()
{
    if($('#klubbenForm').css('display') == 'none')
    {
        $('#klubbenForm').slideDown(500);
        $('#klubbenToggleBtn').attr('src', '/images/buttons/arrow_down.gif');
    }
    else
    {
        $('#klubbenForm form').resetForm();
        $('#klubbenForm').slideUp(500);
        $('#klubbenErrorMessage').slideUp(500);
        $('#klubbenToggleBtn').attr('src', '/images/buttons/arrow_right.gif');
    }
    return false;
}

Site.supportenSend = function(form){
    $.ajax({
        type: 'post',
        url: "/action.php",
        data: $(form).serialize(),
        dataType: 'json',
        success: function(json){
            if(json.result == 'error')
            {
                alert(json.info);
            }else
            {
                $.modal.close();
				alert('sent');
            }
        }
    });
    return false;
}

Site.klubbenSend = function(form){
    $.ajax({
        type: 'post',
        url: "/action.php",
        data: $(form).serialize(),
        dataType: 'json',
        success: function(json){
            if(json.result == 'error')
            {
                alert(json.info);
            }else
            {
                $.modal.close();
            }
        }
    });
    return false;
}

Site.contactClub = function(club){
    $('#clubContactDialog').modal();

    var dudes = new Array();
    dudes[1] = "Jimmy Wallingstam";
    dudes[2] = "Christer Hellqvist";
    dudes[3] = "Tomas af Geijerstam";
    dudes[4] = "Eric Sj&ouml;lin";
    dudes[5] = "Tony Balog";
    dudes[6] = "Anders Berg";
    dudes[11] = "Marcus Jodin";
    dudes[8] = "Sven Elmberg";
    dudes[9] = "Urban Rybrink";
    dudes[10] = "Frederik Ericsson";
    dudes[7] = "Linda &aring;kerberg";
    dudes[12] = "Bj&ouml;rn Andersson";
    dudes[13] = "Peter &Aring;hlander";
    dudes[15] = "Patrik Ahlstrand";
    dudes[16] = "Ulrika Rosén";
    dudes[14] = "Barbro Bengtsson";

    dudes[17] = "Tommy Theorin";

    $('#clubContactName form').resetForm();

    $('input[name=team]').val(''+club);
    $('#clubContactName').html(dudes[club]);
    Cufon.replace('#clubContactDialog');

    return false;
}

Site.initVoteBars = function(number, percent)
{
    swfobject.embedSWF("/flash/votebar_vagrat.swf", "voteResult_"+number, "365", "8", "9.0.0",null, {
        percent: percent
    }, {
        wmode: 'transparent'
    }, {});
}

Site.vote = function()
{
    $.ajax({
        type: 'post',
        url: "/action.php",
        data: $('#voteForm form').serialize(),
        dataType: 'json',
        success: function(json){
            if(json.result == 'error')
            {
                $('#voteErrorMessage').html(json.info).slideDown(500);
            }else
            {
                $('#dailyQuestion').html(json.info).fadeIn(500);
                if(json.result){
                    var highest = 0;
                    for(i = 0; i < json.result.length; i++){
                        swfobject.embedSWF("/flash/votebar_vagrat.swf", "voteResult_"+(i+1), "365", "8", "9.0.0",null, {
                            percent: /*Math.round((json.result[i] / highest)*100)*/ json.result[i]
                        }, {
                            wmode: 'transparent'
                        }, {});
                    }
                }
            }
        }
    });
    return false;
}

Site.nextQuestion = function(id)
{
    $.ajax({
        type: 'post',
        url: '/action.php',
        data: 'action=daily&id='+id,
        dataType: 'json',
        success: function (json){
            Site.currentQuestion = id;
            $('#dailyQuestion').html(json.info);
            if(json.result){
                var highest = 0;
                for(i = 0; i < json.result.length; i++){
                    swfobject.embedSWF("/flash/votebar_vagrat.swf", "voteResult_"+(i+1), "365", "8", "9.0.0",null, {
                        percent: /*Math.round((json.result[i] / highest)*100)*/ json.result[i]
                    }, {
                        wmode: 'transparent'
                    }, {});
                }
            }
            if(Site.commentToggled)
                Site.toggleComments();
        }
    });
    return false;
}

Site.prevQuestion = function(id)
{
    $.ajax({
        type: 'post',
        url: '/action.php',
        data: 'action=daily&id='+id,
        dataType: 'json',
        success: function (json){
            Site.currentQuestion = id;
            $('#dailyQuestion').html(json.info);
            if(json.result){
                var highest = 0;
                for(i = 0; i < json.result.length; i++){
                    swfobject.embedSWF("/flash/votebar_vagrat.swf", "voteResult_"+(i+1), "365", "8", "9.0.0",null, {
                        percent: /*Math.round((json.result[i] / highest)*100)*/ json.result[i]
                    }, {
                        wmode: 'transparent'
                    }, {});
                }
            }
            if(Site.commentToggled)
                Site.toggleComments();
        }
    });
    return false;
}

Site.tips = function(form)
{
    $.ajax({
        type: "post",
        url: "/action.php",
        data: $(form).serialize(),
        dataType: 'json',
        success: function(json){
            if(json.result == 'error')
            {
                $('#tipsErrorMessage').html(json.info).slideDown(500);
            }else
            {
                if($('#tipsText').html() != json.info)
                    $('#tipsText').html(json.info).fadeIn(500);
                $('#tipsErrorMessage').fadeOut(500).empty();
                $('#tipsTextImage').attr('src', '/images/text/har_du_fler_forslag.gif').attr('width', '149');
                Site.tipsToggle();
            }
        }
    });
    return false;
}

Site.tipsToggle = function()
{
    if($('#tipsForm').css('display') == 'none')
    {
        $('#tipsForm').slideDown(500);
        $('#tipsToggleBtn').attr('src', '/images/buttons/arrow_down.gif');
    }
    else
    {
        $('#tipsForm form').resetForm();
        $('#tipsForm').slideUp(500);
        $('#tipsErrorMessage').slideUp(500);
        $('#tipsToggleBtn').attr('src', '/images/buttons/arrow_right.gif');
    }
    return false;
}

Site.comment = function(form, question)
{
    $.ajax({
        type: "post",
        url: "/action.php",
        data: $(form).serialize()+'&question_id='+question,
        dataType: 'json',
        success: function(json){
            if(json.result == 'error')
            {
                $('#commentFormError').html(json.info).slideDown(500);
            }else
            {
                if(Site.commentToggled)
                    Site.commentShow(Site.currentQuestion, 0);
                else
                    Site.toggleComments();
                $('#commentForm form').resetForm();
                $('#commentFormError').slideUp(500).empty();
            }
        }
    });
    return false;
}

Site.toggleComments = function()
{
    if($('#questionComments').css('display') == 'none')
    {
        $('#commentsToggleBtn').attr('src', '/images/buttons/arrow_down.gif');
        Site.commentToggled = true;
        Site.commentShow(Site.currentQuestion, Site.commentPage);
    }
    else
    {
        $('#commentsToggleBtn').attr('src', '/images/buttons/arrow_right.gif');
        $('#questionComments').slideUp(500);
        Site.commentToggled = false;
    }
    return false;
}

Site.scrollInited = false;

Site.commentShow = function(question, page)
{
    $.ajax({
        type: "post",
        url: "/action.php",
        data: 'action=comment_page&question='+question+'&page='+page,
        dataType: 'html',
        success: function(response){
            $("#questionComments").html(response).slideDown(500);
            $('#commentBox').jScrollPane({
                scrollbarWidth: 25,
                scrollbarMargin: 0,
                dragMinHeight: 50,
                dragMaxHeight: 50
            });
        }
    });
    return false;
}

Site.reportComment = function(id)
{
    if(confirm('Är du säker på att du vill anmäla detta inlägg?\nEtt meddelande kommer att skickas till redaktören för verifiering.'))
    {
        $.ajax({
            type: "post",
            url: "/action.php",
            data: 'action=report_comment&id='+id,
            success: function(){
                alert('Inlägget har rapporterats till redaktören.');
            }
        });
    }
    return false;
}

Site.answerComment = function()
{
    //$.scrollTo('#commentForm');
    $('#commentForm form textarea[name=comment]').focus();
    return false;
}

Site.gotoArchive = function()
{
    $.ajax({
        type: "post",
        url: "/action.php",
        data: 'action=archive',
        dataType: 'html',
        success: function(html){
            if(Site.commentToggled)
                Site.toggleComments();
            $('#questionComments').empty();
            $('#commentForm').slideUp(500);
            $('#qToggler').slideUp(500);
            $('#dailyQuestion').html(html).slideDown(500);
            $('#archiveBox').jScrollPane({
                scrollbarWidth: 25,
                scrollbarMargin: 0,
                dragMinHeight: 50,
                dragMaxHeight: 50
            });
        }
    });
    return false;
}

Site.gotoQuestion = function(question)
{
    $.ajax({
        type: "post",
        url: "/action.php",
        data: 'action=daily&id='+question,
        dataType: 'json',
        success: function(json){
            Site.currentQuestion = question;
            $('#dailyQuestion').html(json.info).slideDown(500);
            $('#commentForm').slideDown(500);
            $('#qToggler').slideDown(500);
            if(json.result){
                var highest = 0;
                for(i = 0; i < json.result.length; i++){
                    swfobject.embedSWF("/flash/votebar_vagrat.swf", "voteResult_"+(i+1), "365", "8", "9.0.0",null, {
                        percent: json.result[i] /*Math.round((json.result[i] / highest)*100)*/
                    }, {
                        wmode: 'transparent'
                    }, {});
                }
            }
        }
    });
    return false;
}

Site.candidates = {};
Site.currentCandidate = 0;
Site.currentCategory = 0;

Site.getCandidates = function(cat, candidate)
{
    $.ajax({
        type: 'post',
        url: '/action.php',
        data: 'action=get_candidates&category='+cat,
        dataType: 'json',
        success: function(json){
            if(json.result == 'voted'){
                $('#votedModal').modal();
            }
            else
            {
                Site.candidates = json.info;
                Site.currentCandidate = candidate;
                Site.currentCategory = cat;
                Site.populatePreviewModal();
                $('#previewCandidate').modal();
            }
        }
    });
}

Site.populatePreviewModal = function()
{
    var candidate = Site.candidates[Site.currentCandidate];
    $('#voteDialogSupporter img').attr('src', candidate.card);
    $('#voteDialogSupporterInfo h1').html(candidate.heading);
    $('#supporterDescription').html(candidate.description);
    
}

Site.swapCandidate = function()
{
    if(Site.currentCandidate == 0)
        Site.currentCandidate = 1;
    else
        Site.currentCandidate = 0;
    Site.populatePreviewModal();
// Cufon.replace('#previewCandidate', {fontFamily: 'SerifaBTRoman'});
}

Site.voteForCandidate = function()
{
    $.ajax({
        type: 'post',
        url: '/action.php',
        data: 'action=vote_candidates&category='+Site.currentCategory+'&answer='+Site.candidates[Site.currentCandidate].id,
        dataType: 'json',
        success: function(json){
            if(json.result == 'counted'){
                $.modal.close();
                $('#voteCount').modal();
                $('#shareThisText').attr('src', '/images/text/dela_det_har_pa.gif');
                $('#vote_0').replaceWith('<div id="vote_0">&nbsp;</div>');
                $('#vote_1').replaceWith('<div id="vote_1">&nbsp;</div>');
                $('#vote_0Name').html(json.info[0].name);
                $('#vote_1Name').html(json.info[1].name);
                $('#embedCode').val('<div style="width: 242px;"><iframe src="http://www.supporterlaget.se/badge.php?team='+Site.currentCategory+'" width="242" height="246" scrolling="no" frameborder="0"></iframe><p style="text-align: center; margin: 1px; font-style: Geneva; font-size: 12px;"><a href="http://www.supporterlaget.se/">Svenska Supporterlaget</a></p></div>');
                $('#badgeImage').attr('src','/images/badge/'+Site.currentCategory+'.png');
                var highest = json.info[0].count + json.info[1].count;
                swfobject.embedSWF("/flash/votebar_lodrat.swf", "vote_0", "31", "165", "9.0.0",null, {
                    percent: Math.round((json.info[0].count / highest*100))
                }, {
                    wmode: 'transparent'
                }, {});
                swfobject.embedSWF("/flash/votebar_lodrat.swf", "vote_1", "31", "165", "9.0.0",null, {
                    percent: Math.round((json.info[1].count / highest*100))
                }, {
                    wmode: 'transparent'
                }, {});
                Cufon.replace('#t4', {
                    fontFamily: 'SerifaBTLight'
                });
                //Cufon.replace('#vote_0Name', {fontFamily: 'SerifaBTBold'});
                //Cufon.replace('#vote_1Name', {fontFamily: 'SerifaBTBold'});
                Cufon.replace('#t5', {
                    fontFamily: 'SerifaBTBlack'
                });
                Cufon.replace('#t6', {
                    fontFamily: 'SerifaBTBold'
                });
            }
            else
            {
                $.modal.close();
                $('#voteCount').modal();
                $('#shareThisText').attr('src', '/images/text/dela_pa.gif');
                $('#t5').html('Du har redan röstat!');
                $('#t6').html('Du kan rösta en gång per klubb och per dag till och med 21/10. Kom gärna tillbaka imorgon!');
                $('#vote_0').replaceWith('<div id="vote_0">&nbsp;</div>');
                $('#vote_1').replaceWith('<div id="vote_1">&nbsp;</div>');
                $('#vote_0Name').html(json.info[0].name);
                $('#vote_1Name').html(json.info[1].name);
                $('#embedCode').val('<div style="width: 242px;"><iframe src="http://www.supporterlaget.se/badge.php?team='+Site.currentCategory+'" width="242" height="246" scrolling="no" frameborder="0"></iframe><p style="text-align: center; margin: 1px; font-style: Geneva; font-size: 12px;"><a href="http://www.supporterlaget.se/">Svenska Supporterlaget</a></p></div>');
                $('#badgeImage').attr('src','/images/badge/'+Site.currentCategory+'.png');
                var highest = json.info[0].count + json.info[1].count;
                swfobject.embedSWF("/flash/votebar_lodrat.swf", "vote_0", "31", "165", "9.0.0", null, {
                    percent: Math.round((json.info[0].count / highest*100))
                }, {
                    wmode: 'transparent'
                }, {});
                swfobject.embedSWF("/flash/votebar_lodrat.swf", "vote_1", "31", "165", "9.0.0", null, {
                    percent: Math.round((json.info[1].count / highest*100))
                }, {
                    wmode: 'transparent'
                }, {});
                Cufon.replace('#t4', {
                    fontFamily: 'SerifaBTLight'
                });
                //Cufon.replace('#vote_0Name', {fontFamily: 'SerifaBTBold'});
                //Cufon.replace('#vote_1Name', {fontFamily: 'SerifaBTBold'});
                Cufon.replace('#t5', {
                    fontFamily: 'SerifaBTBlack'
                });
                Cufon.replace('#t6', {
                    fontFamily: 'SerifaBTBold'
                });
            }
        }
    });
}
function VotePopup(cat, candidate)
{
    Site.getCandidates(cat, candidate);
}

Site.shareFacebook = function()
{
    u='http://www.supporterlaget.se';
    //u='http://supporterlandslaget.test.phosdev.se';

    t=document.title;
    w = window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent('http://www.supporterlaget.se/?share=facebook')+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

Site.shareTwitter = function()
{
    u='http://www.supporterlaget.se';//location.href;
    //u='http://supporterlandslaget.test.phosdev.se';

    t=document.title;
    w = window.open('http://twitter.com/home?status='+encodeURIComponent('har röstat på sin representant i Svenska Supporterlaget. Gör det du med!  - '+'http://www.supporterlaget.se'),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

Site.showAbout = function()
{
    $('#aboutModal').modal();
    Cufon.replace('#t10', {
        fontFamily: 'SerifaBTBold'
    });
    Cufon.replace('#t11', {
        fontFamily: 'SerifaBTRoman'
    });
    Cufon.replace('#t12', {
        fontFamily: 'SerifaBTRoman'
    });
    Cufon.replace('#t13', {
        fontFamily: 'SerifaBTBold'
    });

    return false;
}