$(function(){
    $("#poster").jCarouselLite({
        btnNext: '.next',
        btnPrev: '.prev',
        auto: 5000,
        speed: 500,
        visible: 1
    });
    $('a[rel=gallery]').fancybox({
        'overlayOpacity': 0.7,
        'overlayColor'  : '#000',
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic',
        'easingIn'      : 'easeOutBack',
        'easingOut'     : 'easeInBack',
        'titlePosition' : 'over',
        'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over"><em>Bilde ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</em>' + title + '</span>';
        }
    });
    $('a[rel*=external]').click(function(){
        window.open(this.href);
        return false;
    });
    var vidWidth  = 656;
    var vidHeight = 492;
    var obj       = '<div class="youtube-vid"><object width="' + vidWidth + '" height="' + vidHeight + '">' +
        '<param name="movie" value="http://www.youtube.com/v/[vid]&amp;hl=en&amp;fs=1">' +
        '</param><param name="allowFullScreen" value="true"></param><param ' +
        'name="allowscriptaccess" value="always"></param>' +
        '<param name="wmode" value="transparent"></param><em' +
        'bed src="http://www.youtube.com/v/[vid]&amp;hl=en&amp;fs=1" ' +
        'type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" ' +
        'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' +
        vidHeight + '"></embed></object></div>';
    // youtube video replacement
    $('.article').find('p:contains("youtube.com/watch")').each(function(){
        var that = $(this);
        var vid = that.html().match(/(?:v=)([\w\-]+)/g);
        if (vid.length) {
            $.each(vid, function(i){
                that.replaceWith( obj.replace(/\[vid\]/g, this.replace('v=','')) );
            });
        }
    });
    $('#add-pics').click(function(){
        var el = $('#picpane');
        if(el.is(':visible')){
            el.fadeOut(300);
        } else {
            el.fadeIn(300);
        }
        return false;
    });
    $('.pic-del').click(function(){
        var el = $(this);
        $.post(el.attr('href'), function(res){
            if (res='success'){
                el.fadeOut(300);
            }
        });
        return false;
    });
});

// Icons & Social apps

function DraugiemSay( title, url, titlePrefix ){
    window.open(
        'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) +
        '&link=' + encodeURIComponent( url ) +
        (titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ),
        '',
        'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400'
    );
    return false;
}

