function kwiz_ig_OnLoadPage()
{
    kwiz_ig_SetPagingActiveLink();
}

function  kwiz_ig_FindParentMainForm(elm)
{
    var el = elm.parentElement;
    while(el != null)
    {
        if((el.id.indexOf("_mainForm_") < 0) && (el.id.indexOf("_mainForm") > 0))
            return el;
        el = el.parentElement;
    }
    return null;
}
function  kwiz_ig_FindGalleryImageCell(elm)
{
    var el = elm.parentElement;
    while(el != null)
    {
        if(el.id == "galleryImageCell")
            return el;
        el = el.parentElement;
    }
    return null;
}
function  kwiz_ig_ItemMouseOver(elm)
{
	if((elm != document.body.calttel) && (document.body.calttel != null))
	{
		document.body.calttel.popup = null;
	}
	document.body.calttel = elm;
	if(elm.mouseovertype == "show_tooltip")
	{
	    window.setTimeout(kwiz_ig_ShowTooltip, 200);
	}
	if(elm.mouseovertype == "update_presentation")
	{
	    window.setTimeout(kwiz_ig_ShowPresentation, 200);
	}
}
function kwiz_ig_ShowPresentation(e)
{
    if(e == null) 
	    e = document.body.calttel;
	var mainForm = kwiz_ig_FindParentMainForm(e);
	var imgGallCell = kwiz_ig_FindGalleryImageCell(e);
	mainForm.all["presentationImage"].src = e.presentationimage;
	mainForm.all["presentationProperties"].innerHTML = imgGallCell.all["presentationPropertiesHiddenDiv"].innerHTML;
}
function kwiz_ig_ItemClick(elm)
{
	if(elm.onclicktype == "new_window")
	{
	    window.open(elm.presentationimage);
	}
	if(elm.onclicktype == "replace_gallery")
	{
        var mainForm = kwiz_ig_FindParentMainForm(elm);
        if(mainForm == null)
			return;
        var hiddenId = "";
        var dataId = "";
        if(mainForm.parentElement.kwiz_ig_paging == null)
        {
		    hiddenId = mainForm.parentElement.kwiz_ig_paging = mainForm.kwiz_ig_paging;
		    dataId = mainForm.parentElement.kwiz_ig_data = mainForm.kwiz_ig_data;
        }
        else
        {
	        hiddenId = mainForm.parentElement.kwiz_ig_paging;
		    dataId = mainForm.parentElement.kwiz_ig_data;
        }
        document.all[hiddenId].value = elm.imageindex;
        var url = mainForm.kwiz_ig_asyncurl + "?isrender=true&showgallerytype=presentation&imageindex=" + elm.imageindex + "&dir=" + mainForm.dir;
        kwiz_ig__RenderImageGallery(url, document.all[dataId].innerHTML, mainForm.parentElement);
	}
	if(elm.onclicktype == "under_gallery")
	{
	    kwiz_ig_ShowPresentation(elm);
	}
}
function  kwiz_ig_ItemMouseOut(elm)
{
	if(elm.mouseovertype == "show_tooltip")
	{
	    elm.toElement = window.event.toElement;
	    window.setTimeout(kwiz_ig_HideTooltip, 150);
	}
}

function kwiz_ig_ShowTooltip()
{
	var e = document.body.calttel;
	if(e.popup != null)
	{
		return;
	}
	kwiz_ig_LocateToottip(e);
}

function kwiz_ig_HideTooltip(toEl)
{
	var e = document.body.calttel;
	if(e == null)
		return;
	if(e.popup == null) 
		return;
	if(e.popup.document.body.focused == true)
		return;
	e.popup.hide();
	e.popup = null;
}
function kwiz_ig_TooltipOnUnload()
{
	var e = document.body.calttel;
	e.popup = null;
}

