/**
 * @author wes
 */
var activeNav = 0;
$(document).ready(function(){
	$('a.bookThumb img').bind("mouseover", function(data) {
		//show red bar
		var thumbLeft = $(this).offset().left;
		var thumbTop = $(this).offset().top;
		var thumbHeight = $(this).height();
		var thumbWidth = $(this).width();
		$('#redHooverBar').css({position:'absolute',top:thumbTop+thumbHeight-3,left:thumbLeft,width:thumbWidth}).show();
	});
	
	$('a.bookThumb img').bind("mouseout", function(data) { $('#redHooverBar').hide();});
	$('#bookInfo #enlarge').bind("click", function() {
		$('#bookInfo #enlarge').unbind();
		$('#enlarge img').attr({src:'../img/icn_close_wt.gif'});
		$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_close_rd.gif'}); });
		$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_close_wt.gif'}); });
		
		$('#bookInfo p').slideUp(400);
		$('.largeImg').slideDown(500);
		$('.medImg').slideUp(400);
		readyToClose();
	});
	$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
	$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });

});

var activeId;
function getBookThumb(num,lang) {
	
	if(activeId != num && num != 0) {
		$('.read:hidden').show();
		$('.back:visible').hide();
	
	$('#bookInfo').slideUp(220, function() {
		$('.'+num+' .read').hide();
		$('.'+num+' .back').show();
		//call ajax
		$.get('../pages/functions/getbookinfo.php', {'lang':lang,'id':num}, function(data){
			
			$('#bookInfo').empty().html(data);
			prepGallery();
			$('#bookInfo').slideDown(400);
			$('div.contentBucket2 p:visible').slideUp(200);
		});
	});
	activeId = num;
	} else {
		$('.read:hidden').show();
		$('.back:visible').hide();
		$('#bookInfo:visible').slideUp(200).empty();
		$('div.contentBucket2 p:hidden').slideDown(400);
		activeId = "";
	}
}

var activeColId;
function getCollectionThumb(num,lang,type) {
	
	if(activeColId != num) {
	$('#bookInfo').slideUp(220, function() {
		
		//call ajax
		$.get('../pages/functions/getcollectioninfo.php', {'lang':lang,'id':num,'type':type}, function(data){
			
			$('#bookInfo').empty().html(data);
			prepGallery();
			$('#bookInfo').slideDown(400);
			$('div.contentBucket2 p:visible').slideUp(200);
		});
	});
	activeId = num;
	} else {
		$('#bookInfo').slideUp(200).empty();
		$('div.contentBucket2 p').slideDown(400);
		activeId = "";
	}
}

function readyToClose() {
	$('#bookInfo #enlarge').bind("click", function() {
		
	$('#bookInfo #enlarge').unbind();
	$('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'});
	$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_rd.gif'}); });
	$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_enlarge_wt.gif'}); });
		
		
	$('#bookInfo p').slideDown(400);
	$('.largeImg').slideUp(500);
	$('.medImg').slideDown(400);
	
	readyToEnlarge();		
	});
}

function readyToEnlarge() {
	$('#bookInfo #enlarge').bind("click", function() {
		$('#bookInfo #enlarge').unbind();
		$('#enlarge img').attr({src:'../img/icn_close_wt.gif'});
		$('#enlarge').bind("mouseover", function() { $('#enlarge img').attr({src:'../img/icn_close_rd.gif'}); });
		$('#enlarge').bind("mouseout", function() { $('#enlarge img').attr({src:'../img/icn_close_wt.gif'}); });
		
		$('#bookInfo p').slideUp(400);
		$('.largeImg').slideDown(500);
		$('.medImg').slideUp(400);
		
		readyToClose();
	});
}

//For Library
function updateLibrary(cat, character,numb) {
	getBookThumb(0);
	getLibraryItems(cat, character);
	getAlphabet(cat);

		sIFR.callbacks['.publicationLink td'][activeNav].changeCSS({
			'.sIFR-root': {'font-weight': 'bold'},
			'a': {'color': '#545454','text-decoration': 'none'},
			'a:hover': {'color': '#000000'}
		});
		
		sIFR.callbacks['.publicationLink td'][numb].changeCSS({'.sIFR-root': {'font-weight': 'bold'},
			'a': {'color': '#000000','text-decoration': 'none'},
			'a:hover': {'color': '#000000'}
		});
		activeNav = numb;
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) { 
    } else {
		return document[movieName];
    }

}
//sends character to swf file
function sendToActionScript(val) {
	  if (navigator.appName.indexOf("Microsoft") != -1) {
	  	//since this does not work, must to this
		$('.publishNav #letter').empty().html('<div class="charLet">'+val+'</div>');
	  } else {
	  	thisMovie("letter").sendToActionScript(val);
	  }
	  
}
function getLibraryItems(cat,character) {
	
	$('#rightContent').css("min-height",$('#rightContent').height());
	$('.libList').css('background','#ffffff').fadeOut(400, function() {
		if(character == undefined) {
			var params = {'category': cat};
		} else {
			var params = {'category': cat, 'character': character};
			
			$('.alphabet .chars .active').removeClass('active').addClass('inactive');
			$('.alphabet .chars #alphabetNavLetter'+character).removeClass('inactive').addClass('active');	
			//update swf letter
			sendToActionScript(character);
		}
		
		$.get('../pages/functions/getLibraryItems.php', params, function(data) {
			$('.libList').html(data).css('background','#ffffff').fadeIn(400,function() {
				$('a.bookThumb img').bind("mouseover", function(data) {
					//show red bar
					var thumbLeft = $(this).offset().left;
					var thumbTop = $(this).offset().top;
					var thumbHeight = $(this).height();
					var thumbWidth = $(this).width();
					$('#redHooverBar').css({position:'absolute',top:thumbTop+thumbHeight-3,left:thumbLeft,width:thumbWidth}).show();
				});
				$('a.bookThumb img').bind("mouseout", function(data) {
					$('#redHooverBar').hide();
				});
			});			
		});
	});
}

