/* Document ready event. */
$(document).ready(titlesReady);

/* Project document ready event function. */
function titlesReady()
{
	carouselListing();
	
	/* Title Details */
	$(".jqTopImageList").click(showTitleDetails);
	
	/* Comic Thumbimage */
	$(".jqComicThumb").live("click", showComicThumbImage);
	
	/* Main image pop-up */
	$(".jqMainImageLink").live("click", showComicMainImagePopUp);
	
	/* Close pop up window */
	$(".jqPopUpClose").live("click", closeComicMainImagePopUp);
	
	/* Pop up image - next image */
	$(".jqPopUpNext").live("click", showNextComicMainImagePopUp);
	
	/* Pop up image - previous image */
	$(".jqPopUpPrevious").live("click", showPreviousComicMainImagePopUp);
	
	/* Library details of comic */
	$(".jqTitleLibraryTab").live("click", showLibraryTitleDetails);
	
	/* Comic details on Latest button */
	$(".jqTitleLatestTab").live("click", submitTitleDetails);
	
	/* Title review */
	$(".jqTitleReviewTab").live("click", showTitleReview);
	
	/* Title Gallery */
	$(".jqTitleGalleryTab").live("click", showTitleGallery);
	
	/* Title Downloads */
	$(".jqTitleDownloadTab").live("click", showTitleDownloads);
	
	
	/* Comic details overlay from Library */
	$(".jqLibraryThumb").live("click", showComicDetailsOverlay);
	
	/* Close comic Read online */
	$(".jqComicOverlayClose").live("click", closeComicOverlayPopUp);
	
	/* Comic Read online */
	$(".jqComicReadOnline").live("click", showComicReadOnline);
	
	/* Close readonline pop up window */
	$(".jqReadOnlineClose").live("click", closeReadOnlineImagePopUp);
	
	/* Readonline Pop up image - next image */
	$(".jqReadOnlineNext").live("click", showNextReadOnlineMainImagePopUp);
	
	/* Readonline Pop up image - previous image*/
	$(".jqReadOnlinePrevious").live("click", showPreviousReadOnlineMainImagePopUp);
	
	/*Gallery image overlay */
	$(".jqGalleryThumb").live("click", showGalleryImagePopUp);
	
	/* close gallery pop up */
	$(".jqGalleryPopUpClose").live("click", closeGalleryMainImagePopUp);
	
	/* Pop up image - next image */
	$(".jqGalleryPopUpNext").live("click", showNextGalleryMainImagePopUp);
	
	/* Pop up image - previous image */
	$(".jqGalleryPopUpPrevious").live("click", showPreviousGalleryMainImagePopUp);
	
	/* Motion Comics */
	$(".jqTitleMotionComicTab").live("click", showMotionComics);
	
	/* Motion comic video overlay */
	$(".jqMotionComicVideo").live("click", showMotionComicsVideoOverlay);
	
	/* Close Motion comic video overlay */
	$(".jqCloseMotionComicOverlay").live("click", closeMotionComicsVideoOverlay);
	
	/* Pop up image - next image */
	$(".jqMotionComicPopUpNext").live("click", showNextMotionComicVideoPopUp);
	
	/* Pop up image - previous image */
	$(".jqMotionComicPopUpPrevious").live("click", showPreviousMotionComicVideoPopUp);
	
	$(".activeTab").removeClass("activeTab");
	$('#jqTitleSubNav li:first a').addClass("activeTab");
}

/* Top Image carousel listing */	
function carouselListing()
{
	var selectedTitle  	= $('.titleList input[value="' + titleId + '"]'); 
	var itemIndex 		= $('.titleList input[type="hidden"]').index(selectedTitle);
	$('.jqTopImageList img').not(':first').css({'opacity':'0.5', 'filter': 'alpha(opacity = 50)'});
	if($(".JMyCarousel > ul > li").length > 7)
	{
		$(".JMyCarousel").jMyCarousel({   
			visible: '100%',
			circular: true,
			speed: 500,
			btnPrev: '#imgPrevious',
			btnNext: '#imgNext',
			start: itemIndex * 125
		});
	}
	var currentLink  = selectedTitle.parents('.jqTopImageList:first');
	$(".jqCurrentTitle").removeClass("jqCurrentTitle");
	currentLink.addClass("jqCurrentTitle");
	setIndicatorPosition(currentLink);
}

