/**
 * Dragon Age Origins
 *
 * @package Dragon Age Origins
 * @author  Michal Luberda <luberda@interlogica.it>
 */
 
/**
 * main.js
 *
 * @version 0.1.4
 * @package Dragon Age Origins
 * @author  Michal Luberda <luberda@interlogica.it>
 */
$ (document).ready (function () {
    
    // set default language
    if ($ ("#span-" + $ ("#span-lang").text ()).length) {
        $ ("#span-" + $ ("#span-lang").text ()).addClass ("border");
    }
    if ($ ("#select-lang").length) {
        $ ("#select-lang option[value='" + $ ("#span-lang").text () + "']").attr ("selected", "selected");
        $ ("#select-lang").change (function () {
            $.post (
                "lang", {
                    st_lang: $ (this).val (),
                    st_href: ""
                },
                function (st) {
                    location.href = st;
                }
            );
        });
    }
    
    // change language
    if (! $ ("#h3-team").length) {
        $ (".flag").click (function () {
            $ ("#inputh-lang").val ($ (this).attr ("id").substr ($ (this).attr ("id").indexOf ("-") + 1));
            $ (".flag").removeClass ("border");
            $ (this).addClass ("border");
        });
        
        $ ("#inputs-enter").click (function () {
            $.post (
                "lang", {
                    st_lang: $ ("#inputh-lang").val (),
                    st_href: $ ("#span-href").text () + "/main"
                },
                function (st) {
                    location.href = st;
                }
            );
        });
    }
    else {
        $ (".flag").css ({
            cursor: "default"
        });
    }
    
    // accordion
    if ($ ("#div-accordion").length) {
        $ ("#div-accordion").accordion ();
    }
    
    // leaderboard update
    if ($ ("#div-teams").length) {
        ob_leaderboard._init ();
    }
    
    // timeline
    ob_timeline._init ();
    if ($ ("#div-timeline").length) {
        ob_timeline._set_live ();
    }
    
    // score
    if ($ ("#div-score").length) {
        ob_score._init ();
    }
});

// score
ob_score = {
    id_intval: false,
    
    _init: function ()
    {
        $.post (
            "score-" + $ ("#div-team-name").text (), {
            },
            function (st) {
                $ ("#div-leaderboard").html (st);
            }
        );
        
        ob_score.id_intval = window.setInterval (function () {
            $.post (
                "score-" + $ ("#div-team-name").text (), {
                },
                function (st) {
                    $ ("#div-leaderboard").html (st);
                }
            );
        }, 1000 * 60 * 5);
    }
};

// leaderboard
ob_leaderboard = {
    id_intval: false,
    
    _init: function ()
    {
        $.post (
            "leaderboard", {
            },
            function (ar) {
                ob_leaderboard._render (ar);
            },
            "json"
        );
        
        ob_leaderboard.id_intval = window.setInterval (function () {
            $.post (
                "leaderboard", {
                },
                function (ar) {
                    ob_leaderboard._render (ar);
                },
                "json"
            );
        }, 1000 * 60 * 6);
    },
    
    _render: function (ar)
    {
        for (var i in ar) {
            for (var j in ar[i]) {
                st_val = ar[i][j];
                ar_split = ar[i][j].split ("|");
                if (ar_split[0] != "") {
                    $ ("#div-accordion div:eq(" + i + ") table tbody tr:eq(" + j + ") td:eq(1)").html ("<a href='team-" + (ar_split[0] ? ar_split[0] : "") + "'><span class='" + (ar_split[2] == "true" ? "out" : ("team-" + (ar_split[0] ? ar_split[0] : ""))) + "'></span></a>");
                    $ ("#div-accordion div:eq(" + i + ") table tbody tr:eq(" + j + ") td:eq(2)").html (ar_split[1] ? ar_split[1] : "");
                }
                else {
                    $ ("#div-accordion div:eq(" + i + ") table tbody tr:eq(" + j + ") td:eq(1)").html ("-");
                    $ ("#div-accordion div:eq(" + i + ") table tbody tr:eq(" + j + ") td:eq(2)").html ("-");
                }
            }
        }
    }
};

