function kwiz_ig_OnLoadPage()
{
    kwiz_ig_SetPagingActiveLink();
}

function  kwiz_ig_FindParentMainForm(elm)
{
    var el = elm.parentNode;
    while(el != null)
    {
        if((el.id.indexOf("_mainForm_") < 0) && (el.id.indexOf("_mainForm") > 0))
            return el;
        el = el.parentNode;
    }
    return null;
}
function  kwiz_ig_FindGalleryImageCell(elm)
{
    var el = elm.parentNode;
    while(el != null)
    {
        if(el.id == "galleryImageCell")
            return el;
        el = el.parentNode;
    }
    return null;
}
function  kwiz_ig_ItemMouseOver(elm)
{
    var mouseovertype = elm.getAttribute("mouseovertype");
	if(mouseovertype == "show_tooltip")
	{
		var imgGallCell = kwiz_ig_FindGalleryImageCell(elm);
	    var tooltiptext = null;
	    var tooltipEl = kwiz_general_GetChildByTagAndId(imgGallCell, "div" , "tooltipcontent");
	    if(tooltipEl != null)
	    {
	        tooltiptext = tooltipEl.innerHTML;
	    }
	    elm.tooltipcontent = tooltiptext.replace(/tooltipimg/gi, "img"); 
	    elm.popupclick = kwiz_ig_ItemClick;
	    kwiz_general_ItemMouseOver(elm);
	}
	if(mouseovertype == "update_presentation")
	{
	    if((elm != document.body.calttel) && (document.body.calttel != null))
		    document.body.calttel.popup = null;
	    document.body.calttel = elm;
	    window.setTimeout(function(){kwiz_ig_ShowPresentation(null);}, 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);
	kwiz_general_GetChildByTagAndId(mainForm, "img", "presentationImage").src = e.getAttribute("presentationimage");
	kwiz_general_GetChildByTagAndId(mainForm, "img", "presentationImage").realimageurl = e.getAttribute("realimageurl");
	kwiz_general_GetChildByTagAndId(mainForm, "td", "presentationProperties").innerHTML = kwiz_general_GetChildByTagAndId(imgGallCell, "div", "presentationPropertiesHiddenDiv").innerHTML;
}
function kwiz_ig_ItemClick(elm)
{
    var onclicktype = elm.getAttribute("onclicktype");
	if(onclicktype == "new_window")
	{
	    var stURL = document.location.href;
	    var ind = stURL.indexOf("?");
	    
	    if (ind > 0)
	    {
            stURL=stURL.substring(0, ind);
	    }
	    window.open(stURL + elm.getAttribute("presentationimage"));
	}
	if(onclicktype == "replace_gallery")
	{
        var mainForm = kwiz_ig_FindParentMainForm(elm);
        if(mainForm == null)
			return;
        var hiddenId = "";
        var dataId = "";
        if(mainForm.parentNode.kwiz_ig_paging == null)
        {
		    hiddenId = mainForm.parentNode.kwiz_ig_paging = mainForm.getAttribute("kwiz_ig_paging");
		    dataId = mainForm.parentNode.kwiz_ig_data = mainForm.getAttribute("kwiz_ig_data");
        }
        else
        {
	        hiddenId = mainForm.parentNode.kwiz_ig_paging;
		    dataId = mainForm.parentNode.kwiz_ig_data;
        }
        document.getElementById(hiddenId).value = elm.getAttribute("imageindex");
        var url = mainForm.getAttribute("kwiz_ig_asyncurl") + "?isrender=true&showgallerytype=presentation&imageindex=" + elm.getAttribute("imageindex") + "&dir=" + mainForm.getAttribute("dir") + "&wpq=" + mainForm.getAttribute("wpq");
        kwiz_ig__RenderImageGallery(url, document.getElementById(dataId).innerHTML, document.getElementById("WebPart" + mainForm.getAttribute("wpq")));
	}
	if(onclicktype == "under_gallery")
	{
	    kwiz_ig_ShowPresentation(elm);
	}
}
function  kwiz_ig_ItemMouseOut(elm)
{
    var mouseovertype = elm.getAttribute("mouseovertype");
	if(mouseovertype == "show_tooltip")
	{
	    kwiz_general_ItemMouseOut(elm);
	}
}


function kwiz_ig_GoToPage(elm, ind, mainFormId, sWPQ)
{
    var mainForm = mainFormId != null ? document.getElementById(mainFormId) : kwiz_ig_FindParentMainForm(elm);
    var hiddenId = "";
    var dataId = "";
    if(mainForm.parentNode.kwiz_ig_paging == null)
    {
		hiddenId = mainForm.parentNode.kwiz_ig_paging = mainForm.getAttribute("kwiz_ig_paging");
		dataId = mainForm.parentNode.kwiz_ig_data = mainForm.getAttribute("kwiz_ig_data");
    }
    else
    {
	    hiddenId = mainForm.parentNode.kwiz_ig_paging;
		dataId = mainForm.parentNode.kwiz_ig_data;
    }
    document.getElementById(hiddenId).value = ind;
    var url = mainForm.getAttribute("kwiz_ig_asyncurl") + "?isrender=true&pagingdata=" + ind + "&showgallerytype=" + mainForm.getAttribute("kwiz_ig_showgallerytype")+ "&dir=" + mainForm.getAttribute("dir") + "&wpq=" + sWPQ;
    kwiz_ig__RenderImageGallery(url, document.getElementById(dataId).innerHTML, document.getElementById("WebPart" + mainForm.getAttribute("wpq")));
}

function kwiz_ig_SetPagingActiveLink()
{
    try
    {
        var cont = document.getElementById("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.getElementsByTagName("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;
	
	Parent = parent;
	Url = url;
	Data = data;
	HttpRequest = null;
	LastReadyStatus = -1;
	kwiz_ig_SendHttpRequest(Url, Data);
	function kwiz_ig_CreateXMLHttpRequestObject()
	{
		try
		{
			var httpRequest = null;
			if( window.XMLHttpRequest ) 
			{
				var httpRequest = new XMLHttpRequest();
			} 
			else
			{
			    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;
					    }
				    }
			    }
			}
			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 = HttpRequest.readyState;           
		}
		catch(e)
		{
			//Handle exception here             
		}
	                
		try
		{        
			if ((currentState == 0 || currentState == XMLHTTPREQUEST_COMPLETE) && LastReadyStatus != XMLHTTPREQUEST_COMPLETE) 
			{                        
				try
				{
					httpCode = HttpRequest.status;                
				}
				catch(e)
				{                
					httpCode = 377;                
				}
	                      
				LastReadyStatus = currentState;    
	            
				if (httpCode == XMLHTTPREQUEST_OK)
				{   
					var res = HttpRequest.responseText;
					res = res.substring(res.indexOf("<!-- Start ImageGallery Container"), res.indexOf("<!-- End ImageGallery Container"));
					parent.innerHTML =  res;
					kwiz_ig_SetPagingActiveLinkProperties(kwiz_general_GetChildByTagAndId(parent, "table", "pagingContaner"));
				}            
				else if (currentState != 0)
				{   
					alert("The operation failed with HTTP code " + httpCode);
				}
			}
			else
			{
				LastReadyStatus = currentState;   
			}        
		}
		catch(e)
		{        
			//Handle exception here		
		}
	}

	function kwiz_ig_SendHttpRequest(url, data)
	{   
		try
		{		   				
			HttpRequest = kwiz_ig_CreateXMLHttpRequestObject();
			HttpRequest.open("POST", url, true);
			HttpRequest.onreadystatechange = kwiz_ig_ReadyStateChanged;
			HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			HttpRequest.send(data);
		}
		catch(e)   
		{
			alert("Failed to send XmlHttpRequest");
		}
	}
}

try
{
    WPSC.RegisterForEvent('urn:schemas-microsoft-com:dhtml', 'onload', kwiz_ig_OnLoadPage);
}
catch(e)
{
}