function setIndicatorPosition(currentLink)
{
	/*relativeLink = currentLink.children("a:first");
//	alert('Link Left = ' + relativeLink.offset().left + ' : ' + $('#dvIndicator').offset().left - 100);
	
	$('#dvIndicator').css({'position':'absolute','left': relativeLink.offset().left + (relativeLink.outerWidth() /2) -100});*/
	
	var relativeOffset = currentLink.offset();
	var indicatorParentOffset = $("#jqDivIndicatorParent").offset();
	
	$('#dvIndicator').css({
							'position':'absolute',
							'left': relativeOffset.left + (currentLink.outerWidth() /2) - indicatorParentOffset.left - ($('#dvIndicator').outerWidth()/2)
						  });
	
	
}

/* Title details*/
function showTitleDetails(e)
{
	//	Select the current clicked link.
	$(".jqCurrentTitle").removeClass("jqCurrentTitle");
	var currentLink		= $(this).addClass("jqCurrentTitle");

	$('.jqTopImageList img').css({'opacity':'0.5', 'filter': 'alpha(opacity = 50)'});
	var currentImage 	= currentLink.find('img');
	currentImage.css({'opacity':'1', 'filter': 'alpha(opacity = 100)'});
	setIndicatorPosition(currentLink);
	//var imgPosition 	= currentImage.offset();
	//var relativeLink 	= currentLink.children("a:first");
	
	
	//$("a:first").html(relativeLink.offset().left +  " " + currentLink.outerWidth());
	//$('#dvIndicator').css({'position':'absolute','left': relativeLink.offset().left + ((currentLink.outerWidth() - $('#dvIndicator').outerWidth())/2) + 4 });
	
	//setIndicatorPosition(imgPosition.left);
	//	Select the current clicked link.
	//selectCurrentClickedTab(currentLink);
	
	//var titleDeatilContainer = currentLink.parents(".jqTitleMainDiv");
	titleId 			     = currentLink.find(".jqHidTitleID:first").val();
	
	submitTitleDetails(e, null, currentLink);
	
	return false;						   
}

function submitTitleDetails()
{
	//	Select the current clicked link.
	if(arguments.length > 2)
	{
		var currentLink			 = arguments[2];
		var ajaxOverlay = currentLink.find('img');
	}
	else
	{
		var currentLink			 = $(this);
		var ajaxOverlay = currentLink;
	}
	
	//	Select the current clicked link.
	selectCurrentClickedTab(currentLink);
	
	var ajaxSubmitOption 	= {
								url 	 : titleDetailsUrl,
								data     : {titleid : titleId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			$("#jqComicDetails").html(unescape(data.details));
                                                    $("#jqComicDetails").pngFix();
										   			$(".activeTab").removeClass("activeTab");
										   			$('#jqTitleSubNav li:first a').addClass("activeTab");
										   		}
										   },
							    beforeSend : function()
					   						 {
					   						 	showAjaxLoader(ajaxOverlay);
					   						 },
					   			complete : function()
				   						   {
				   						 	 hideAjaxLoader();
				   						   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

/* Comic Thumb images*/
function showComicThumbImage()
{
	//	Select the current clicked link.
	var currentLink			 = $(this);
	
	var thumbImageSrc 	 	 = currentLink.find('img:first').attr('src');
	var mainImageSrc	 	 = thumbImageSrc.replace(/51X77/, "238X361");
	
	var mainImage			= $(".jqMainImageLink").find('img:first');
	var currentImage		= mainImage.attr("src");
	
	if(mainImageSrc != currentImage)
	{
		mainImage.fadeOut("slow",	function()
									{
										mainImage.attr('src', mainImageSrc);
										showAjaxLoader(currentLink.find('img:first'));
										//$(this).fadeIn("slow");
									});
		//mainImage.attr('src', mainImageSrc);
		mainImage.unbind("load").load(function()
				{
					hideAjaxLoader();
					$(this).fadeIn("slow");
				});
	}
	return false;
}

/* Comic main image pop-up */
function showComicMainImagePopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqComicPopUp");
	
	var mainImageSrc 			 = currentLink.find('img:first').attr('src');
	var mainImagePopUpSrc	 	 = mainImageSrc.replace(/238X361/, "512X777");
	
	var popupHeight 			 = dvPopUpContainer.height();
	var windowHeight 			 = $(window).height();
	//var popupScrollTop 			 = document.documentElement.scrollTop;
	var popupScrollTop 			 = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);
	var popupTop 				 = popupScrollTop;
	if(windowHeight > popupHeight)
	{
		popupTop = (windowHeight - popupHeight)/2 + popupScrollTop - 10;
	}

	if(dvPopUpContainer.is(":hidden"))
	{
		showAjaxLoader(currentLink.find('img:first'));
		
		dvPopUpContainer.css({
								position : "absolute",
								left: 0,
								top : 0,
								width : $(document).width(),
								height: $(document).height()
								});
		if($(".jqComicThumb").length <= 1)
		{
			$(".jqComicPrevNext").hide();
		}
		else
		{
			$(".jqComicPrevNext").show();
		}
		
		$(".jqMainPopUpImage").children('img:first').attr('src', mainImagePopUpSrc).unbind("load").load(function(){
			dvPopUpContainer.find(".jqComicImagePopup:first").css({"padding-top" : popupTop}).end().fadeIn("slow",function()
																												{
																													hideAjaxLoader();
																												});
		});
	}
	return false;
}

