/*
######################################################################################
######                                                                        #######
######     LIBRARY SCRIPTS			                                         #######
######                                                                      #######
##################################################################################
*/

	var Poems = new Object();

/********************************************************************************/
/***** DETAIL OVERLAY FUNCTIONS ************************************************/
/******************************************************************************/
	
	var libraryPopup;
	var itemHeight;
	var columnLeft;
	var columRight;
	var clmCount;
	var popupLeft;
	var popupTop;
	var popupRight;
	var popupBottom;
	
	Poems.items = function(id,popup) {
		Poems.itemsSet(id,popup);
		$(window).resize(function() { Poems.itemsSet(id,popup); });
		$(window).change(function() { Poems.itemsSet(id,popup); });
	};
	
	Poems.itemsSet = function(id,popup) {
		libraryPopup = $('#' + popup);
		
		var itemList	= $('#' + id).find('li.item');
		var column		= $('#' + id).find('ul.library_list');
		var columnLeft	= $(column[0]);
		var columRight	= $(column[1]);
		
		var countList 	= $(columnLeft).find('li');
		
		var count	= countList.length;
		
		itemHeight 	= $(itemList[0]).height();

		for(var i=0; i<itemList.length; i++) {
			var i2 = i + 1;
			var parent = $(itemList[i]).parent();
			if($(parent).attr('class').indexOf('left') != -1) {
				var column = 'left';
			} else {
				var column = 'right';
			}
			var link = $(itemList[i]).find('a');

			if(i2 <= count) {
				var activeNo = i2;
			} else {
				var activeNo = i2 - count;
			}
			var top = (itemHeight * activeNo) - itemHeight;
			
			Poems.itemEvents(link,column,top,i);
		}
		
		var cOffset = $('#' + id).offset();
		var cWidth	= $('#' + id).width();
		var cHeight	= $('#' + id).height();

		$(document).mousemove(function(e) {
			var xPos	= e.pageX;
			var yPos	= e.pageY;
			
			var minL	= cOffset.left;
			var minT	= cOffset.top;
			var maxL	= minL + cWidth;
			var maxT	= minT + cHeight;
			
			if(xPos > popupLeft && xPos < popupRight && yPos > popupTop && yPos < popupBottom) {
				var status = 'ACTIVE2';
			} else {
				var status = 'INACTIVE';
				var aList = $('#' + id).find('a');
				for(var i=0; i<aList.length; i++) {
					$(aList[i]).attr({'class':''});
				}
				$(libraryPopup).hide();
				
				/*
				if(xPos > popupLeft && xPos < popupRight && yPos > popupTop && yPos < popupBottom) {
					var status = 'ACTIVE';
				} else {
					var status = 'INACTIVE';
					var aList = $('#' + id).find('a');
					for(var i=0; i<aList.length; i++) {
						$(aList[i]).attr({'class':''});
					}
					$(libraryPopup).hide();
				}
				*/
			}
		});
	}
	
	Poems.itemEvents = function(link,column,top,i) {
		$(link).unbind('mouseover');
		$(link).mouseout(function() { $(link).attr({'class':''}); });
		$(link).mouseover(function() {
			$(libraryPopup).hide();
			$(link).attr({'class':'active'});

		// Get the content
			var title				= libraryList[i].title;
			var poet				= libraryList[i].poet;
			var type				= libraryList[i].type;
			var age_group			= libraryList[i].age_group;
			var date_added			= libraryList[i].date_added;
			var last_updated		= libraryList[i].last_updated;
			var avg_rating			= libraryList[i].avg_rating;
			var avg_rating_class	= libraryList[i].avg_rating_class;
			var viewed				= libraryList[i].viewed;
			//var reviewed			= libraryList[i].reviewed;
			var desc				= libraryList[i].desc;
			var imageFile			= libraryList[i].imageFile;

			$(libraryPopup).find('.title strong').html(stripslashes(title));
			$(libraryPopup).find('.open_link').html(stripslashes(title));
			$(libraryPopup).find('.poet').html(poet);
			$(libraryPopup).find('.type').html(type);
			$(libraryPopup).find('.age_group').html(age_group);
			$(libraryPopup).find('.date_added').html(date_added);
			$(libraryPopup).find('.last_updated').html(last_updated);
			$(libraryPopup).find('.avg_rating').html(avg_rating);
			$(libraryPopup).find('.viewed').html(viewed + ' times');
			//$(libraryPopup).find('.reviewed').html(reviewed + ' times');
			$(libraryPopup).find('.desc').html(stripslashes(desc));
			if(imageFile != '') {
				$(libraryPopup).find('.image').html('<img class="img_profile" src="/dynamic/profiles/' + imageFile + '" /><div class="dots_horizontal">&nbsp;</div>');
			} else {
				$(libraryPopup).find('.image').html('');
			}

			$('#avg_rating').attr({'class':'photo_rated photo_rated_' + avg_rating_class});
			$('#avg_rating').html(avg_rating);
			$(libraryPopup).show();

			var popupHeight = $(libraryPopup).height();
			var topPos = top - (popupHeight / 2) + (itemHeight / 2);
			if(column == 'left') {
				$(libraryPopup).css({'left':'193px','right':'auto','top':topPos + 'px'});
			} else {
				$(libraryPopup).css({'left':'auto','right':'290px','top':topPos + 'px'});
			}
			
			var offset = $(link).offset();
			/*
			popupLeft 	= offset.left;
			popupTop 	= offset.top;
			popupRight	= popupLeft + $(libraryPopup).width();
			popupBottom	= popupTop + $(libraryPopup).height();
			*/
			popupLeft 	= offset.left;
			popupTop 	= offset.top;
			popupRight	= popupLeft + 311;
			popupBottom	= popupTop + $(link).height();
		});
	}


/********************************************************************************/
/****** AJAX FUNCTIONS *********************************************************/
/******************************************************************************/

	Poems.Read = function(PoemId) {
		var url = document.location.href;
		var splitter	= url.split('#');
		if(splitter[1] != '') {
			document.location.href = splitter[0] + '#' + PoemId;
		} else {
			document.location.href = document.location.href + '#' + PoemId;
		}
		popupWindow.Start('popup_library','poem',PoemId);
	}


/***** STAR RATING *****/
	
	Poems.StarListing = function() {
		var aList = $('#photo_star_shell').find('a');
		
		for(var i=0;i<aList.length;i++) {
			Poems.StarRollover($(aList[i]),i);
		}
	}

	Poems.StarRollover = function(element,level) {
		$(element).mouseover(function() {
			$('#photo_star_rating').attr({'class':'photo_star_' + (level + 1)});
		});
		$(element).mouseout(function() {
			$('#photo_star_rating').attr({'class':'photo_star_0'});
		});
	}

	Poems.RatingAdd = function(PoemId, Rating, Type) {
		$('#photo_star_shell').html('&nbsp;');
		$.post("/local/templates/poems/ajax/rating.ajax.php", { PoemId: PoemId, Rating: Rating, Type: Type },
			function(data){
				$('#photo_star_shell').html('Thank you for rating this poem');
			}
		);
	}



/********************************************************************************/
/***** SHARE THIS BOOKMARKING SCRIPTS ******************************************/
/******************************************************************************/

	Poems.Bookmarking = function() {
		var url = document.location.href;
		if(url.indexOf('#') != -1) {
			var splitter	= url.split('#');
			var PoemId		= splitter[1];
			
			if(PoemId != '') {
				Poems.Read(PoemId);
			}
		}
	}
	
	$(document).ready(function() { Poems.Bookmarking() });











