// -*- mode: javascript; -*-





/*
  Main javascript, depends on jQuery,
  This script is shared among the various NV NOM sites.
  @author: Andre van Toly
  @version: '$Id: main.js.jsp 736 2012-02-01 15:55:49Z nom $'
*/
function initLightBox() {
    if ($('a.lightbox').length) {
        var settings = jQuery.extend({
            imageLoading: '/style/js/lightbox-ico-loading.gif',
            imageBtnPrev: '/style/js/lightbox-btn-prev.gif',
            imageBtnNext: '/style/js/lightbox-btn-next.gif',
            imageBtnClose: '/style/js/lightbox-btn-close.gif',
            imageBlank: '/style/js/lightbox-blank.gif'
        }, settings);
        $('a.lightbox').lightBox(settings);
    }
}
function formatTweets() {
    $('li.tweet em a, div.tweet span a').each(function(i) {
        var time = $(this).text();
        $(this).text( relative(time) );
    });
    $('li.tweet, div.tweet').each(function(i) {
        var txt = $(this).html();
        txt = txt.replace(/\B@([_a-z0-9]+)/ig, function(r) { return r.charAt(0)+'<a href="http://twitter.com/'+r.substring(1)+'">'+r.substring(1)+'</a>';});
        $(this).html(txt);
    });
}
function relative(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);
  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
function initSearch() {
    var default_value = "Zoek";
    $('input#search_q, input.searchbox').focus(function(ev) {
        if ($(this).val() == default_value) $(this).val('');
    });
    $('input#search_q, input.searchbox').blur(function(ev) {
        if ($(this).val() == "") $(this).val(default_value);
    });
    /*$('label.search').click(function(ev){
        $(this).parents('form').submit();
    }); */
}
/* function to include a tabbed based interface for videos with jquery.ui and oiplayer */
function initVideoTabs(conf) {
    if ($('div#videos').length) {
        $("div#videos").tabs({
            ajaxOptions: {
                error: function( xhr, status, index, anchor ) {
                    $(anchor.hash).html("Sorry an error happened while trying to load this video.");
                }
            },
            select: function(event, ui) {
                if ($('#videos').find('object').length) {
                    var objId = $('#videos').find('object').attr('id');
                    /* reload when using flowplayer: gives problems in msie (unload flash or something) */
                    if (objId != undefined && objId.indexOf('fp') > -1 && $.browser.msie) {
                        var i = ui.index + 1;
                        var loc = document.location.href;
                        if (loc.indexOf('#') > -1) {
                            loc = loc.substring(0, loc.indexOf('#'));
                        }
                        loc = loc + '#video_' + i;
                        //alert('msie reload');
                        document.location = loc;
                    }
                }
            },
            load: function(event, ui) {
                $('div#videos').oiplayer(conf);
            }
        });
        $(".tabs-bottom .ui-tabs-nav, .tabs-bottom .ui-tabs-nav > *")
            .removeClass( "ui-corner-all ui-corner-top")
            .addClass("ui-corner-bottom");
    }
}
/* function opening a video on top of a page */
function initVideoZoom(conf) {
    if ($('a.zoom-video').length) {
        // link around the video preview image is to video
        $('a.zoom-video').click(function(ev){
            ev.preventDefault();
            var videolink = $(this).attr('href');
            $('body').append('<div id="zoom-video"><div></div></div>');
            $('div#zoom-video > div').load(videolink + ' #videocontainer', function(){
                // the close button has to live in inner div
                $(this).prepend('<a id="zoom-video-close" href="#close"></a>');
                $('div#zoom-video').fadeIn('slow');
                $(this).oiplayer(conf); // should have auto start function?
                // close zoomed video when clicked outside video
                $(this).click(function(ev){ ev.stopPropagation(); });
                $('div#zoom-video, a#zoom-video-close').click(function(ev){
                    ev.preventDefault();
                    $('div#zoom-video').fadeOut("slow", function() {
                        var objId = $('div#zoom-video').find('object').attr('id');
                        /* reload when using flowplayer: gives problems in msie (unload flash or something) */
                        if (objId != undefined && objId.indexOf('fp') > -1 && $.browser.msie) {
                            window.location.reload();
                        } else {
                            $('div#zoom-video').remove();
                        }
                    });
                });
            });
        });
    }
}
$(document).ready(function() {
    initLightBox();
    formatTweets();
    initSearch();
    var oiplayerConf = {
        server : "http://www.nom.nl/", 
        jar : "/oiplayer/plugins/cortado-ovt-stripped-0.6.0.jar",
        flash : "/oiplayer/plugins/flowplayer-3.2.7.swf",
        controls: 'top white'
    };
    initVideoTabs(oiplayerConf);
    initVideoZoom(oiplayerConf);
    if ($('td.video').length) { $('td.video').oiplayer(oiplayerConf); }
    //alert('ready');
});


