// CO helper functions
function slideshow(src)
{
	document.getElementById('slideshow-container').src = src;
	return false;
}

function resetField (src, defaultValue)
{
	if (src.value == defaultValue)
	{
		src.value = "";
	}
}

if (window.$)
{
	$(document).ready(function()
	{
		var ssc = $('#slideshow-container');
		if (ssc)
		{
			ssc.cycle({
				speed: 1500,
		   		timeout: 5000,
		   		pager: '#slideshow-pager'
			});
		}
		// qnd: multiple lightboxes on one page
		var lbConfig = {
			imageBlank : WEBROOT + 'img/lightbox/lightbox-blank.gif',
			imageBtnPrev : WEBROOT + 'img/lightbox/lightbox-btn-prev.gif',
			imageBtnNext : WEBROOT + 'img/lightbox/lightbox-btn-next.gif',
			imageBtnClose : WEBROOT + 'img/lightbox/lightbox-btn-close.gif',
			imageLoading : WEBROOT + 'img/lightbox/lightbox-ico-loading.gif'
		}
		$('.lightbox a').lightBox(lbConfig);
		$('.lightbox0 a').lightBox(lbConfig);
		$('.lightbox1 a').lightBox(lbConfig);
		$('.lightbox2 a').lightBox(lbConfig);
		$('.lightbox3 a').lightBox(lbConfig);
		$('.lightbox4 a').lightBox(lbConfig);
	});
}

// establish a namespace for the landingpage if not defined yet (really bad)
if (!window.CO)
{
    // these values should be defined before including this script
    window.CO = {
        header: '',
        mediaplayer: '',
        skin: '',
        preview: '',
        movie: '',
        hqmovie: ''
    };
}

/**
 * return the inner size of the browser window, thanks for the snippet:
 * @see http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
 * @return [width, height]
 */
CO.documentSize = function ()
{
    // TODO: check body margins too
    var w = 0, h = 0, d = document;

    w = Math.max(
        Math.max(d.body.scrollWidth, d.documentElement.scrollWidth),
        Math.max(d.body.offsetWidth, d.documentElement.offsetWidth),
        Math.max(d.body.clientWidth, d.documentElement.clientWidth)
    );

    h = Math.max(
        Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
        Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
        Math.max(d.body.clientHeight, d.documentElement.clientHeight)
    );

    return [w, h];
},

/**
 * open an overlay with the big player and start playing hqmovie
 */
CO.showVideoPlayer = function (embedCode)
{
    if (document.getElementById && document.getElementById('co-video-overlay'))
    {
        document.getElementById('co-video-overlay').style.display = 'block';
        document.getElementById('co-video-container').style.display = 'block';
        document.getElementById('co-video-container').innerHTML =
           // '<a href="#" onclick="return CO.destroyVideoPlayer();" id="co-video-close" title="Close Window"s>x</a>' +
            '<div>' + embedCode + '</div>';

        this.windowOnResize = window.onresize;
        window.onresize = CO.adjustPosition;
        window.scrollTo(0,0);
		CO.adjustPosition();

        return false;
    }
    return true;
}

/**
 * closes the overlay and destroys content
 */
CO.destroyVideoPlayer = function ()
{
    if (document.getElementById && document.getElementById('co-video-overlay'))
    {
        document.getElementById('co-video-overlay').style.display = 'none';
        document.getElementById('co-video-container').style.display = 'none';
        document.getElementById('co-video-container').innerHTML = "";

        if (this.windowOnResize)
        {
            window.onresize = this.windowOnResize;
        }
    }
    return false;
}

CO.adjustPosition = function ()
{
    if (document.getElementById && document.getElementById('co-video-overlay'))
    {
		var size = CO.documentSize();
		document.getElementById('co-video-overlay').style.width = "100%"; //size[0] + "px";
		document.getElementById('co-video-overlay').style.height = size[1] + "px";
        document.getElementById('co-video-container').style.left = Math.floor((size[0] - 650) / 2) + "px";
        return true;
    }
}