function kwiz_ig_LocateToottip(e)
{
	try
	{
		var iX = -10;
		var iY = 18;
		var imgGallCell = kwiz_ig_FindGalleryImageCell(e);

		var oPopup = window.createPopup();
		e.popup = oPopup;
		var popupBody = oPopup.document.body;
		var popupDocument = oPopup.document;
		popupBody.onload = null;
		popupDocument.createStyleSheet();
		
		for (var s = 0; s < document.styleSheets.length; s++) 
		{
			for (var r = 0; r < document.styleSheets[s].rules.length; r++) 
			{
				if (document.styleSheets[s].rules[r].selectorText.indexOf('.kwiz-ig-tooltip_') == 0)
                	popupDocument.styleSheets[0].addRule(document.styleSheets[s].rules[r].selectorText, document.styleSheets[s].rules[r].style.cssText);
			}
			kwiz_ig_isStyleSheetCreated = true;
		}
		popupBody.className = 'kwiz-ig-tooltip_body';
		popupBody.parentmouseout = kwiz_ig_HideTooltip;
		popupBody.onclick = function(){kwiz_ig_ItemClick(e);};
		popupBody.onmouseover = function(){this.ownerDocument.body.focused = true;};
		popupBody.onmouseleave = function(){this.ownerDocument.body.focused = false;this.parentmouseout();};
		oPopup.document.body.innerHTML = imgGallCell.all["tooltipcontent"].innerHTML;
		oPopup.show(0, 0, 0, 0, e);
		var nRealWidth	= popupBody.scrollWidth + popupBody.offsetWidth - popupBody.clientWidth;
		var nRealHeight = popupBody.scrollHeight + popupBody.offsetHeight - popupBody.clientHeight;
		oPopup.hide();
		nRealWidth = nRealWidth < 150 ? 150 : nRealWidth;
		nRealHeight = nRealHeight < 150 ? 150 : nRealHeight;
		popupBody.nrealwidth = nRealWidth;
		popupBody.nrealheight = nRealHeight;
		popupBody.onload = function(){kwiz_ig_TooltipOnload(e);};
		oPopup.show(iX, iY, nRealWidth, nRealHeight, e);
	}
	catch(e)
	{
	}
}
function kwiz_ig_TooltipOnload(elm)
{
	var nRealWidth	= document.body.scrollWidth + document.body.offsetWidth - document.body.clientWidth;
	var nRealHeight = document.body.scrollHeight + document.body.offsetHeight - document.body.clientHeight;
	if((nRealWidth > document.body.nrealwidth) || (nRealHeight > document.body.nrealheight))
	{
	    window.resizeTo(nRealWidth, nRealHeight);
	}
}
function kwiz_ig_GoToPage(elm, ind, mainFormId)
{
    var mainForm = mainFormId != null ? document.all[mainFormId] : kwiz_ig_FindParentMainForm(elm);
    var hiddenId = "";
    var dataId = "";
    if(mainForm.parentElement.kwiz_ig_paging == null)
    {
		hiddenId = mainForm.parentElement.kwiz_ig_paging = mainForm.kwiz_ig_paging;
		dataId = mainForm.parentElement.kwiz_ig_data = mainForm.kwiz_ig_data;
    }
    else
    {
	    hiddenId = mainForm.parentElement.kwiz_ig_paging;
		dataId = mainForm.parentElement.kwiz_ig_data;
    }
    document.all[hiddenId].value = ind;
    var url = mainForm.kwiz_ig_asyncurl + "?isrender=true&pagingdata=" + ind + "&showgallerytype=" + mainForm.kwiz_ig_showgallerytype + "&dir=" + mainForm.dir;
    kwiz_ig__RenderImageGallery(url, document.all[dataId].innerHTML, mainForm.parentElement);
}
function kwiz_ig_SetPagingActiveLink()
{
    try
    {
        var cont = document.all["pagingContaner"];
        if(cont.length != null)
        {
            for(var i = 0; i < cont.length; i++)
            {
                kwiz_ig_SetPagingActiveLinkProperties(cont[i]);
            }
        }
        else
        {
            kwiz_ig_SetPagingActiveLinkProperties(cont);
        }
    }
    catch(e)
    {
    }
}
function kwiz_ig_SetPagingActiveLinkProperties(cont)
{
    var as = cont.all.tags("a");
    for(var i = 0; i < as.length; i++)
    {
        if(as[i].outerHTML.indexOf("javascript:void(0)") > 0)
        {
            as[i].attributes.removeNamedItem("href");
            var namedItem = document.createAttribute("nohref");
            namedItem.value = "true";
            as[i].attributes.setNamedItem(namedItem);    
            as[i].click();
            return;
        }
    }
}
function kwiz_ig__RenderImageGallery(url, data, parent)
{
	parent.isasync = true;
	
	this.Parent = parent;
	this.Url = url;
	this.Data = data;
	this.HttpRequest = null;
	this.LastReadyStatus = -1;
/*	var headObj = parent.all["navigationHeaderButonsDiv"];
	if(headObj == null)
		parent.innerHTML = "<table border='0' class='ms-cal-header' cellspacing='15' cellpadding='15' width='100%' height='100%'><tr><td nowrap valign='middle'><img src='" + imageUrl + "'></td></tr></table>";
	else
	{
		headObj.style.height = headObj.offsetHeight;
		headObj.style.width = headObj.offsetWidth;
		headObj.innerHTML = "<img src='" + imageUrl + "'>";
	}*/
	kwiz_ig_SendHttpRequest(this.Url, this.Data);
	function kwiz_ig_CreateXMLHttpRequestObject()
	{
		try
		{
			var httpRequest = null;
			if( window.ActiveXObject )  // IE
			{
				var MSXML_XMLHTTP_PROGIDS = new Array(
								'Microsoft.XMLHTTP',
								'MSXML2.XMLHTTP.5.0',
								'MSXML2.XMLHTTP.4.0',
								'MSXML2.XMLHTTP.3.0',
								'MSXML2.XMLHTTP'
								);
				for (var i=0; i < MSXML_XMLHTTP_PROGIDS.length; i++)
				{
					httpRequest = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
					if( httpRequest != null )
					{
						break;
					}
				}
			}
			else if( window.XMLHttpRequest ) // // Mozilla, Safari,...
			{
				var httpRequest = new XMLHttpRequest();
				httpRequest.overrideMimeType('text/xml');
			}
			if(!httpRequest)
			{            
				alert("Failed to create the XmlHttpRequest Object");
			}  
			return httpRequest;
		}
		catch(e)
		{
		}
	}

	function kwiz_ig_ReadyStateChanged()
	{
		var XMLHTTPREQUEST_COMPLETE = 4;
		var XMLHTTPREQUEST_OK = 200;
		var XMLHTTPREQUEST_REDIRECT = 302;
		var XMLEXPIRATION = 288;             
		var currentState = null;        
		var httpCode = null;
	    
		try
		{
			currentState = this.HttpRequest.readyState;           
		}
		catch(e)
		{
			//Handle exception here             
		}
	                
		try
		{        
			if ((currentState == 0 || currentState == XMLHTTPREQUEST_COMPLETE) && this._lastReadyStatus != XMLHTTPREQUEST_COMPLETE) 
			{                        
				try
				{
					httpCode = this.HttpRequest.status;                
				}
				catch(e)
				{                
					httpCode = 377;                
				}
	                      
				this.LastReadyStatus = currentState;    
	            
				if (httpCode == XMLHTTPREQUEST_OK)
				{   
					var res = this.HttpRequest.responseText;
					res = res.substring(res.indexOf("<!-- Start ImageGallery Container"), res.indexOf("<!-- End ImageGallery Container"));
					parent.innerHTML =  res;
					kwiz_ig_SetPagingActiveLinkProperties(parent.all["pagingContaner"]);
				}            
				else if (currentState != 0)
				{   
					alert("The operation failed with HTTP code " + httpCode);
				}
			}
			else
			{
				this.LastReadyStatus = currentState;   
			}        
		}
		catch(e)
		{        
			//Handle exception here		
		}
	}

	function kwiz_ig_SendHttpRequest(url, data)
	{   
		try
		{		   				
			this.HttpRequest = kwiz_ig_CreateXMLHttpRequestObject();
			this.HttpRequest.open("POST", url, true);
			this.HttpRequest.onreadystatechange = kwiz_ig_ReadyStateChanged;
			this.HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.HttpRequest.send(data);
		}
		catch(e)   
		{
			alert("Failed to send XmlHttpRequest");
		}
	}
}

try
{
    WPSC.RegisterForEvent('urn:schemas-microsoft-com:dhtml', 'onload', kwiz_ig_OnLoadPage);
}
catch(e)
{
}

