﻿if (!window.console) {
	window.console = {log: function() {}};
}

var bookViewerFlash = null;

// weird flash external method
function thisMovie(movieName) {
	//return bookViewerFlash;

	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

// scrolling functions
function scrollRowIntoView(row) {
	var table =  $(row.parentNode.parentNode);
	var container = $(table.parentNode);
	
	//return;
	
	// get sizes and positions
	var rowPos = row.cumulativeOffset();
	var rowSize = row.getDimensions();
	var currentScroll = container.scrollTop;
	var containerSize = container.getDimensions();
	var containerPos = container.cumulativeOffset();

	var relativeRowPos = rowPos.top-containerPos.top;
	
	//console.log(relativeRowPos, currentScroll, ":", rowPos.top, containerPos.top, containerSize.height);
	
	
	// if the row is lower than the container	
	if (relativeRowPos+rowSize.height > containerSize.height + currentScroll) {
		
		// scroll so it's the bottom row
		container.scrollTop = relativeRowPos-containerSize.height+rowSize.height;
	
	// if the row is above	
	} else if (relativeRowPos < currentScroll ) {
		
		// scroll so it's the top row
		container.scrollTop = relativeRowPos;
	}
	
	
	//container.scrollTop = 100;
	
	//console.log(row.cumulativeOffset(), currentScroll);
}

var currentSelectedID = -1;
/// mouse over in the HTML table
function bookRowClick(bookID) {
	
	var row = $('book_' + bookID);
	var flash = thisMovie('BookViewerFlash');
	
	if (currentSelectedID == bookID) { 
				
		//console.log('undoing selected one', flash.unselectBookFromJs);
		
		row.removeClassName('selected');
		if (flash != null && flash.unselectBookFromJs)    
			flash.unselectBookFromJs(bookID);
			
		currentSelectedID = -1;
	} else {
		// undo last one
		if (currentSelectedID > -1) {
			$('book_' + currentSelectedID).removeClassName('selected');
			if (flash != null && flash.unselectBookFromJs)    
				flash.unselectBookFromJs(currentSelectedID);			
		}
	
		// highlight this one
		row.removeClassName('flashover');
		row.addClassName('selected');

		if (flash != null && flash.selectBookFromJs)    
			flash.selectBookFromJs(bookID);		
		
		currentSelectedID = bookID;
	
		scrollRowIntoView(row);
	}
	
	/*
	var row = $('book_' + bookID);
	row.removeClassName('flashover');
	
	
	if (row.hasClassName('selected')) {
	
	} else {
		
	}
	$('book_' + bookID).addClassName('flashover');

	// pull book out
	var flash = thisMovie('BookViewerFlash');
	if (flash != null && flash.highlightBookFromJs)    
		flash.highlightBookFromJs(bookID);
	*/
}

function bookRowOver(bookID) {
	

	// highlight
	var row = $('book_' + bookID)
	row.addClassName('flashover');


	
	//return;

	// pull book out
	var flash = thisMovie('BookViewerFlash');
	//alert(flash.highlightBookFromJs);
	if (flash && flash.highlightBookFromJs)    
		flash.highlightBookFromJs(bookID);
		
	scrollRowIntoView(row);
}

function bookRowOut(bookID) {
    
    $('book_' + bookID).removeClassName('flashover');
    
    var flash = thisMovie('BookViewerFlash');
    if (flash != null && flash.unhighlightBookFromJs)    
        flash.unhighlightBookFromJs(bookID);    
}

// mouse over from flash
function bookOverFromFlash(bookID) {
    var row = $('book_' + bookID);
    row.addClassName('flashover');
    scrollRowIntoView(row);
}
function bookOutFromFlash(bookID) {
    $('book_' + bookID).removeClassName('flashover');
}

// click from flash
function bookSelectFromFlash(bookID) {
    $$('.selected').each( function(el) { el.removeClassName('selected'); });    
    $('book_' + bookID).addClassName('selected');
    currentSelectedID = bookID;
}
function bookUnselectFromFlash(bookID) {
    $('book_' + bookID).removeClassName('selected');
    currentSelectedID = -1;
}

/* 
Version 1

var lastSelectedBookID = 0;
/// mouse over in the HTML table
function bookRowOver(bookID) {
	// unhighlight
	//$$('.selected').each( function(el) { el.removeClassName('selected'); });
	
	if (lastSelectedBookID != bookID) {			
		if (lastSelectedBookID > 0)
			$('book_' + lastSelectedBookID).removeClassName('selected');

		// highlight
		$('book_' + bookID).addClassName('selected');

		// pull book out
		var flash = thisMovie('BookViewerFlash');
		if (flash != null && flash.highlightBookJs)    
			flash.highlightBookJs(bookID);
	        
		lastSelectedBookID = bookID;
    }
}

function bookRowOut(bookID) {
    // don't do anything on out
    return;
    
    $('book_' + bookID).removeClassName('selected');
    
    var flash = thisMovie('BookViewerFlash');
    if (flash != null)    
        flash.unhighlightBooksJs();    
}

// mouse over from flash
function bookOverFromFlash(bookID) {
    $('book_' + bookID).addClassName('flashover');
}
function bookOutFromFlash(bookID) {
    $('book_' + bookID).removeClassName('flashover');
}

// click from flash
function bookSelectFromFlash(bookID) {
    $$('.selected').each( function(el) { el.removeClassName('selected'); });    
    $('book_' + bookID).addClassName('selected');
}
function bookUnselectFromFlash(bookID) {
    $('book_' + bookID).removeClassName('selected');
}
*/

// tagging
function selectTag(cb) {
	var rows = $$('.bookrow');
	var tagchecks = $$('.tagcheckbox');
	
	// examine all the checkboxes, figure out if all are checked
	var requiredTags = [];
	var noneChecked = true;
	tagchecks.each (function(cb) { 
		if (cb.checked) {
			noneChecked = false;
			requiredTags.push(cb.id);
		}	
	});
	
	
	// if nothing is checked, then highlight everything
	if (noneChecked) {
		rows.each(function(r) { 
			r.removeClassName('deemphasized');
			//r.setOpacity(1);	 
		});
	} else {
		// go through rows see if they have anything checked
		rows.each(function(r) { 
			var hasClass = false;
			requiredTags.each(function(t) {
				if (r.hasClassName(t)) 
					hasClass = true;
			});
			if (!hasClass) {
				//r.setOpacity(0.2);	
				r.addClassName('deemphasized');
			} else{
				//r.setOpacity(1);	
				r.removeClassName('deemphasized');
			}
		});
	}
	
	
}



var BC = {};
BC.MainMenu = Class.create(
{
    initialize: function(menuid, listid) {
        this.menu = $(menuid);
        this.list = $(listid);
        this.list.style.display = 'none';
    
        Event.observe(this.menu, 'mouseover', this.menuMouseOverHandler.bindAsEventListener(this));       
        this.docListener = this.documentMouseMoveHandler.bindAsEventListener(this);
    },
    menuMouseOverHandler: function(e) { 
        var menuPos = this.menu.positionedOffset();
        var menuSize = this.menu.getDimensions();
        
        this.menu.addClassName('selected');
        this.list.style.top = (menuPos.top + menuSize.height) + 'px';
        this.list.style.left = (menuPos.left) + 'px';
        this.list.style.display = '';
        
        Event.observe(document, 'mousemove', this.docListener);
    },
    documentMouseMoveHandler: function(e) {
        var mousePos = Event.pointer(e);

        var menuPos = this.menu.positionedOffset();
        var menuSize = this.menu.getDimensions();

        var listPos = this.list.positionedOffset();
        var listSize = this.list.getDimensions();        
        
        var mouseIsOver = false;
        
        if ( (mousePos.x > menuPos.left-1 && mousePos.x < menuPos.left + menuSize.width+1) &&
             (mousePos.y > menuPos.top-1 && mousePos.y < menuPos.top + menuSize.height+1) ) {
            mouseIsOver = true;     
        }
        if ( (mousePos.x > listPos.left-1 && mousePos.x < listPos.left + listSize.width+1) &&
             (mousePos.y > listPos.top-1 && mousePos.y < listPos.top + listSize.height+1) ) {
            mouseIsOver = true;     
        }          
        
        //console.log(mouseIsOver, mousePos, menuPos, menuSize);
        
        if (!mouseIsOver)  {
            this.list.style.display = 'none';
            Event.stopObserving(document, 'mousemove', this.docListener);            
            this.menu.removeClassName('selected');
        }
        
    }    
    
});