/* Close comic pop up window */
function closeComicMainImagePopUp()
{
	var dvPopUpContainer 		 = $(".jqComicPopUp");
	if(! dvPopUpContainer.is(":hidden"))
	{
		dvPopUpContainer.fadeOut("slow");
	}
	return false;
}

/* Pop up image - next image */
function showNextComicMainImagePopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqComicPopUp");
	var currentMainImageSrc 	 = $(".jqMainPopUpImage").children('img:first').attr('src');
	var currentSmallImageSrc	 = currentMainImageSrc.replace(/512X777/, "51X77");
	
	var nextImage 				 = $(".jqComicThumbImages").find("img[src='" + currentSmallImageSrc + "']")
								    .parents(".jqLiComicThumbImage:first").next(".jqLiComicThumbImage:first")
								    .find("img:first"); //.attr("src");
	var nextImageSrc = '';
	if(nextImage.length > 0)
	{
		nextImageSrc = nextImage.attr("src");
	}
	else
	{
		nextImageSrc = $(".jqComicThumbImages").find("img:first").attr("src");
	}
	nextImageSrc = nextImageSrc.replace(/51X77/, "512X777");
	showAjaxLoader(currentLink.find('img:first'));
	if(! dvPopUpContainer.is(":hidden"))
	{
		 $(".jqMainPopUpImage").children('img:first').attr('src', nextImageSrc).unbind("load").load(function()
					{
					hideAjaxLoader();
					});

	}
	return false;
}

/* Pop up image - previous image */
function showPreviousComicMainImagePopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqComicPopUp");
	var currentMainImageSrc 	 = $(".jqMainPopUpImage").children('img:first').attr('src');
	var currentSmallImageSrc	 = currentMainImageSrc.replace(/512X777/, "51X77");
	var prevImage 				 = $(".jqComicThumbImages").find("img[src='" + currentSmallImageSrc + "']")
								    .parents(".jqLiComicThumbImage:first").prev(".jqLiComicThumbImage:first")
								    .find("img:first");
	var prevImageSrc = '';
	if(prevImage.length > 0)
	{
		prevImageSrc = prevImage.attr("src");
	}
	else
	{
		prevImageSrc = $(".jqComicThumbImages").find("img:last").attr("src");
	}
	prevImageSrc = prevImageSrc.replace(/51X77/, "512X777");
	showAjaxLoader(currentLink.find('img:first'));
	if(! dvPopUpContainer.is(":hidden"))
	{
		 $(".jqMainPopUpImage").children('img:first').attr('src', prevImageSrc).unbind("load").load(function()
					{
					hideAjaxLoader();
					});

	}
	return false;
}

