var CurrentMouseOverDivIDs = "";
var LoadedDivIDs = "";
var MousePositionX = 0;
var MousePositionY = 0;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
    }
  }
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
  }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function mm_swapimgrestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function mm_preloadimages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=mm_preloadimages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
    }
  }
}

function mm_swapimage() { //v3.0
  var i,j=0,x,a=mm_swapimage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=mm_findobj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
  }
}

function mm_findobj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=mm_findobj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ShowCatalogSimplified(CatalogOID, ticket)
{
	ShowCatalog(CatalogOID, ticket, "ViewCatalog.aspx?Left=1:amp:Right=-1:amp:CatalogOID=" + CatalogOID);
	
}

function ShowCatalog(CatalogOID, Password, ShowPage)
{
	var width = 975;
	var height = 686;
	var useragent = navigator.userAgent.toLowerCase();
	
	if (useragent.indexOf("safari") != -1)
	{
	    width = 320;
	    height = 500;
	}
	var property = "top=0, left=0, resizable=1, width=" + width + ", height=" + height;
	//alert(ShowPage);
	var wincatalog = window.open("CatalogView.aspx?CatalogOID=" + CatalogOID + "&SupplierID=168664&ShowPage=" + ShowPage + "&Password=" + Password + "&Rand=" + Math.random(), 'CatalogViews', property);
	wincatalog.focus();
}

function ShowCatalogSimplified2(CatalogOID, SupplierID, ticket)
{
	ShowCatalog2(CatalogOID, SupplierID, ticket, "ViewCatalog.aspx?Left=1:amp:Right=-1:amp:CatalogOID=" + CatalogOID);
	
}

function ShowCatalog2(CatalogOID, SupplierID, Password, ShowPage)
{
	var intWidth = screen.width;
	//var width = 980;
	var width = 978;
	var height = 686;
	
	var useragent = navigator.userAgent.toLowerCase();
	if (useragent.indexOf("safari") != -1)
	{
	    width = 320;
	    height = 500;
	}
	

	var property = "top=0, left=0, resizable=1, width=" + width + ", height=" + height;
	//alert(property);
	var wincatalog = window.open("CatalogView.aspx?CatalogOID=" + CatalogOID + "&SupplierID=" + SupplierID + "&ShowPage=" + ShowPage + "&Password=" + Password + "&Rand=" + Math.random(), 'CatalogViews', property);
	//	var wincatalog = window.open("OpenPage.aspx?CatalogOID=" + CatalogOID + "&SupplierID=168664&ShowPage=" + ShowPage + "&Password=" + Password, 'ActiveMerchandiser', property);
	wincatalog.focus();
}


function ShowSearchResult(CatalogOID, SupplierID, Password, SearchCriteria)
{
	var intWidth = screen.width;
	var width = 976;
	var height = 686;

	var strSubDir = "";
	var property = "top=0, left=0, resizable=1, width=" + width + ", height=" + height;
	var wincatalog = window.open("CatalogView.aspx?CatalogOID=" + CatalogOID + "&SupplierID=" + SupplierID + "&Password=" + Password + "&SearchCriteria=" + SearchCriteria + "&Rand=" + Math.random(), 'CatalogViewsSearch', property);
	wincatalog.focus();

}

function CloseCurrentMouseOverDivs()
{
	
	while (CurrentMouseOverDivIDs.indexOf('|') != -1 && CurrentMouseOverDivIDs != "|")
	{
		var DivIDtoClose = CurrentMouseOverDivIDs.substring(0, CurrentMouseOverDivIDs.indexOf('|')).replace('|').replace('|');
		CurrentMouseOverDivIDs = CurrentMouseOverDivIDs.substring(CurrentMouseOverDivIDs.indexOf('|')+1);
		try
		{
		if (document.getElementById(DivIDtoClose) != null)
		{ document.getElementById(DivIDtoClose).style.visibility = "hidden";}
		}
		catch (e) {}
	}
}

function showcatalogfirstpages(Catalog_OID)
{
	CloseCurrentMouseOverDivs()
	
	if (LoadedDivIDs.indexOf('|'+ Catalog_OID+'|') != -1)
	{
		CurrentMouseOverDivIDs += "div" + Catalog_OID + "|";
		document.getElementById("div" + Catalog_OID).style.visibility = "visible";
		return 1;
	}
	else
	{
		return 0;
	 }	
		
}

function showfirstpages2(Catalog_OID)
{
	if (showcatalogfirstpages(Catalog_OID) != 1)
	{
		try
		{
			var searchcriteria = "";
				if (document.getElementById("txtSearch") != null)
				{
					searchcriteria = document.getElementById("txtSearch").value;
				}
			ActiveMerchandiser.catalogsearchall.GetHTMLforFirstPages(Catalog_OID, searchcriteria, showfirstpages_CallBack);
		}
		catch (e){}
	}
}

function showfirstpages(Catalog_OID)
{
        if (showcatalogfirstpages(Catalog_OID) != 1)
		{
			try
			{
				var searchcriteria = "";
				if (document.getElementById("txtSearch") != null)
				{
					searchcriteria = document.getElementById("txtSearch").value;
				}
				ActiveMerchandiser.catalogsearchallcategorized.GetHTMLforFirstPages(Catalog_OID, searchcriteria, showfirstpages_CallBack);
			}
			catch (e){}
		}
}

function showfirstpages_CallBack(response)
{
  if (response.error != null)
  {    
    document.getElementById("div" + Catalog_OID).innerHTML = "";
  }  
  var HTMLForFirstPages = response.value; 
  
  //if the response wasn't what we expected  
  if (HTMLForFirstPages == null)
  {
	document.getElementById("div" + Catalog_OID).innerHTML = "";
  }
  else
  {
	if (HTMLForFirstPages.indexOf('|') != -1)
	{
		var Catalog_OID = HTMLForFirstPages.substring(0, HTMLForFirstPages.indexOf('||'));
		HTMLForFirstPages = HTMLForFirstPages.substring(HTMLForFirstPages.indexOf('||') + 2);
		document.getElementById("div" + Catalog_OID).innerHTML = HTMLForFirstPages;
		LoadedDivIDs += '|' + Catalog_OID + '|';
		var parentleft = $('#divCover' + Catalog_OID).offset().left;
		var parenttop = $('#divCover' + Catalog_OID).offset().top;
		var myleft = parentleft + 150;
		var mytop = parenttop - 70;
		if (mytop < 0)
		{
			mytop = 0;
		}

		if (myleft > 500) {
			
			var windowwidth = 800;
	  		if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
    				windowwidth = window.innerWidth;
   			} else if( document.documentElement &&  document.documentElement.clientWidth  ) {
    				//IE 6+ in 'standards compliant mode'
    				windowwidth = document.documentElement.clientWidth;
   			} else if( document.body && document.body.clientWidth  ) {     //IE 4 compatible
    				windowwidth = document.body.clientWidth;
    			}
			//alert(windowwidth);
			if (windowwidth - myleft < 360) { myleft = myleft - 452; }

        }
		
		document.getElementById("div" + Catalog_OID).style.left  = myleft + "px";
		document.getElementById("div" + Catalog_OID).style.top  = mytop + "px";
		
		
		if (MousePositionX <= parentleft + 150 && MousePositionX >= parentleft && MousePositionY <= parenttop + 150 && MousePositionX >= parenttop)
		{
			CurrentMouseOverDivIDs += "div" + Catalog_OID + "|";
			document.getElementById("div" + Catalog_OID).style.visibility = "visible";
		}
	}	
  }
  
}


function hidefirstpages()
{
	CloseCurrentMouseOverDivs();
}

