﻿var video = "";
var v = "";
jQuery(document).ready(function() {
    //potrzebne jquery.query.js
    var od = jQuery.query.get('od');
    video = jQuery.query.get('vid');
    v = jQuery.query.get('v');
    //first load data
    if (od == null) { GetPage("youtubevideosdata.aspx"); }
  
});

function GetPage(page) {

    jQuery("#videoUl").text("");
    jQuery("#videoUl").val("");
    jQuery("#videoUl").addClass("load");
    jQuery.ajax({
                 type: "GET",
                 url: page,
                 cache: false,
                 dataType: "html",
                 success: function(dane) {
                 jQuery("#videoUl").append(dane);
                 },
                 complete: function(){
                 jQuery("#videoUl").removeClass("load");
                 }
         });
}
function PlayVideo(videoid,articleurl) 
{
    jQuery(document).ready(function() {


        var title = "";
        var desc = "";
        var object = "<object width=\"479\" height=\"384\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + videoid + "&hl=pl&fs=1&autoplay=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/" + videoid + "&hl=pl&fs=1&autoplay=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"479\" height=\"384\"></embed></object>";
        jQuery("#youtubeplayer").html(object);


        title = jQuery("#videoUl > .videoUlBox:first > .text > .titleDate > a").text();
        desc = jQuery("#videoUl > .videoUlBox:first > .text > .desc > .descFilm").text();

        jQuery(".leftVideo > #descVideo > #videoTitle").text(title);
        jQuery(".leftVideo > #descVideo > #videoLead").text(desc);

        jQuery("#videoUl > .videoUlBox > a.imgFilm").click(function() {
            var title = jQuery(this).parent().find(".text > .titleDate > a").text();
            var desc = jQuery(this).parent().find(".text > .desc > .descFilm").text();
            desc = desc.substr(0, 350);
            jQuery(".leftVideo > #descVideo > #videoTitle").text(title);
            jQuery(".leftVideo > #descVideo > #videoLead").text(desc);
        });

        jQuery("#videoUl > .videoUlBox > .text > .titleDate > a").click(function() {
            var title = jQuery(this).text();
            var desc = jQuery(this).parent().siblings().find(".descFilm").text();
            desc = desc.substr(0, 350);
            jQuery(".leftVideo > #descVideo > #videoTitle").text(title);
            jQuery(".leftVideo > #descVideo > #videoLead").text(desc);
        });

        SetBookmarks(articleurl, title, desc);
        //zmien link
        jQuery("#moreurl").attr("href", articleurl);
    });
}

function SetBookmarks(articleurl,title,desc)
{
		jQuery("div#videoAddTo > a.wykop").attr("href", "http://www.wykop.pl/dodaj?url=" + articleurl + "&title=" + escape(title) + "&desc=" + escape(desc));
		jQuery("div#videoAddTo > a.delicious").attr("onclick", "window.open('http://delicious.com/post?v=5&noui&jump=close&url=" + encodeURIComponent(articleurl) + "&title=" + encodeURIComponent(title) + "', 'delicious','toolbar=no,width=550,height=550'); return false;");
		jQuery("div#videoAddTo > a.google").attr("href", "http://www.google.pl/bookmarks/mark?op=add&hl=pl&title=" + escape(title) + "&bkmk=" + articleurl + "&annotation=" + escape(desc));
}

function GetVideoInfo(licznik, od, page, videoid, aid, data, url) {
    jQuery("#" + videoid + aid).addClass("load");
    jQuery.ajax({
        type: "POST",
        url: page,
        data: "nr=" + licznik + "&vid=" + videoid + "&aid=" + aid + "&url=" + url + "&d=" + data,
        cache: false,
        dataType: "html",
        success: function(dane) {
            jQuery("#" + videoid + aid).append(dane);
        },
        complete: function() {
            jQuery("#" + videoid + aid).removeClass("load");
            //zaladuj 1 film

            if (v != null && od == 'null' && licznik == 0 && video == null) {
                PlayOuterVideo();
            }
            else {
                if (od == 'null' && licznik == 0 && video == null) {
                    PlayVideo(videoid, url+'artykul'+aid+'.html');
                }
            }

			// usuwa border z ostatniego elementu w "paging"
			jQuery("#paging > a:last").css({border:'none'});
        }
    });
}
function PlayOuterVideo() {

    var title = jQuery("#title").val();
    var desc = jQuery("#lead").val();
    var articleurl = jQuery("#url").val();
    var videoid = jQuery("#vid").val();
    
    var object = "<object width=\"479\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + videoid + "&hl=pl&fs=1&autoplay=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://www.youtube.com/v/" + videoid + "&hl=pl&fs=1&autoplay=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"479\" height=\"344\"></embed></object>";
    jQuery("#youtubeplayer").html(object);
    jQuery(".leftVideo > #descVideo > #videoTitle").text(title);
    jQuery(".leftVideo > #descVideo > #videoLead").text(desc);
    SetBookmarks(articleurl, title, desc);
}