/* Library details of Title */
function showLibraryTitleDetails()
{
	var currentLink			 = $(this);
	//	Select the current clicked link.
	selectCurrentClickedTab(currentLink);
	
	var ajaxSubmitOption 	= {
								url 	 : titleLibraryUrl,
								data     : {titleid : titleId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			$(".jqDetailsMainDiv").html(unescape(data.libraryDetails));
                                                    $(".jqDetailsMainDiv").pngFix();
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink);
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

function showTitleReview()
{
	var currentLink			 = $(this);
	//	Select the current clicked link.
	selectCurrentClickedTab(currentLink);
	
	var ajaxSubmitOption 	= {
								url 	 : titleReviewUrl,
								data     : {titleid : titleId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			$(".jqDetailsMainDiv").html(unescape(data.details));
                                                    $(".jqDetailsMainDiv").pngFix();
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink);
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

/* Gallery listing. */
function showTitleGallery()
{
	var currentLink			 = $(this);
	//	Select the current clicked link.
	selectCurrentClickedTab(currentLink);
	
	var ajaxSubmitOption 	= {
								url 	 : titleGalleryUrl,
								data     : {titleid : titleId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			$(".jqDetailsMainDiv").html(unescape(data.details));
                                                    $(".jqDetailsMainDiv").pngFix();
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink);
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

/*Downloads listing*/
function showTitleDownloads()
{
	var currentLink			 = $(this);
	//	Select the current clicked link.
	selectCurrentClickedTab(currentLink);
	
	var ajaxSubmitOption 	= {
								url 	 : titleDownloadsUrl,
								data     : {titleid : titleId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			$(".jqDetailsMainDiv").html(unescape(data.details));
                                                    $(".jqDetailsMainDiv").pngFix();
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink);
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

/* Activate the current clicked tab*/
function selectCurrentClickedTab(currentLink)
{
	$(".activeTab").removeClass("activeTab");
	currentLink.addClass("activeTab");
}

/* Show Comic details overlay */
function showComicDetailsOverlay()
{
	//	Select the current clicked link.
	var currentLink			 = $(this);
	
	var comicId 			     = currentLink.find(".jqHidComicId:first").val();
	
	submitComicDetails(comicId, currentLink);

	return false;						   
}

/* Submit Comic Id and get display the comic details overlay */
function submitComicDetails(comicId, currentLink)
{
	var dvComicOverlayPopUpContainer = $(".jqComicOverlayPopUp");
	
	var ajaxSubmitOption 	= {
								url 	 : comicDetailsOverlayUrl,
								data     : {comicid : comicId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			var overlay = $(unescape(data.comicDetailsOverlay));
										   			
										   			$(".jqComicPopUp:first").before(overlay);
										   			
										   			var popupHeight 			 = overlay.height();
													var windowHeight 			 = $(window).height();
													var popupScrollTop 			 = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);
													var popupTop 				 = popupScrollTop;
													
										   			if(windowHeight > popupHeight)
													{
														popupTop = (windowHeight - popupHeight)/2 + popupScrollTop - 10;
													}
										   			
										   			overlay.css({
																position : "absolute",
																left: 0,
																top : 0,
																width : $(document).width(),
																height: $(document).height() - popupTop
															  });
										   			overlay.css({"padding-top" : popupTop}).fadeIn();
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink.find('img'));
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

/* Close comic pop up window */
function closeComicOverlayPopUp()
{
	var dvOverlayPopUpContainer 		 = $(".jqComicOverlayPopUp");
	if(! dvOverlayPopUpContainer.is(":hidden"))
	{
		dvOverlayPopUpContainer.fadeOut("slow", function(){ $(this).remove(); });
	}
	return false;
}

var readOnlineImages = null;
/* Show comic read online */
function showComicReadOnline()
{
	readOnlineImages = null;
//	Select the current clicked link.
	var currentLink			 = $(this);
	
	var comicId 			     = currentLink.find("#jqHidComicID").val();
	
	var ajaxSubmitOption 	= {
								url 	 : comicReadOnlineUrl,
								data     : {comicid : comicId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			readOnlineImages = data.comicReadOnlineDetails;
										   			
										   			if(readOnlineImages.length > 0)
										   			{
										   				showReadOnlineMainImage(0);
										   			}
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink.find("img:first"));
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							};
$.ajax(ajaxSubmitOption);

	return false;
}

//Read online Images
function showReadOnlineMainImage(imageIndex)
{
	if(readOnlineImages != null)
	{
		var totalImages = readOnlineImages.length;
		
		if(imageIndex >= 0 && imageIndex < totalImages)
		{
			var mainImage = readOnlineImages[imageIndex].Thumbnail;
			
			var dvPopUpContainer 		 = $(".jqReadOnlinePopUp");
			
			var mainImagePopUpSrc	 	 = comicReadOnlineImageUrl + mainImage;
			
			var popupHeight 			 = dvPopUpContainer.height();
			var windowHeight 			 = $(window).height();
			var popupScrollTop 			 = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);
			var popupTop 				 = popupScrollTop;
			if(windowHeight > popupHeight)
			{
				popupTop = (windowHeight - popupHeight)/2 + popupScrollTop - 10;
			}

			if(dvPopUpContainer.is(":hidden"))
			{
				$(".jqMainReadOnlineImage").children('img:first').attr('src', mainImagePopUpSrc).data('Index', imageIndex);
				
				dvPopUpContainer.css({
										position : "absolute",
										left: 0,
										top : 0,
										width : $(document).width(),
										height: $(document).height()
										});
				
				dvPopUpContainer.find(".jqReadOnlineImagePopup:first").css({"padding-top" : popupTop}).end().fadeIn("slow");
			}
			else
			{
				$(".jqMainReadOnlineImage").children('img:first').attr('src', mainImagePopUpSrc).data('Index', imageIndex);
			}
		}
	}
	
	return false;
}

/* Close readonline pop up window */
function closeReadOnlineImagePopUp()
{
	var dvPopUpContainer 		 = $(".jqReadOnlinePopUp");
	if(! dvPopUpContainer.is(":hidden"))
	{
		dvPopUpContainer.fadeOut("slow");
	}
	return false;
}

/* Pop up image - next image */
function showNextReadOnlineMainImagePopUp()
{
	var currentImageIndex 		 = $(".jqMainReadOnlineImage").children('img:first').data('Index');
	
	if(!isNaN(currentImageIndex) && currentImageIndex >= 0)
	{
		showReadOnlineMainImage(currentImageIndex + 1);
	}
	return false;
}

/* Pop up image - previous image */
function showPreviousReadOnlineMainImagePopUp()
{
	var currentImageIndex 		 = $(".jqMainReadOnlineImage").children('img:first').data('Index');
	
	if(!isNaN(currentImageIndex) && currentImageIndex >= 0)
	{
		showReadOnlineMainImage(currentImageIndex - 1);
	}
	return false;
}

/* Gallery main image pop up */
function showGalleryImagePopUp()
{
//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqGalleryPopUp");
	
	var mainImageSrc 			 = currentLink.find('img:first').attr('src');
	var mainImagePopUpSrc	 	 = mainImageSrc.replace(/125X190/, "512X777");
	
	var popupHeight 			 = dvPopUpContainer.height();
	var windowHeight 			 = $(window).height();
	var popupScrollTop 			 = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);
	var popupTop 				 = popupScrollTop;
	if(windowHeight > popupHeight)
	{
		popupTop = (windowHeight - popupHeight)/2 + popupScrollTop - 10;
	}

	if(dvPopUpContainer.is(":hidden"))
	{
		showAjaxLoader(currentLink.find('img:first'));
		
		dvPopUpContainer.css({
								position : "absolute",
								left: 0,
								top : 0,
								width : $(document).width(),
								height: $(document).height()
								});
		if($(".jqDivGalleryThumb").length <= 1)
		{
			$(".jqGalleryPrevNext").hide();
		}
		else
		{
			$(".jqGalleryPrevNext").show();
		}
		
		$(".jqGalleryMainPopUpImage").children('img:first').attr('src', mainImagePopUpSrc).unbind("load").load(function(){
			dvPopUpContainer.find(".jqGalleryImagePopup:first").css({"padding-top" : popupTop}).end().fadeIn("slow",function()
																												{
																													hideAjaxLoader();
																												});
		});
	}
	return false;
}

/* Close gallery pop up window */
function closeGalleryMainImagePopUp()
{
	var dvPopUpContainer 		 = $(".jqGalleryPopUp");
	if(! dvPopUpContainer.is(":hidden"))
	{
		dvPopUpContainer.fadeOut("slow");
	}
	return false;
}

/* Pop up image - next image */
function showNextGalleryMainImagePopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqGalleryPopUp");
	var currentMainImageSrc 	 = $(".jqGalleryMainPopUpImage").children('img:first').attr('src');
	var currentSmallImageSrc	 = currentMainImageSrc.replace(/512X777/, "125X190");
	
	var nextImage 				 = $(".jqGalleryThumbImages").find("img[src='" + currentSmallImageSrc + "']")
								    .parents(".jqDivGalleryThumb:first").next(".jqDivGalleryThumb:first")
								    .find("img:first"); //.attr("src");
	var nextImageSrc = '';
	if(nextImage.length > 0)
	{
		nextImageSrc = nextImage.attr("src");
	}
	else
	{
		nextImageSrc = $(".jqGalleryThumbImages").find("img:first").attr("src");
	}
	nextImageSrc = nextImageSrc.replace(/125X190/, "512X777");
	showAjaxLoader(currentLink.find('img:first'));
	if(! dvPopUpContainer.is(":hidden"))
	{
		 $(".jqGalleryMainPopUpImage").children('img:first').attr('src', nextImageSrc).unbind("load").load(function()
																									{
																										hideAjaxLoader();
																									});
	}
	return false;
}

/* Pop up image - previous image */
function showPreviousGalleryMainImagePopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqGalleryPopUp");
	var currentMainImageSrc 	 = $(".jqGalleryMainPopUpImage").children('img:first').attr('src');
	var currentSmallImageSrc	 = currentMainImageSrc.replace(/512X777/, "125X190");
	var prevImage 				 = $(".jqGalleryThumbImages").find("img[src='" + currentSmallImageSrc + "']")
								    .parents(".jqDivGalleryThumb:first").prev(".jqDivGalleryThumb:first")
								    .find("img:first");
	var prevImageSrc = '';
	if(prevImage.length > 0)
	{
		prevImageSrc = prevImage.attr("src");
	}
	else
	{
		prevImageSrc = $(".jqGalleryThumbImages").find("img:last").attr("src");
	}
	prevImageSrc = prevImageSrc.replace(/125X190/, "512X777");
	showAjaxLoader(currentLink.find('img:first'));
	if(! dvPopUpContainer.is(":hidden"))
	{
		 $(".jqGalleryMainPopUpImage").children('img:first').attr('src', prevImageSrc).unbind("load").load(function()
																										{
																											hideAjaxLoader();
																										});
	}
	return false;
}

/* Motion COMICS DETAILS */
function showMotionComics()
{
	var currentLink			 = $(this);
	//	Select the current clicked link.
	selectCurrentClickedTab(currentLink);
	
	var ajaxSubmitOption 	= {
								url 	 : motionComicUrl,
								data     : {titleid : titleId},
								type	 : "post",
								dataType : "json",
								success  : function(data)
										   {
										   		if(data.error)
												{
											   		if(data.error == 'errorShowImage')
													{
														showAjaxError(data.info);
													}
													else
													{
														handleAjaxError(data.error);
													}
												}
										   		else if(data.success)
										   		{
										   			$(".jqDetailsMainDiv").html(unescape(data.details));
                                                    $(".jqDetailsMainDiv").pngFix();
										   		}
										   },
								beforeSend : function()
											 {
											 	showAjaxLoader(currentLink);
											 },
								complete : function()
										   {
										   	 hideAjaxLoader();
										   }
							  };
	$.ajax(ajaxSubmitOption);
	return false;
}

/* Motion comic video overlay */
function showMotionComicsVideoOverlay()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var motionComicId		     = currentLink.find("input[name='hidMotionComicID']:first").val();
	var motionComicVideo		 = unescape(currentLink.find("input[name='hidMotionComicVideo']:first").val());
	var motionComicTitle		 = unescape(currentLink.find("input[name='hidMotionComicTitle']:first").val());
	var motionComicPrice		 = unescape(currentLink.find("input[name='hidMotionComicPrice']:first").val());
	//alert(motionComicVideo);
	//return false;
	
	var dvPopUpContainer 		 = $(".jqMotionComicVideoPopUp");
	
	var popupHeight 			 = dvPopUpContainer.height();
	var windowHeight 			 = $(window).height();
	var popupScrollTop 			 = document.documentElement.scrollTop ? document.documentElement.scrollTop : (document.body.scrollTop ? document.body.scrollTop : 0);
	var popupTop 				 = popupScrollTop;
	if(windowHeight > popupHeight)
	{
		popupTop = (windowHeight - popupHeight)/2 + popupScrollTop - 10;
	}
	
	if(dvPopUpContainer.is(":hidden"))
	{
		//showAjaxLoader(currentLink.find('img:first'));
		//alert(dvPopUpContainer.length);
		$(".jqMotionComicTrialVideo").html(motionComicVideo);
		$(".jqMotionComicTitle").html(motionComicTitle);
		if(motionComicPrice > 0)
		{
			$(".jqMotionComicPrice").html('$<span>' + motionComicPrice + '</span>');
		}
		
		//show and hide prev/next button
		if($(".jqMotionComicVideo").length <= 1)
		{
			$(".jqMotionPrevNext").hide();
		}
		else
		{
			$(".jqMotionPrevNext").show();
		}
		//
		
		dvPopUpContainer.css({
								"position" 		: "absolute",
								"left"			: 0,
								"top" 			: 0,
								"width" 		: $(document).width(),
								"height"		: $(document).height()- popupTop,
								"padding-top" 	: popupTop
								}).show();
		dvPopUpContainer.data("MotionComicId", motionComicId);
		/*$(".jqMainPopUpImage").children('img:first').attr('src', mainImagePopUpSrc).unbind("load").load(function(){
			dvPopUpContainer.find(".jqComicImagePopup:first").css({"padding-top" : popupTop}).end().fadeIn("slow",function()
																												{
																													hideAjaxLoader();
																												});*/
		//dvPopUpContainer.show();
		
						
	}
	return false;
}

/* Close gallery pop up window */
function closeMotionComicsVideoOverlay()
{
	var dvPopUpContainer 		 = $(".jqMotionComicVideoPopUp");
	if(! dvPopUpContainer.is(":hidden"))
	{
		dvPopUpContainer.fadeOut("slow");
	}
	return false;
}


/* Pop up image - next image */
function showNextMotionComicVideoPopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqMotionComicVideoPopUp");
	var currentMotionComicId = dvPopUpContainer.data("MotionComicId");
	
	var currentMotionComicThumbnail = $("#jqMotionComicVideoContainer" + currentMotionComicId);
	var nextMotionComicThumbnail = currentMotionComicThumbnail.next(".jqMotionComicVideoContainer");
	
	if(nextMotionComicThumbnail.length == 0)
	{
		nextMotionComicThumbnail = $(".jqMotionComicVideoContainer:first");
	}
	
	var motionComicId		     = nextMotionComicThumbnail.find("input[name='hidMotionComicID']:first").val();
	var motionComicVideo		 = unescape(nextMotionComicThumbnail.find("input[name='hidMotionComicVideo']:first").val());
	var motionComicTitle		 = unescape(nextMotionComicThumbnail.find("input[name='hidMotionComicTitle']:first").val());
	var motionComicPrice		 = unescape(nextMotionComicThumbnail.find("input[name='hidMotionComicPrice']:first").val());

	$(".jqMotionComicTrialVideo").html(motionComicVideo);
	$(".jqMotionComicTitle").html(motionComicTitle);
	if(motionComicPrice > 0)
	{
		$(".jqMotionComicPrice").html('$<span>' + motionComicPrice + '</span>');
	}
	
	dvPopUpContainer.data("MotionComicId", motionComicId);
	return false;
}
function showPreviousMotionComicVideoPopUp()
{
	//	Select the current clicked link.
	var currentLink				 = $(this);
	var dvPopUpContainer 		 = $(".jqMotionComicVideoPopUp");
	var currentMotionComicId = dvPopUpContainer.data("MotionComicId");
	
	var currentMotionComicThumbnail = $("#jqMotionComicVideoContainer" + currentMotionComicId);
	var nextMotionComicThumbnail = currentMotionComicThumbnail.prev(".jqMotionComicVideoContainer");
	
	if(nextMotionComicThumbnail.length == 0)
	{
		nextMotionComicThumbnail = $(".jqMotionComicVideoContainer:last");
	}
	
	var motionComicId		     = nextMotionComicThumbnail.find("input[name='hidMotionComicID']:first").val();
	var motionComicVideo		 = unescape(nextMotionComicThumbnail.find("input[name='hidMotionComicVideo']:first").val());
	var motionComicTitle		 = unescape(nextMotionComicThumbnail.find("input[name='hidMotionComicTitle']:first").val());
	var motionComicPrice		 = unescape(nextMotionComicThumbnail.find("input[name='hidMotionComicPrice']:first").val());

	$(".jqMotionComicTrialVideo").html(motionComicVideo);
	$(".jqMotionComicTitle").html(motionComicTitle);
	if(motionComicPrice > 0)
	{
		$(".jqMotionComicPrice").html('$<span>' + motionComicPrice + '</span>');
	}
	
	dvPopUpContainer.data("MotionComicId", motionComicId);
	return false;
}
