//window.onresize = position
//window.onload = onloadHook


function setHeight(id, val)
{
	var e = document.getElementById(id)
	e.style.height = val + 'px'
}

function setArchiveHeight()
{
	var innerHeight = window.innerHeight
	if (innerHeight == undefined) {
		// the case in IE6
		innerHeight = document.documentElement.clientHeight
	}
	var h = innerHeight - 500
	if (h < 250) {
		h = 250
	}
	setHeight('book_list', h)
	setHeight('chapter_list', h)
	setHeight('archive', h)
    setHeight('segments', h)
}


function bookSelected(book)
{
    $('book_' + selectedBook).className = ''
    $('book_' + book).className = 'selected'
    selectedBook = book

    new Ajax.Updater('chapters_segments',
            '?view=fragments/chapters_segments' +
            '&book=' + book,                              
            {evalScripts: true, onComplete: position})

    return false
}

function chapterSelected(chapter)
{
    $('chapter_' + selectedChapter).className = ''
    $('chapter_' + chapter).className = 'selected'
    selectedChapter = chapter

    new Ajax.Updater('segments',
            '?view=fragments/segments' +
            '&chapter=' + chapter)

    return false
}


function fixFirefoxMacListScrolls()
{
	if (window.navigator.userAgent.indexOf('Firefox') == -1) {
		// this only affects Firefox
		return;
	}
	if (window.navigator.platform.indexOf('Mac') == -1) {
		// this only affects Mac users
		return;
	}
	e = document.getElementById('book_list')
	var overflow = 'hidden'
	if (e.style.overflow == 'hidden') {
		overflow = 'auto'
	}
	e.style.overflow = overflow
	e = document.getElementById('chapter_list')
	e.style.overflow = overflow
}

function toggleBibleMenu()
{
	fixFirefoxMacListScrolls()		
		
	
	bm = document.getElementById('bible_menu')
	if (bm.style.display != 'block') {
		bm.style.display = 'block';
	} else {
		bm.style.display = 'none';
	}
}

function findPosTop(element)
{
	var cur = element.offsetTop
	while (element = element.offsetParent) {
		cur += element.offsetTop
	}
	return cur;
}

function findPosLeft(element)
{
	var cur = element.offsetLeft
	while (element = element.offsetParent) {
		cur += element.offsetLeft
	}
	return cur;
}

function positionListenForFree()
{
	var lff = document.getElementById('listen_for_free')
	var a = document.getElementById('archive')
	lff.style.top = (findPosTop(a) - 50) + 'px'
	lff.style.left = (findPosLeft(a) - 25) + 'px'
	lff.style.display = 'block'

}

function position()
{
	setArchiveHeight()
	positionListenForFree()
}

function onloadHook()
{
    $('book_' + selectedBook).className = 'selected'
    $('chapter_' + selectedChapter).className = 'selected'
    position()
}

function switchVersion(version, testament)
{
     new Ajax.Updater('book_view', '?view=fragments/archive'
             + '&amp;version=' + version
             + '&amp;t=' + testament, { evalScripts: true, onComplete: position })
}


/* playback */
var doPlay = true;

var playlistWindowParams = "left=0,screenX=0,top=0,screenY=0,height=350," +
          "innerHeight=350,width=500,innerWidth=500,resizable=yes,scrollbars=1"

var segmentWindowParams = "left=0,screenX=0,top=0,screenY=0,height=210," +
          "innerHeight=210,width=500,innerWidth=500,resizable=yes,scrollbars=1"

function playBook(enable)
{
    return play("/player/b/" + bookId, true, enable);
}

// a workaround for the usual play wanting to toggle the login box when not
// logged in
function special_play(url)
{
    return play(url, false, true)
}

function playAlbum( album_id )
{
    return play("/player/c/" + album_id, true, true );
}

function embedAlbum( album_id )
{
    return displayEmbed('/c/' + album_id, true)
}

function playSegment(segmentId, enable)
{
    return play("/player/s/" + segmentId + "/segmentId.mp3", false, enable);
}

function play(url, isList, enable)
{
    return openwWindowOrPulsate(url, "voxplayer", isList, enable)
}

function openwWindowOrPulsate(url, windowName, isList, enable)
{
    if (enable) {
	    
		var player = window.open(url, windowName, isList ? playlistWindowParams : segmentWindowParams)
        
		if(player==null || typeof(player)=="undefined")
		   init_popupWindow( '/static/dialogs/player_unavailable.html', 450, 200, '' );
        else
           player.focus()
        
    } else {
		
        var toPulsate = document.getElementById('msg_container');
        if (!toPulsate) {
            toPulsate = document.getElementById('login_wrapper');
        }
        new Effect.Pulsate(toPulsate, {pulses: 3, duration: 1});
    
	}
    return false;
}

function dl(url, enabled)
{
    if (enabled) {
        window.location = url
    }
    return false;
    /*
    if (fullAccess) {
        window.location = url
        return false
    }

    if (window.bookId != undefined) {
        if (inArray(bookAccess, bookId)) {
            window.location = url
            return false
        }
    }

    if (window.collectionId != undefined) {
        if (inArray(collectionAccess, collectionId)) {
            window.location = url
            return false
        }
    }

    // access of type COUNTED_DOWNLOADS shouldn't give book download access.
    if (url.indexOf("/dl/b/") == 0) {
        AlertBox("buy_archive");
        return false;
    }

    if (accessCount > 0) {
        accessCount--;
        updateAccessCount();
        window.location = url;
        return false;
    }

    AlertBox('buy_archive');
    return false;
    */
}

function downloadSegment(segmentId, enabled)
{
    return dl("/dl/s/" + segmentId + "/2/bible.mp3", enabled);
}

function downloadAlbum(albumId, shortname)
{
    window.location = "/dl/c/" + albumId + "/2/Voxbiblia_download.zip"
}

function downloadFreeAlbum(albumId)
{
   init_popupWindow( '/albums/downloadPopup', 453, 449, 'albumId=' + albumId);
   return false;
}

// login related functions

function validateLogin(responseText)
{
    if(responseText.indexOf("verifyPending") != -1) {
	AlertBox('verify_pending');
    } else if (responseText.indexOf("<input") != -1) {
        AlertBox('login_failed');
    }
}

function submitReminder(form, targetId)
{
    new Ajax.Updater(targetId, '/changepassword?action=sendMail',
            {parameters:Form.serialize(form)});
    return false;
}

function makeVisible(element)
{
   if (element && element.innerHTML.length > 0) {
       element.style.display="block";
   }
}
function makeInvisible(element)
{
    if (element) {
        element.style.display="none";
    }
}