function getCollectionItemsByChar(type,cat,character) {

	$('#rightContent').css("min-height",$('#rightContent').height());
	$('.libList').css('background','#ffffff').fadeOut(400, function() {
		sendToActionScript(character);
		
		$.get('../pages/functions/getCollectionItems.php', {'type':type,'subtype':cat,'char':character}, function(data) {
			$('.libList:hidden').html(data).css('background','#ffffff').fadeIn(400);			
		});
	});
}

function getAlphabet(cat){
	$('#alphabetContainer').css("min-height", $('#alphabetContainer').height());
	$('.alphabet').css('background','#ffffff').fadeOut(400, function() {

		$.get('../pages/functions/getLibraryAlphabet.php', {'category': cat}, function(data) {
			$('#alphabetContainer').html(data).css('background','#ffffff').fadeIn(400);	
		});
	});
}

function updateAdditionalLinks(catId,numb) {
	$('#rightContent').css("min-height",$('#rightContent').height());
	$('.libList').css('background','#ffffff').fadeOut(400, function() {

		var params = {'catId': catId};
		
		sIFR.callbacks['.publicationLink td'][activeNav].changeCSS({
			'.sIFR-root': {'font-weight': 'bold'},
			'a': {'color': '#545454','text-decoration': 'none'},
			'a:hover': {'color': '#000000'}
		});
		
		sIFR.callbacks['.publicationLink td'][numb].changeCSS({'.sIFR-root': {'font-weight': 'bold'},
			'a': {'color': '#000000','text-decoration': 'none'},
			'a:hover': {'color': '#000000'}
		});
		activeNav = numb;
		$.get('../pages/functions/getAdditionalLinks.php', params, function(data) {
			$('.libList').html(data).css('background','#ffffff').fadeIn(400);			
		});
	});
}

//used in online collections
function updateCollectionList(type,numb) {
	getSubCatList(type);
	getCollectionAlphabet(type);
	$('#rightContent').css("min-height",$('#rightContent').height());
	$('.libList').css('background','#ffffff').fadeOut(400, function() {
		$('.publishNav .letter').html('A');
		sIFR.callbacks['.publicationLink td'][activeNav].changeCSS({
			'.sIFR-root': {'font-weight': 'bold'},
			'a': {'color': '#545454','text-decoration': 'none'},
			'a:hover': {'color': '#000000'}
		});
		
		sIFR.callbacks['.publicationLink td'][numb].changeCSS({'.sIFR-root': {'font-weight': 'bold'},
			'a': {'color': '#000000','text-decoration': 'none'},
			'a:hover': {'color': '#000000'}
		});
		activeNav = numb;
		$.get('../pages/functions/getCollectionItems.php', {'type':type}, function(data) {
			$('.libList:hidden').html(data).css('background','#ffffff').fadeIn(400);			
		});
	});
}

function updateCollectionListOnly(type,subtype) {
	//update alphabet
	getCollectionAlphabet(type,subtype);
	$('.publicationSubLink a.active').removeClass('active').addClass('inactive');
	
	$('#rightContent').css("min-height",$('#rightContent').height());
	$('.libList').css('background','#ffffff').fadeOut(400, function() {
		$('.publishNav .letter').html('A');
		$.get('../pages/functions/getCollectionItems.php', {'type':type,'subtype':subtype}, function(data) {
			$('.libList:hidden').html(data).css('background','#ffffff').fadeIn(400);
			$('.publicationSubLink a.'+subtype).removeClass('inactive').addClass('active');			
		});
	});
}

function getSubCatList(cat){
	$('#pubLinkContent').css("min-height", $('#pubLinkContent').height());
	$('.publicationSubLink').css('background','#ffffff').fadeOut(400, function() {
		$.get('../pages/functions/getCollectionSubCatList.php', {'category': cat}, function(data) {
			$('.publicationSubLink:hidden').html(data).css('background','#ffffff').fadeIn(400);	
		});
	});
}

function getCollectionAlphabet(cat,subcat){
	if(!subcat || subcat=="") subcat = "sketch";
	$('.alphabet').css('background','#ffffff').fadeOut(400, function() {
		$.get('../pages/functions/getCollectionAlphabet.php', {'category': cat,'subcategory':subcat}, function(data) {
			$('#alphabetContainer').html(data).css('background','#ffffff').fadeIn(400);	
		});
	});
}

