/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * written by Alen Grakalic (http://cssglobe.com)
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		xOffset = 10;
		yOffset = 30;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */
	$("a.preview").hover(function(e){

		// Remove any left over "#preview" before adding.
		$("#preview").remove();
		
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ jQuery(this).attr("previewimg") +"' alt='Image preview' />"+ c +"</p>");
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};
// starting the script on page load
$(document).ready(function(){
	imagePreview();
});

// Called by the FeedListControl when the search button is clicked.
var handleFeedSearchButtonClick = function(sender) {
	try
	{
		$('.btn_submit').hide();
		$('.search_wait_msg_hldr').show();
		$('.srch_box').css("background", "#f0f0f0").attr("disabled","disabled");
		new AjaxRequest(sender, null, writeToDiv, true).run();

		return false;
	}
	catch (e)
	{
		return true;
	}
};

// Called by the FeedListControl when the "See All" button is clicked.
var seeAllButtonClick = function(elemPrefix) {
	try
	{
		var buttonElem = $('.seeall_' + elemPrefix);
		buttonElem.toggleClass("expand");
		buttonElem.html(buttonElem.hasClass("expand") ? "Collapse" : "See All");
		
		$('.seeall_item_' + elemPrefix).toggle();
	}
	catch (e)
	{
	}
	return false;
};

// Called by the FeedListTheaterSearchControl when the pagination buttons are clicked.
var gotoTheaterPage = function(buttonElem, pageCount)
{
	var numberOfPageLinks = 10;

	var targetPage = parseInt($(buttonElem).attr("pageNum"));
	
	var lowPageBound = 1;
	var highPageBound = pageCount;
	
	// If the page count is greater than the max number of page links then
	// we have to figure out the subset of page links to show.
	if (pageCount > numberOfPageLinks)
	{
		// Calculate what the lower bound should be in order for the current page to be exactly centered.
		lowPageBound = targetPage - parseInt(numberOfPageLinks / 2);

		// If the lower bound falls off the end, then set it to one.
		if (lowPageBound < 1)
		{
			lowPageBound = 1;
		}

		// Now calculate the upper bound based on the lower bound.
		highPageBound = lowPageBound + numberOfPageLinks - 1;

		// If the upper bound exceeds the page count then we have to adjust it.
		if (highPageBound > pageCount)
		{
			// Set the upper bound to the page count.
			highPageBound = pageCount;

			// Calculate the lower bound based off the upper bound.
			lowPageBound = highPageBound - numberOfPageLinks + 1;

			// This shouldnt happen but just to be safe.
			if (lowPageBound < 1)
			{
			    lowPageBound = 1;
			}
		}
	}
	
	var transPageNum = lowPageBound;
	for (var i = 1; i <= numberOfPageLinks; i ++)
	{
		$('#pg' + i + ' a').attr('pageNum', transPageNum + '').html(transPageNum + '');
		
		var pageLI = $('#pg' + i);
		
		if (transPageNum == targetPage)
		{
			pageLI.addClass('selected');
		}
		else
		{
			pageLI.removeClass('selected');
		}
		
		if (transPageNum <= highPageBound)
		{
			pageLI.show();
		}
		else
		{
			pageLI.hide();
		}
	
		transPageNum ++;
	}
		
	if (targetPage > 1)
	{
		$('#pgback a').attr('pageNum', (targetPage - 1) + '');
		$('#pgback').css('visibility', 'visible');
	}
	else
	{
		$('#pgback').css('visibility', 'hidden');
	}
		
	if (targetPage < pageCount)
	{
		$('#pgnext a').attr('pageNum', (targetPage + 1) + '');
		$('#pgnext').css('visibility', 'visible');
	}
	else
	{
		$('#pgnext').css('visibility', 'hidden');
	}
	
	// Hide all the pages.
	$(".thtrrow").hide();
	// Show the target page.
	$(".thtrpg_" + targetPage).show();

	return false;
};

// Called by the FeedListTheaterSearchControl when a theater is deselected.
var removeTheater = function(buttonElem, theaterId, hiddenFieldId)
{
	$('#' + hiddenFieldId).val(theaterId);
	if (handleFeedSearchButtonClick)
	{
	    return handleFeedSearchButtonClick(buttonElem);
	}
	else
	{
	    return true;
	}
}
