var galleryPreviewClass="galleryPreviewContainer";
var galleryPreviewClassHighlight="galleryPreviewContainerHighlight";
var galleryPreviewSingleSideClass="gallerySingleSideContainer";
var galleryPreviewSingleSideClassHighlight="gallerySingleSideContainerHighlight";
var templateIdsForUploadArtwork=new Array();
function validateKeywords(filterPrefixFlag){
	if(!document.forms["galleryFrm"].keywords.value.match(/\w/) || document.forms["galleryFrm"].keywords.value.match(/^Enter\sKeyword/))
	{alert("You forgot to enter a keyword.");
	return false;
	}
    else {
		if(document.forms["galleryFrm"].productIDview.value == document.forms["galleryFrm"].productid.value)
		document.forms["galleryFrm"].productIDview.disabled=true;
		if(document.forms["galleryFrm"].keywords_and.value == "")
		document.forms["galleryFrm"].keywords_and.disabled = true;
		if(document.forms["galleryFrm"].TemplateID.value == "")
		document.forms["galleryFrm"].TemplateID.disabled = true;
		var keywordSearch=document.getElementById('searchKeywords').value;
		if(filterPrefixFlag && keywordSearch.match(/group\{(\w|\-)+\}/))
		document.getElementById('searchKeywords').value=keywordSearch.replace(/group\{(\w|\-)+\}/,"");
		return true;
	}
}
function galleryGroup(categoryName){
	document.forms["galleryFrm"].keywords.value=categoryName;
	if(validateKeywords(false)){
		document.forms["galleryFrm"].submit();
		}
	}
function initGalleryHtml(){setPageResultsCount();showSearchResultsDesc();highlightTemplateCategory(firstTermHtmlEncoded);var pagignationHTML="";if(totalPagesForResults>1){if(currentPageNumber>1){pagignationHTML+="<a class='PagingPrevious' href='"+getUrlForPagination(paginationOffset-galleryResultsPerPageSetting)+"'>< Prev</a>";}
var addPeriodsOnce=false;for(var pageNum=1;pageNum<=totalPagesForResults;pageNum++){var classForPaginationNumber="pageNumber";if(currentPageNumber==pageNum)
classForPaginationNumber="PageNumberSelected";var pageDifference=currentPageNumber+pageNum-2;if((pageNum+5)<currentPageNumber)
continue;if((pageNum-8)>currentPageNumber){if(!addPeriodsOnce){pagignationHTML+=" ...";addPeriodsOnce=true;}
continue;}
pagignationHTML+="<a href='"+getUrlForPagination(Math.abs(paginationOffset-galleryResultsPerPageSetting*pageDifference))+"' class='"+classForPaginationNumber+"'>"+pageNum+"</a>";}
if(currentPageNumber<totalPagesForResults){pagignationHTML+="<a class='PagingNext' href='"+getUrlForPagination(paginationOffset+galleryResultsPerPageSetting)+"'>Next ></a>";}}
try{document.getElementById("PaginationLinks").innerHTML=pagignationHTML;document.getElementById("PaginationLinks2").innerHTML=pagignationHTML;}
catch(e){}}

function highlightTemplateCategory(categoryName){
	if(categoryName.match(/group\{(\w|\-)+\}/)){
		categoryName=categoryName.replace(/group/i,"");
		categoryName=categoryName.replace(/\{/,"");
		categoryName=categoryName.replace(/\}/,"");
		categoryName=categoryName.replace(/\-/g,"");
		if(document.getElementById("grp_"+categoryName)){
			document.getElementById("grp_"+categoryName).setAttribute('style','font-weight:bold; font-size:14px;color:#666;');
		} else {
			alert("Template Group Undefined: "+"grp_"+categoryName)
		} 
	//The code below is to make the All link bold.	
	}
	document.forms["galleryFrm"].keywords.value="";
}

