jQuery.noConflict();

var lerp = function (a, b, percent) {
    return a + percent * (b - a);
};

var cancelBubble = function (e) {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
};

jQuery(document).ready(function () {
    /* This code is executed after the DOM has been completely loaded */

    /* jcarrousel */

    /* The number of event sections / years with events */
    var tot = jQuery('.event').length + 1;

    /* uncomment this part to put back per-project highlight popups
    jQuery('.eventList').click(function (e) {
    cancelBubble(e);
    });

    jQuery('.devinfo').click(function (e) {
    cancelBubble(e);
    });

    jQuery('.eventitem_footer').click(function (e) {
    cancelBubble(e);
    });

    jQuery('.eventList li').click(function (e) {
    showWindow('<div>' + jQuery(this).find('div.content').html() + '</div>');
    cancelBubble(e);

    });*/

    jQuery('.event').click(function (e) {
        showWindow('<div>' + jQuery(this).find('div.content').html() + '</div>');
        cancelBubble(e);
    });

    var numberOfEntriesWeCanShow = Math.floor((screen.width - 100) / 280);

    /* the width of the entire display. 824 houses 3 */
    var displayWidth = 280 * numberOfEntriesWeCanShow;

    /* resize what needs resizing */
    var centerPanelWidth = 280 * numberOfEntriesWeCanShow + 24;
    jQuery('#centerPanel').width(centerPanelWidth);   /* 864 for 3 */
    jQuery('#centerPanel').css({ 'margin-left': -1 * centerPanelWidth / 2 });

    var mainframeWidth = 280 * numberOfEntriesWeCanShow + 8;
    jQuery('#mainframe_top').width(mainframeWidth) /* 848 for 3 */
    jQuery('#mainframe_bottom').width(mainframeWidth) /* 848 for 3 */

    jQuery('#mainframe_right').css({ 'left': mainframeWidth + 12 });
    jQuery('#mainframe_topright').css({ 'left': mainframeWidth + 8 });
    jQuery('#mainframe_bottomright').css({ 'left': mainframeWidth + 8 });

    jQuery('#scrollframe_middle').width(mainframeWidth + 6) /* 854 for 3 */
    jQuery('#scrollframe_right').css({ 'left': mainframeWidth + 11 }); /* 859 for 3 */

    /* Each event section is 280 px wide */
    var timelineWidth = (280 * tot) - 10;    // last one is 10 pixels less
    var numYearsInScrollbar = 9;


    /* set the width of the scrollpoints */

    jQuery('.scrollPoints').width(displayWidth / numYearsInScrollbar);

    jQuery('#timelineScroll').width(timelineWidth);

    /* If the timeline is wider than the screen show the slider: */
    if (timelineWidth > displayWidth) {
        jQuery('#scroll,#slider').show();
        jQuery('#centered,#slider').width(displayWidth); /*(120 * tot);*/

        /* Making the scrollbar draggable: */
        jQuery('#highlight').width((120 / 280) * displayWidth).draggable({

            containment: 'parent',
            axis: 'x',
            drag: function (e, ui) {

                if (!this.elem) {
                    /* This section is executed only the first time the function is run for performance */

                    this.elem = jQuery('#timelineScroll');

                    /* The difference between the slider's width and its container: */
                    this.maxSlide = ui.helper.parent().width() - ui.helper.width();

                    /* The difference between the timeline's width and its container */
                    this.cWidth = this.elem.width() - this.elem.parent().width();
                    //    this.highlight = jQuery('#highlight');
                }

                /* Translating each movement of the slider to the timeline: */
                this.elem.css({ marginLeft: '-' + ((ui.position.left / this.maxSlide) * this.cWidth) + 'px' });

                /* Moving the highlight: */
                //this.highlight.css('left', ui.position.left)
            }
        });

        jQuery('#highlight').width((120 / 280) * displayWidth - 3);
    }

});

function showWindow(data)
{
	/* Each event contains a set of hidden divs that hold
	   additional information about the event: */

    jQuery('<div id="siteOverlay">').css({

        width: jQuery(document).width(),
        height: jQuery(document).height(),
        opacity: 0.8

    }).appendTo('body').click(function () {

        jQuery(this).remove();
        jQuery('#windowBox').remove();

    });
       
	//var title = jQuery('.title',data).text();
	var date = jQuery('.date',data).text();
	var body = jQuery('.body', data).html();
	var images = jQuery('.images', data);
	var imagesHtml = null;

	if (images != null) {
	    imagesHtml = images.html();
	}

	/*if (imagesHtml != null) {
	    jQuery('body').append('<div id="windowBox"><div id="carrousel"><button class="carrousel_left"></button><button class="carrousel_right"></button><div class="carrousel_frame"><div id="carrousel_images">' + imagesHtml.replace(/lightboxrel/gi, "popuprel") + '</div></div></div><div class="windowbox_body">' + body + '<div id="windowBoxBottomNav"><a id="bottomNavClose" href="#"><img src="img/close_label.png"></img></a></div></div>');
	} else {
	    jQuery('body').append('<div id="windowBox"><div class="windowbox_body">' + body + '<div id="windowBoxBottomNav"><a id="bottomNavClose" href="#"><img src="img/close_label.png"></img></a></div></div>');
	}*/

	if (imagesHtml != null) {
	    jQuery('body').append('<div id="windowBox"><div id="carrousel"><button class="carrousel_left"></button><button class="carrousel_right"></button><div class="carrousel_frame"><div id="carrousel_images">' + imagesHtml.replace(/lightboxrel/gi, "popuprel") + '</div></div></div><div class="windowbox_body">' + body + '</div>');
    } else {
	    jQuery('body').append('<div id="windowBox"><div class="windowbox_body">' + body + '</div>');
	}

	jQuery('#windowBoxBottomNav').click(function () {
	    jQuery('#siteOverlay').remove();
	    jQuery('#windowBox').remove();

	});

    jQuery('#windowBox').width(920);
    jQuery('#windowBox').css({
		/*width:920,*/
		/*height:600,*/
		left: (jQuery(window).width() - 920)/2,
		top: (jQuery(window).height() - jQuery('#windowBox').height())/2
	});

	if (imagesHtml != null) {
	    jQuery("#carrousel_images").jCarouselLite({
	        btnNext: ".carrousel_right",
	        btnPrev: ".carrousel_left",
            circular: false
	    });
	}	
}
