
function positionPlayer(playerDiv, element) {

    try {
        $('#' + playerDiv).css("top", $("#" + element)[0].offsetTop);
        $('#' + playerDiv).css("left", $("#" + element)[0].offsetLeft);
        $('#' + playerDiv).css("width", $("#" + element).css("width"));
        $('#' + playerDiv).css("height", $("#" + element).css("height"));
    }
    catch (e) {
    }

}

function playMovieShort(filename, playerDiv, width, height, url) {

    $('#' + playerDiv).css("top", $("#" + element)[0].offsetTop);
    $('#' + playerDiv).css("left", $("#" + element)[0].offsetLeft);
    $('#' + playerDiv).css("width", $("#" + element).css("width")-100);
    $('#' + playerDiv).css("height", $("#" + element).css("height"));
    playMovie(filename, playerDiv, width, height, url);
}

function playMovie(filename, playerDiv, width, height, url) {
    if ($('#' + playerDiv).size()) {
        var webm = filename + '.webm';
        var mp4 = filename + '.mp4';

        switch (lang) {
            case 'eng':
                {
                    webm = webm.replace('portugues', 'ingles');
                    mp4 = mp4.replace('portugues', 'ingles');
                    break;
                }
        }

        $('#' + playerDiv).html('');

        //Detects if exists Silverlight Installed
        if (Silverlight.isInstalled("2.0") || Silverlight.isInstalled("3.0") || Silverlight.isInstalled("4.0")) {
            try {
                //Existe um player silverlight
                var html = ' <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="' + width + '" height="' + height + '">';
                html += '<param name="source" value="/ClientBin/PlayerLight.xap" />';
                    html += '<param name="onError" value="onSilverlightError" />';
                    html += '<param name="background" value="white" />';
                    html += '<param name="minRuntimeVersion" value="4.0.50401.0" />';
                    html += '<param name="autoUpgrade" value="true" />';
                    html += '<param name="enableGPUAcceleration" value="true" />';
                    html += '<param name="maxFrameRate" value="30" />';
                    html += '<param name="uiculture" value="pt-PT" />';
                    html += '<param name="initParams" value="Source=' + url + mp4 + ',title=bySense,Description=, AutoPlay=true, Repeat=false" />';
                    html += '<h2>Silverlight</h2><p>In order to correctly see this page, the Silverlight plug-in must be installed.     <br />     De modo a poder ver o conteúdo desta página, é necessário que tenha o plug-in de Silverlight instalado.     <br />     <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration: none">     <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" />     </a>     </p>     <p><a href="http://www.bysense.com">bySense</a></p>';
                    html += '</object>';
                    $('#' + playerDiv).append("<div>" + html + "</div>");
                    $("#mood").css("display", "none");
            }
            catch (err) {
                //alert(err.Description);
            }
        } else if (supports_video()) {
            // OPTION B :: Browser Support video
            var html = '<video width="' + width + '" height="' + height + '" controls autoplay>';
                html += '<source src="' + url + mp4 + '"  type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'>';
                html += '<source src="' + url + webm + '" type=\'video/webm; codecs="vp8, vorbis"\'>';
                html += '</video>';
                $('#' + playerDiv).append("<div>" + html + "</div>");
                $("#mood").css("display", "none");
        } else {
            // Last resort: redirect the user to the file directly
            if ($.client.os == "Windows") {
                window.location.replace(url + mp4);
            } else {
                window.location.replace(url + webm);
            }
        }
    }
}

function supports_video() {
    return !!document.createElement('video').canPlayType;
}



function readCookie(strCookie) {
    var strNomeIgual = strCookie + "=";
    var arrCookies = document.cookie.split(';');

    for (var i = 0; i < arrCookies.length; i++) {
        var strValorCookie = arrCookies[i];
        while (strValorCookie.charAt(0) == ' ') {
            strValorCookie = strValorCookie.substring(1, strValorCookie.length);
        }
        if (strValorCookie.indexOf(strNomeIgual) == 0) {
            return strValorCookie.substring(strNomeIgual.length, strValorCookie.length);
        }
    }
    return null;
}

function loadPage(first, lang, refreshcache) {
    if (refreshcache != "")
        $('#content-right').load('/lastNews.aspx?first=' + first + '&lang=' + lang + '&refreshcache=' + refreshcache);
    else
        $('#content-right').load('/lastNews.aspx?first=' + first + '&lang=' + lang);
}