function DynamicPageName() {
	//Containers to build the product option dynamically based on the product ID.
	var ProductOptionName = "";
	var ProductDimensionExt = "";
	// Remove the group {} from the 
	var TrimmedGroupIdentifier = firstTermHtmlEncoded;
	
if(TrimmedGroupIdentifier.match(/group\{(\w|\-)+\}/)){	
	//Control to fill the ProductOptionName with the correct Product option name
	if (prodIdGlobal == 211 || prodIdGlobal == 212 || prodIdGlobal == 213 ) {
			ProductOptionName = "Standard Postcards";
	} else if (prodIdGlobal == 214 || prodIdGlobal == 215 || prodIdGlobal == 216 ) {
			ProductOptionName = "Personalized Data";
	} else if (prodIdGlobal  == 217 || prodIdGlobal == 218 || prodIdGlobal == 219 ) {
			ProductOptionName = "Mailing Service";
	}

	//Control to fill the ProductDimensionExt with the correct size
	if (prodIdGlobal == 211 || prodIdGlobal == 214 || prodIdGlobal == 217 ) {
			ProductDimensionExt = "4 x 6";
	} else if (prodIdGlobal == 212 || prodIdGlobal == 215 || prodIdGlobal == 218 ) {
			ProductDimensionExt = "5.5 x 8.5";
	} else if (prodIdGlobal  == 213 || prodIdGlobal == 216 || prodIdGlobal == 219 ) {
			ProductDimensionExt = "6 x 11";
	}
		document.getElementById("GalleryBreadCrumb").innerHTML =  ProductOptionName+ " &gt; " +ProductDimensionExt+ " &gt; " +TrimmedGroupIdentifier+" Postcards";
		document.getElementById("GalleryPageHeader").innerHTML =  TrimmedGroupIdentifier+" Postcards";
} else if(TrimmedGroupIdentifier.match(/ /)) {
		document.getElementById("GalleryBreadCrumb").innerHTML =  ProductOptionName+ " &gt; " +ProductDimensionExt+ " &gt; " +TrimmedGroupIdentifier+" Postcards";
		document.getElementById("GalleryPageHeader").innerHTML =  TrimmedGroupIdentifier+" Postcards";
	
}
}



