/*
 * Copyright 2009 Liquid Amber Inc.
 *
 * JavaScript page functions for home.jsp
 *
 */


/******************************************************************************************
*                        HOME IMAGE RELATED FUNCTIONS                                     *
******************************************************************************************/ 
var loadHomeImage = function(gid, file, direction) {
	$.ajax({
	url: "/ax/homeImage",
	data: {gid: gid, img: file, dir: direction},
	dataType: "json",
	async: true,
	success: function(data){
			if(data.code == 0){
				image = data.response;
				$("#homeImageContainer").hide();
				formatHomeImage(image);
				$("#homeImageContainer").fadeIn(250);
			}
			else {
				ajaxError(data.code, "loadHomeImage");
			}
		}
	});	
}

/**
  * The html generated by this function must be the same as is used for the initial home image,
  * which is loaded statically on the page for the purposes of SEO, i.e. so that the dumb crawlers can see
  * an <img> tag.
  */
var formatHomeImage = function(image) {
	if (!image) return;
	// The image
	var html = 
		'<a href="' + image.detailUrl + '?lsrc=hr">' +
		'<img class="clickable" src="' + image.imageUrl + '" height="' + image.h + '" width="' + image.w + '" /></a>';
		
	$("#homeImageContainer").html(html);
	
 	//Disable the context menu (right click over the home image)	
	$('#homeImageContainer').contextMenu('nonExistentDiv', {});
			
	// By line
	html = 'By <a href="' + image.storeUrl + '">' + image.by + '</a>, ' + image.media + '.';
	if (image.price) html += ' <span style="font-weight:bold;">' + image.price + '</span>'; 
	$("#homeImageBy").html(html);
	
	// Image controls click events
	$("#left-pointer").unbind().removeAttr("onclick");  // Remove the onclick set in HTML tag.
	$("#right-pointer").unbind().removeAttr("onclick"); // Ditto.
	$("#left-pointer").one("click", function() {loadHomeImage(image.gid, image.file, "prev");});
	$("#right-pointer").one("click", function() {loadHomeImage(image.gid, image.file, "next");});
}

/*********************************************************************************************
*                                     Hover previews                                         *
**********************************************************************************************/

var hoverPreviews;
var galleryHoverPreview = function(index, preview) {
	var footerHtml = '<span style="color:#666;font-weight:bold;">' + preview.title + '</span>' + 
		'<br> By <a href="' + preview.storeUrl + '">' + preview.by + '</a>' + 
		'<br>' + preview.imageCount + ' images • ' + preview.age + ' ago';
	footerHtml += '<br><span style="color:#666;font-weight:bold;">' + (preview.priceRange ? preview.priceRange : 'Not For Sale') + '</span>';
	hoverPreviews.showPreview(index, preview, footerHtml);
}

var imageHoverPreview = function(index, preview) {
	var footerHtml = '<span style="color:#666;font-weight:bold;">' + (preview.title ? preview.title : '<span style="color:#999;">No Title Available</span>') + '</span>' + 
		'<br> By <a href="' + preview.storeUrl + '">' + preview.by + '</a>' +
		'<br>' + preview.media;
	if (preview.dims) footerHtml += ' ' + preview.dims;
	footerHtml += '<br><span style="color:#666;font-weight:bold;">' + (preview.price ? preview.price : 'Not For Sale') + '</span>';
	
	hoverPreviews.showPreview(index, preview, footerHtml);
}

/*********************************************************************************************
*                                       Tags Frames                                          *
**********************************************************************************************/
var showMostActiveTags = function() {
	$('#allTags').hide();
	$('#mostActiveTags').show();	
}

var showAllTags = function() {
	$('#allTags').show();
	$('#mostActiveTags').hide();	
}

/*********************************************************************************************
*                                   Page load listener                                       *
**********************************************************************************************/
$(document).ready(
	function() 
		{
			hoverPreviews = new UnsnHoverPreviews();	
			showMostActiveTags();
		}
);

