﻿jQuery.noConflict();
jQuery(document).ready(function() {
    initialize();
    minisVideo(StronaOd, StronaIle, 0);

    jQuery("#filmsBox .fog").mouseover(function() {
        jQuery(this).addClass("active");
    });

    jQuery("#filmsBox .fog").mouseout(function() {
        jQuery(this).removeClass("active");
    });
});

function initialize() {
    var _obj;
    var ivideo = 0;
    if (Video != 'no') {
        for (i in VideoFlv) {
            var patt = new RegExp(Video);
            if (patt.test(VideoFlv[i])) {
                ivideo = i;
            }
        }
    }
    var W = VideoWxH[ivideo].slice(0, VideoWxH[ivideo].indexOf("x"));
    var H = VideoWxH[ivideo].slice(VideoWxH[ivideo].lastIndexOf("x") + 1);
    _obj = new SWFObject('/2009/_swf/jwplayer.swf', 'jwplayer', W, H, 9);
    _obj.addParam('flashvars', 'file=' + VideoFlv[ivideo] + '&image=' + VideoJpg[ivideo] + '&backcolor=' + backcolor + '&frontcolor=' + frontcolor + '&lightcolor=' + lightcolor + '&screencolor=' + screencolor + '');
    _obj.addParam('allowfullscreen', 'true');
    _obj.addParam('allowscriptaccess', 'always');
    _obj.write("playerVideo");
    jQuery("div#titleVideo").text(VideoSig[ivideo]);
    jQuery("div#leadVideo").html(VideoLid[ivideo]);
    getWriteArticle(VideoIds[ivideo]);
}

function minisVideo(StronaOd, StronaIle, Strona) {
    var i = StronaOd - 1;
    jQuery("div#filmsBox").html("")
    var iDo = VideoFlv.length;
    if ((StronaOd - 1 + StronaIle) < iDo)
        iDo = StronaOd - 1 + StronaIle;
    //for (i; i < iDo; i++)
     //   writeMiniVideo(VideoFlv[i], VideoJpg[i].replace("http://www.polskieradio.pl", ""), VideoSig[i])
     for (i=iDo-1;i>=0;i--)
	 {
		 writeMiniVideo(VideoFlv[i], VideoJpg[i].replace("http://www.polskieradio.pl", ""), VideoSig[i]);
	 }

    writePaging(StronaIle, Strona)
}

function writeMiniVideo(flv, jpg, title) {
    jQuery("<a class='video_mini' href='#top' onclick=\"Video='" + flv + "';initialize();\"><img src='http://www.polskieradio.pl/_controls/resize.aspx?img=" + jpg + "&w=120' alt='" + title + "' title='" + title + "'/><div class=\"fog\"></div><span class='title'>" + title + "</span></a>").appendTo("div#filmsBox");
}

function writePaging(StronaIle, Strona) {
    jQuery("div#pagingfilms").html("");
    if (VideoFlv.length > StronaIle) {
        writePage("prev", (((Strona - 1) * StronaIle) + 1), StronaIle, (Strona - 1), "&#171;");
        var ile = 10;

        if (Math.ceil(VideoFlv.length / StronaIle) <= ile) {
            writePages(0, Math.ceil(VideoFlv.length / StronaIle), StronaIle, Strona);
        }
        else {
            var iod = Strona;
            var ido = Strona + ile;
            if (ido > Math.ceil(VideoFlv.length / StronaIle))
                ido = Math.ceil(VideoFlv.length / StronaIle);
            if (Strona >= ido - ile)
                iod = ido - ile;

            writePages(iod, ido, StronaIle, Strona);
        }
        writePage("next", (((Strona + 1) * StronaIle) + 1), StronaIle, (Strona + 1), "&#187;");
        if (Strona == 0) {
            jQuery("a#prev").css("display", "none");
        }
        if (Strona == Math.ceil(VideoFlv.length / StronaIle) - 1) {
            jQuery("a#next").css("display", "none");
        }
    }
}

function writePages(iod, ido, StronaIle, Strona) {
    for (iod; iod < ido; iod++) {
        if (Strona == iod)
            writePage("activ", ((iod * StronaIle) + 1), StronaIle, iod, (iod + 1));
        else
            writePage("s" + iod, ((iod * StronaIle) + 1), StronaIle, iod, (iod + 1));
    }
}

function writePage(aId, StronaOd, StronaIle, Strona, toWrite) {
    if (aId == "activ")
        jQuery("<a id='" + aId + "' onclick='minisVideo(" + StronaOd + ", " + StronaIle + ", " + Strona + ")'><span>" + toWrite + "</span></a>").appendTo("div#pagingfilms");
    else
        jQuery("<a id='" + aId + "' onclick='minisVideo(" + StronaOd + ", " + StronaIle + ", " + Strona + ")'>" + toWrite + "</a>").appendTo("div#pagingfilms");
}

function getWriteArticle(idtb_article) {
    var folder = idtb_article.substring(0, idtb_article.length - 3);
    //alert(idtb_article + '\n' + folder);
    var file = "/2009/getxml.aspx?link=http://www.polskieradio.pl/_cms/articles/" + folder + "/" + idtb_article + ".xml";
    jQuery.ajax({
        type: "GET",
        url: file,
        dataType: "xml",
        success: function(xml) {
            var $txt = jQuery(xml).find('txt').text();
            if ($txt.length != 0) {
                jQuery("div#leadVideo").html();
                jQuery("div#leadVideo").html($txt);
            }
        }
    });
}

