



/* Plugins */
(function ($) { $.extend({ jGFeed: function (url, fnk, num, key) { if (url == null) { return false; } var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url; if (num != null) { gurl += "&num=" + num; } if (key != null) { gurl += "&key=" + key; } $.getJSON(gurl, function (data) { if (typeof fnk == "function") { fnk.call(this, data.responseData.feed); } else { return false; } }); } }); })(jQuery);




$(document).ready(function () {

    $.fn.alignCenter = function () {
        var thiswidth = $(this).width();
        var thisheight = $(this).height();
        var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        var marginLeft = Math.max(40, parseInt($(window).width() / 2 - thiswidth / 2)) + 'px';
        var marginTop = Math.max(40, scrollTop + parseInt($(window).height() / 2 - thisheight / 2)) + 'px';
        if ($.browser.msie)
            return $(this).css({ 'margin-left': marginLeft, 'margin-top': marginTop, 'left:': 0, 'top': 0 });
        else
            return $(this).css({ 'margin-left': marginLeft, 'margin-top': marginTop });
    };

    jfLoadYandexGalleries('media-kostroma-eparhia');
    jfLoadYandexVideos();
	jfLoadYandexAudios();
   
});
String.prototype.trim = function () { return this.replace(/^[\s\n]*/, "").replace(/[\s\n]*$/, ""); }


/*  Yandex.Video support  */

function jfLoadYandexVideos() {
    $('.VideoHolder').each(function () {
        var yauser = this.innerHTML.toString().match(/^(.+)\/[0-9]+\/[0-9]+$/)[1];
        var collection = this.innerHTML.toString().match(/^.+\/([0-9]+)\/[0-9]+$/)[1];
		var count = this.innerHTML.toString().match(/^.+\/[0-9]+\/([0-9]+)$/)[1];
        this.innerHTML = '';
        jfRenderVideos($(this), yauser, collection, count);
    });
}