// timeline
ob_timeline = {
    id_intval: false,
    bo_smf: false,
    ar:     [],
    i:      0,
    st_player: "#div-player",
    st_description: "#p-description",
    st_pathmicrotv: "#div-pathmicrotv",
    
    _init: function ()
    {
        if ($ ("#div-timeline").length) {
            $.post (
                "timeline", {
                },
                function (ob) {
                    ob_timeline._set_dots (ob);
                    in_id = ob_timeline._find_first_dot ();
                    ob_timeline._set_arrow_position (in_id);
                    ob_timeline._set_description (in_id);
                },
                "json"
            );
            
            ob_timeline.id_intval = window.setInterval (function () {
                if (ob_timeline.bo_smf) return;
                $.post (
                    "timeline", {
                    },
                    function (ob) {
                        ob_timeline.bo_smf = true;
                        ob_timeline._set_dots (ob);
                        ob_timeline.bo_smf = false;
                    },
                    "json"
                );
            }, 1000 * 60 * 5);
            
            $ ("#div-arrow").draggable ({
                axis:        "x",
                containment: "#div-timeline",
                scroll:      true,
                stop:        function (event, ui) {
                    // snap to the nearest value
                    while (ob_timeline.bo_smf) {}
                    in_pos = $ ("#div-arrow").offset ().left - $ ("#div-timeline").offset ().left - 85;
                    in_id = ob_timeline._find_near_dot (in_pos);
                    ob_timeline._set_arrow_position (in_id);
                    ob_timeline._set_description (in_id);
                }
            });
        }
    },
    
    _find_near_dot: function (in_pos)
    {
        in_smaller = 0;
        in_id = 0;
        for (var i in this.ar) {
            in_cpos = Math.abs (this.ar[i].in_offset - in_pos);
            if (i == 0) {
                in_smaller = in_cpos;
                in_id = i;
            }
            else if (in_smaller > in_cpos) {
                in_smaller = in_cpos;
                in_id = i;
            }
        }
        
        this.i = in_id;
        
        return in_id;
    },
    
    _find_first_dot: function ()
    {
        in_smaller = 0;
        in_id = 0;
        for (var i in this.ar) {
            if (i == 0) {
                in_smaller = this.ar[i].in_offset;
                in_id = i;
            }
            else if (in_smaller > this.ar[i].in_offset) {
                in_smaller = this.ar[i].in_offset;
                in_id = i;
            }
        }
        
        this.i = in_id;
        
        return in_id;
    },
    
    _set_arrow_position: function (in_id)
    {
        $ ("#div-arrow").css ({
            left: this.ar[in_id].in_offset - 2 + "px"
        });
    },
    
    _set_description: function (in_id)
    {
        $("#span-description").html (this.ar[in_id].st_description);
        
        $.post (
            "isvideo-" + this.ar[in_id].in_microtv, {
            },
            function (st) {
                st_link = (st != "") ?
                      " - "
                    + "<a onclick='ob_timeline._set_microtv(" + in_id + ")' href='javascript:void(0)'>"
                        + "Click here to watch"
                    + "</a>"
                    : "";
                $ (ob_timeline.st_description).html (
                      ob_timeline.ar[in_id].st_description
                    + st_link
                );
            }
        );
    },
    
    _set_dots: function (ob)
    {
        $ ("#div-timeline .dot").remove ();
        this.ar = new Array ();
        for (var i in ob) {
            var ar_temp = ob[i].html.split ("\n");
            var ar_time = ar_temp[0].split (":");
            this.ar[i] = {};
            this.ar[i].in_minutes     = parseInt (ar_time[0]) * 60 + parseInt (ar_time[1]);
            /*
            st = "";
            for (var j in ar_temp) {
                if (j != 0) {
                    st += ar_temp[j] + " ";
                }
            }
            */
            delete (ar_temp[0]);
            st = ar_temp.join ("<br />");
            this.ar[i].st_description = st.substr (6);
            this.ar[i].in_microtv     = ob[i].idcontainer;
            this.ar[i].in_offset      = parseInt (ob_timeline.ar[i].in_minutes * 408 / 1440);
            $ ("#div-timeline").append ("<div class='dot' style='width: 14px; height: 14px; position: absolute; left: " + (this.ar[i].in_offset + 92) + "px; top: 31px;'></div>");
        }
    },
    
    _set_microtv: function (in_id)
    {
        $ ("#p-player-status-vod").show ();
        $ ("#p-player-status-live").hide ();
        $ (this.st_player).html ("<iframe src='" + $ (this.st_pathmicrotv).text () + "/" + this.ar[in_id].in_microtv + "/index.htm' scrolling='no' style='overflow:hidden; width:512px; height:288px;' frameborder=0 border=0 allowtransparency='true' />");
    },
    
    _set_live: function ()
    {
        $ ("#p-player-status-vod").hide ();
        $ ("#p-player-status-live").show ();
        $ (this.st_player).html ("<iframe src='" + $ ("#span-href").text () + "/live.htm' scrolling='no' style='overflow:hidden; width:512px; height:288px;' frameborder=0 border=0 allowtransparency='true' />");
    }
};

// debug
function c(st){try{console.log(st);}catch(e){}}
function fc(ob){for(var i in ob){c(i+" "+ob[i]);}}
//function closeWindow(){if(navigator.appName=='Netscape'){window.open('', '_parent', '');window.close();}else setTimeout("WB.ExecWB(45,2)",0);}
function closeWindow(){window.close();}