function showSearchResultsDesc(){var totalMatchesCount=galleryImageIDsArr.length;if(totalMatchesCount==0)
var resltDesc="No Matches";else if(totalMatchesCount==1)
var resltDesc="1 Match";else
var resltDesc="Total of "+totalMatchesCount+" matches";document.getElementById('galleryResultsDescription').innerHTML=resltDesc;}
function hoverGalleryTable(templateID){for(var i=0;i<galleryHash.length;i++){if(galleryHash[i].TempID==templateID){if(galleryHash[i].TempPreviewCount>1){document.getElementById('mutliSideTemplateTable_'+templateID).setAttribute('class','multiSidePreviewTableHighlight');}
break;}}}
function leaveGalleryTable(templateID){for(var i=0;i<galleryHash.length;i++){if(galleryHash[i].TempID==templateID){if(galleryHash[i].TempPreviewCount>1){document.getElementById('mutliSideTemplateTable_'+templateID).setAttribute('class','multiSidePreviewTable');}
break;}}}
function hoverGalleryImage(templateID){for(var i=0;i<galleryHash.length;i++){if(galleryHash[i].TempID==templateID){for(var j=0;j<galleryHash[i].TempPreviewCount;j++){document.getElementById('galleryID_'+templateID+'_'+j).setAttribute('class',galleryPreviewClassHighlight);}
break;}}}
function leaveGalleryImage(templateID){for(var i=0;i<galleryHash.length;i++){if(galleryHash[i].TempID==templateID){for(var j=0;j<galleryHash[i].TempPreviewCount;j++){document.getElementById('galleryID_'+templateID+'_'+j).setAttribute('class',galleryPreviewClass);}
break;}}}
function hoverGallerySpan(templateID){document.getElementById('singleTemplatePreviewSpan_'+templateID).setAttribute('class',galleryPreviewSingleSideClassHighlight);}
function leaveGallerySpan(templateID){document.getElementById('singleTemplatePreviewSpan_'+templateID).setAttribute('class',galleryPreviewSingleSideClass);}
function changeResultsPerPage(resultCount){
	if(resultCount=="")
return;document.forms["galleryFrm"].resultsCount.value=resultCount;
if(validateKeywords(false)){
	$("#keywordSearchSubmitBtn").click();
	}
}
function setPageResultsCount(){var listMenuObj=document.getElementById('resultsPerPageList');for(var i=0;i<listMenuObj.length;i++){if(listMenuObj[i].value==galleryResultsPerPageSetting){listMenuObj[i].selected=true;break;}}}
function displayGalleryImages(){var returnHtml="";for(var i=0;i<galleryHash.length;i++){var isUploadArtworkTemplate=false;for(var j=0;j<templateIdsForUploadArtwork.length;j++){if(galleryHash[i].TempID==templateIdsForUploadArtwork[j]){isUploadArtworkTemplate=true;break;}}
if(isUploadArtworkTemplate)
var templatePickURL="/product_uploadartwork.php?productid="+prodIdGlobal;else
var templatePickURL="javascript:pickArtwork('"+galleryHash[i].TempID+"')";if(galleryHash[i].TempPreviewCount>1&&galleryHash[i].SideNumber==0){returnHtml+='<table cellpadding="0" cellspacing="0" class="multiSidePreviewTable" id="mutliSideTemplateTable_'+galleryHash[i].TempID+'" onmouseover="hoverGalleryTable(\''+galleryHash[i].TempID+'\')" onmouseout="leaveGalleryTable(\''+galleryHash[i].TempID+'\')">';returnHtml+="<tr>";var totalSideCount=parseInt(galleryHash[i].TempPreviewCount);for(var j=i;j<(i+totalSideCount);j++){returnHtml+="<td align='center'>"+galleryHash[j].SideName+"</td>";}
returnHtml+="</tr>";returnHtml+="<tr>";for(var j=i;j<(i+totalSideCount);j++){returnHtml+="<td align='center'>";returnHtml+='<a href="'+templatePickURL+'" border="0">';returnHtml+='/<img border="0" src="/gallery/'+galleryHash[j].PreviewID+'.jpg" class="'+galleryPreviewClass+'" id="galleryID_'+galleryHash[j].TempID+'_'+galleryHash[j].SideNumber+'" onmouseover="hoverGalleryImage(\''+galleryHash[j].TempID+'\')" onmouseout="leaveGalleryImage(\''+galleryHash[j].TempID+'\')">';returnHtml+='</a>';returnHtml+="</td>";}
returnHtml+="</tr>";returnHtml+="</table>";}
else if(galleryHash[i].TempPreviewCount==1){var imageWidth=parseInt(galleryHash[i].ImgWidth);var imageHeight=parseInt(galleryHash[i].ImgHeight);var spanWidth="360px";returnHtml+='<span id="singleTemplatePreviewSpan_'+galleryHash[i].TempID+'" onmouseover="hoverGallerySpan(\''+galleryHash[i].TempID+'\')" onmouseout="leaveGallerySpan(\''+galleryHash[i].TempID+'\')" class="'+galleryPreviewSingleSideClass+'" " style="padding:0;margin:0;display:block;float:left;position:relative;width:'+spanWidth+';">';returnHtml+='<table cellpadding="0" cellspacing="0" border="0">';returnHtml+='<tr><td>';returnHtml+='<a href="'+templatePickURL+'">'
returnHtml+='<img border="0" src="/gallery/'+galleryHash[i].PreviewID+'.jpg" class="'+galleryPreviewClass+'" id="galleryID_'+galleryHash[i].TempID+'_'+galleryHash[i].SideNumber+'" onmouseover="hoverGalleryImage(\''+galleryHash[i].TempID+'\')" onmouseout="leaveGalleryImage(\''+galleryHash[i].TempID+'\')"  width="'+imageWidth+'" height="'+imageHeight+'" style="position:relative; margin:0;">';returnHtml+='</a>';returnHtml+='</td></tr>';returnHtml+='<tr><td align="center"><br /><br /></td></tr>';returnHtml+='</table>';returnHtml+='</span>';}}
document.getElementById("templatePreviews").innerHTML=returnHtml;}
function pickArtwork(templateID){document.location="/product_loadtemplate.php?newtemplatetype="+galleryViewGlobal+"&newtemplateid="+templateID+"&productid="+prodIdGlobal+"&productIDview="+prodIdViewGlobal+"&cancelurl="+retUrlEnc+"&continueurl="+contUrlEnc;}
function getUrlForPagination(paginationOffset){
	return"/artworkGallery.cgi?productid="+prodIdGlobal+"&resultsCount="+galleryResultsPerPageSetting+"&keywords="+firstTermUrlEncoded+"&offset="+paginationOffset+"&TemplateID="+templateID;}