function jfRenderVideos(target, yauser, collection, count) {
    target.addClass('videocollection_'+collection);
    $.jGFeed('http://video.yandex.ru/users/' + yauser.toString() + '/collection/' + collection.toString() + '/rss/',
          function (data) {
              var output = '<table width=100% cellspacing=0 cellpadding=3>';
              for (var i = 0; i < data.entries.length && i<count; i++) {
                  var img = data.entries[i].content.toString().match(/src=\"(.+)\"/)[1];
                  var storage = img.match(/\/get\/.+\/(.+)\/.+/)[1];
                  var linkopen = '<a href="#" title="' + data.entries[i].title.replace(/\"/g,'') + '" onclick="jfShowVideoPopup(\'' + yauser + '\',\'' + storage + '\',\''+ data.entries[i].title.replace(/\"/g,'') +'\',false); return false;">';
                  output += '<tr><td valign=top>';
                  output += linkopen + data.entries[i].content + '</a>';
                  output += '</td><td valign=top>';
                  output += linkopen + data.entries[i].title + '</a>';
                  output += '</td></tr>';
              }
              output += '</table>'
              $('.videocollection_' + collection).html(output);
          }, 10);
}






/*  Yandex.Video support - Audio files */

function jfLoadYandexAudios() {
    $('.AudioHolder').each(function () {
        var yauser = this.innerHTML.toString().match(/^(.+)\/[0-9]+\/[0-9]+$/)[1];
        var collection = this.innerHTML.toString().match(/^.+\/([0-9]+)\/[0-9]+$/)[1];
		var count = this.innerHTML.toString().match(/^.+\/[0-9]+\/([0-9]+)$/)[1];
        this.innerHTML = '';
        jfRenderAudios($(this), yauser, collection, count);
    });
}


function jfRenderAudios(target, yauser, collection, count) {
    target.addClass('videocollection_'+collection);
    $.jGFeed('http://video.yandex.ru/users/' + yauser.toString() + '/collection/' + collection.toString() + '/rss/',
          function (data) {
              var output = '<table width=100% cellspacing=0 cellpadding=3>';
              for (var i = 0; i < data.entries.length && i<count; i++) {
                  var img = data.entries[i].content.toString().match(/src=\"(.+)\"/)[1];
                  var storage = data.entries[i].mediaGroups[0].contents[0].player.url.toString().match(/\/lite\/.+\/(.+)\/$/)[1];
                  var linkopen = '<a href="#" title="' + data.entries[i].title.replace(/\"/g,'') + '" onclick="jfShowVideoPopup(\'' + yauser + '\',\'' + storage + '\',\''+ data.entries[i].title.replace(/\"/g,'') +'\', true); return false;">';
                  output += '<tr><td valign=top style="padding:6px 5px 0 10px">';
                  output += linkopen + '<img src="/images/bullets/radio/icon-radio_1.png"></a>';
                  output += '</td><td valign=top>';
                  output += linkopen + data.entries[i].title + '</a>';
                  output += '</td></tr>';
              }
              output += '</table>'
              $('.videocollection_' + collection).html(output);
          }, 10);
}


function jfShowVideoPopup(yauser,storage, title, isaudio) {

	if (isaudio)
	{
		_gaq.push(['_trackEvent', 'YandexAudio', 'Audio click', title]);
	}
	else
		_gaq.push(['_trackEvent', 'YandexVideo', 'Video click', title]);

    var divs = '<div id="jfshadow" style="background-color: #000000; left: 0; ';
    if ($.browser.msie) {
        divs += '-ms-filter:\'progid:DXImageTransform.Microsoft.Alpha(Opacity=70)\';filter:progid:DXImageTransform.Microsoft.Alpha(opacity = 70);';
    }
    if (!$.browser.opera) {
        divs += 'filter: alpha(opacity=70);moz-opacity: 0;-khtml-opacity: 0;';
    }
    else {
        divs += 'height:100%;';
    }
    divs += ' opacity: 0;position: absolute;top: 0;width: 100%;z-index: 10;"></div>' +
            '<div id="jfpopup" style="display:none;text-align:center;vertical-align:middle;' +
		'position: absolute;z-index:11;background-color:#ffffff;border:1px solid #888;width:460px;height:348px;left:0px;top:0px;"><div style="float:left; position:absolute; top:-20px; left:460px; z-index:12;"><img src="/jmedia/close-icon.png"></div><table cellspacing=0 cellpadding=0  width=100% height=100% align=center><tr><td id="jfpopupcontainer" valign=middle align=center></td></tr></table></div>';
    
    $('body').append(divs);
    if ($.browser.msie) {
        $('#jfshadow').height($(document).height());
        $('#jfshadow').css('display', 'block');
    }
    else if ($.browser.opera) {
        $('#jfshadow').css('height', document.body.clientHeight);
        $('#jfshadow').fadeTo('slow', 0.7);
    }
    else {
        $('#jfshadow').height($(document).height());
        $('#jfshadow').fadeTo('slow', 0.7);
    }
    $('#jfshadow').click(function () {
        $('#jfpopup').fadeTo('slow', 0).remove();
        $('#jfshadow').fadeTo('slow', 0).remove();
    });
    $('#jfpopup').css('display', 'block');
    $('#jfpopup').css('cursor', 'pointer');

    if ($.browser.opera) {
        $('#jfpopup').alignCenter($('#jfpopup'));
    }
    else {
        $('#jfpopup').alignCenter();
    }

    $('#jfpopup').click(function () {
        $('#jfpopup').fadeTo('slow', 0).remove();
        $('#jfshadow').fadeTo('slow', 0).remove();
    });

    var player = '<object type="application/x-shockwave-flash" id="flash" data="http://static.video.yandex.ru/full/' + yauser + '/' + storage + '/" width="450" height="338"><param name="quality" value="high"><param name="bgcolor" value="#ffffff"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="flashvars" value="login=' + yauser + '&amp;storage_directory=' + storage + '&amp;autostart=no&amp;is-hq=false&amp;reload=yes&amp;has-hq=true&amp;is-mail=false"></object>';
    $('#jfpopupcontainer').append('<div width=460 height=348>'+player+'</div>');
}






/*  Yandex.Fotki support  */

function jfShowPhotoPopup(url,title) {

    _gaq.push(['_trackEvent', 'YandexFotki', 'Photo click', url]);

    var divs = '<div id="jfshadow" style="background-color: #000000; left: 0; ';
    if ($.browser.msie) {
        divs += '-ms-filter:\'progid:DXImageTransform.Microsoft.Alpha(Opacity=70)\';filter:progid:DXImageTransform.Microsoft.Alpha(opacity = 70);';
    }
    if (!$.browser.opera) {
        divs += 'filter: alpha(opacity=70);moz-opacity: 0;-khtml-opacity: 0;';
    }
    else {
        divs += 'height:100%;';
    }
    divs += ' opacity: 0;position: absolute;top: 0;width: 100%;z-index: 10;"></div>' +
            '<div id="jfpopup" style="display:none;text-align:center;vertical-align:middle;'+
		'position: absolute;z-index:11;background-color:#ffffff;border:1px solid #888;width:100px;height:100px;left:0px;top:0px;"><table cellspacing=0 cellpadding=0  width=100% height=100% align=center><tr><td id="jfpopupcontainer" valign=middle align=center><img src="/jmedia/ajax-loader.gif"></td></tr></table></div>';
    $('body').append(divs);
    if ($.browser.msie) {
        $('#jfshadow').height( $(document).height() );
        $('#jfshadow').css('display', 'block');
    }
    else if ($.browser.opera) {
	    $('#jfshadow').css('height', document.body.clientHeight );
        $('#jfshadow').fadeTo('slow', 0.7);
    }
    else {
        $('#jfshadow').height( $(document).height() );
        $('#jfshadow').fadeTo('slow', 0.7);
    }
    $('#jfshadow').click(function () {
        $('#jfpopup').fadeTo('slow', 0).remove();
        $('#jfshadow').fadeTo('slow', 0).remove();
    });
    $('#jfpopup').css('display', 'block');
    $('#jfpopup').css('cursor', 'pointer');
	
    if ($.browser.opera) {
    	$('#jfpopup').alignCenter($('#jfpopup'));
    }
	else{
	    $('#jfpopup').alignCenter();
    }

    $('#jfpopup').click(function () {
        $('#jfpopup').fadeTo('slow', 0).remove();
        $('#jfshadow').fadeTo('slow', 0).remove();
    });

    $('#jfpopupcontainer').append('<img id="jfpopupimg" style="z-index:12;" width=0 height=0 src="' + url + '">');
    $('#jfpopupimg', '#jfpopup').load(function () {
        $('#jfpopupcontainer').html($(this).removeAttr('width').removeAttr('height'));
        $('#jfpopup').width($('#jfpopupimg').width() + 10);
        $('#jfpopup').height($('#jfpopupimg').height() + 10);
	$('#jfpopup').alignCenter();
    });
}




function jfLoadYandexGalleries(yauser) {

    $('.GalleryHolder').each(function (item) {
        $.getJSON("http://api-fotki.yandex.ru/api/users/" + yauser + "/album/" + this.attributes.galleryid.value.toString() + "/photos/?format=json&callback=?", {},
          function (data) {
              var r = new RegExp('\/album\/(.+)\/photos\/$');
              var id = (r.exec(data.links.self))[1];
              var target = $('.GalleryHolder[galleryid="' + id + '"]');

              if (false && window.location.href.indexOf('view=article') == -1) {
              	target.html('<table><tr><td style="width:30px;">&nbsp;</td><td><div style="position:absolute;overflow:hidden;width:445px; height:185px;"><div class="photolist" galleryid="'+id+'" style="position:absolute;height:90px;width:'+(parseInt(data.entries.length)*50)+'px;"></div></div></td><td style="width:30px;">&nbsp;</td></tr></table>');
		target = $('.photolist[galleryid="' + id + '"]');
	      }
		else {target.html('');}

              var max = 999;
              if (window.location.href.indexOf('/index.php/') == -1) max = 6;
              for (var i = 0; i < data.entries.length && i < max; i++) {
                  var photo = data.entries[ data.entries.length - i - 1 ];
                  var small = photo.img.XXS;
                  var big = photo.img.L;
                  if (photo.img.XL != undefined) big = photo.img.XL;

                  var m = 5;
                  target.append('<div style="float:left;padding:0px; border:1px solid #eee; margin:1px;">' +
                    '<a href="#" onclick="jfShowPhotoPopup(\'' + big.href + '\',\''+data.title+' #'+i.toString()+'\'); return false;"><img style="cursor:pointer; margin-left:' + m + 'px;margin-top:' + m + 'px;margin-right:' + m + 'px;margin-bottom:' + m + 'px;" src="' + small.href + '"></a>' +
                    '</div>');
              }
          });
    });

}









