window. onload          = onLoad ;
//window.onbeforeunload   = ConfirmExit ;

var gallery ;
var gallery_cart ;

var editGalleryScrollPos ;
var editDebug ;

var selectedProductId ;
var selectedProductImage ;

var  zoomed ;
var  newRq ;

var g_bodyPanelHeight ;

var  TrinityHistory    = new Array() ;

var  mouseOverNavTree   = false ;


   
function beginRequest(sender, args) 
{
	var panel = document.getElementById( 'pagepanel' ) ;
	panel.style.cursor = "progress" ;
	
}

function endRequest(sender, args) 
{
	var panel = document.getElementById( 'pagepanel' ) ;
	panel.style.cursor = "default" ;

}

function TrinityRq( msg )
{
    __doPostBack('__Page', msg );
    return ;   
    
	var btn = document.getElementById( 'btnCallBack' ) ;
	if( btn == null )
		alert( 'no button' ) ;
                    
	var txt = document.getElementById( 'msgTxt' ) ;
	if( txt == null )
		alert( 'no text' ) ;
                   
	txt.value= msg ;

    //we do not want to put action urls in the history as it can cause problems
    //eg the Send action on the email page
    var action = msg.search(/action=/i) ;
    
    if( action == -1 )      //ie cannot find action= in msg so put it in history
    {
        TrinityHistory.push( msg ) ;
    }
    
    newRq = true ;

    //this seems to be the only way to get the partial postbacks to work properly but it requires
    //EnableEventValidation=false in Page directives
    Sys.WebForms.PageRequestManager.getInstance()._doPostBack('btnCallBack', msg ) ;
    //javascript:__doPostBack('__Page', msg ) ;
    
	//btn.click() ;
                
}

function ConfirmExit()
{
    return "Please use the Back button located at the top of the Trinity Diamonds main menu for navigating within this website." ;
}

function GoBack()
{
    
    var idex = document.getElementById( "idexPanel" ) ;    
    
    if( idex != null )
    {
        window.history.back() ;
        return ;
    }
    
    if( TrinityHistory.length > 1 )
    {
        //remove the current request
        var curMsg = TrinityHistory.pop() ;

        //get the prev request
        var prevMsg = TrinityHistory.pop() ;
                    
        if( prevMsg != null )
        {    
            TrinityRq( prevMsg ) ;
        }
    }
}


function GetCurSelectedImg()
{
    var path; 
    /*
        if a new request has been made, the server will have set the selected image, in which case
        we just use whatever the src is of the image. Otherwise showImg() will have set the 
        selectedProductImage field, so we can go ahead and use that
    */
  
    try
    {
        if( newRq == true )
        {
            //the server will have set the selected image
            var img = document.getElementById( "imgLarge" ) ;    
            var src = img.src ;
            var pathStart   = src.indexOf( 'path', 0 ) ;
            var pathEnd     = src.indexOf( '&', pathStart ) ;
            path        = src.substring( pathStart, pathEnd ) ;
        }
        else    
        {
            path = "path=" + selectedProductImage ;
        }
    }
    catch( e )
    {
        alert( "GetCurSelectedImg - " + e.message ) ;
        throw e ;
    }
        
    return path ;
}

function SetCurrentlySelectedImage( url )
{
    var img = document.getElementById( "imgLarge" ) ;
    
    if( img != null )
    {
        img.src = url ;
    }
}

function showImg(imgName, imgId, imgHeight )
{
    newRq = false ;
        
    try
    {         	             
        selectedProductId       = imgId ;
        selectedProductImage    = imgName ;

        //set this so we know it was selected by the user
        //SetSelectedImg( "" ) ; //imgName ) ;
        
	    imgOn = ("" + imgName);

       var editZoomed = document.getElementById( "editZoomed" ) ;		   
        
        if( editZoomed.value     == "true" ) //zoomed == "true"  )
        {
            ShowZoomedImage() ;
        }
	            	
	    //var  select = "select," + imgId + "," + imgName ;
	    var  select = "url=Gallery,cmd=select,imgId=" + selectedProductId + ",imgName=" + imgName ;
	    
        TrinityRq( select ) ;
        
    }
    catch( e )
    {
        alert( "showImg - " + e.message ) ;
    }
    
}

function ZoomIn()
{  

        
    //TODOO this is a hack
    //for some reason ZoomIn() was getting called when you zoom in on an image, the say select CUfflinksByCOuntry
    //and then hit a country eg England. VERY VERY bizarre , but this seems to fix it
    img = document.getElementById( "imgLarge" ) ;	
    
    if( img == null )
    {
        return ;
    }
    // END HACK
    
    try
    {
	    var bodyPanel       = document.getElementById( "bodypanel" ) ;		
	    var footerPanel       = document.getElementById( "xfooterpanel" ) ;		
	    var mainmenupanel  = document.getElementById( "mainmenupanel" ) ;			    
	    
	    ToggleVisibility( "leftcontent" ) ;	
	    ToggleVisibility( "rightcontent" ) ;	
	    
	    if( bodyPanel == null || mainmenupanel == null )
	    {
	        alert( "Zoom in - no body or menu panel" ) ;
	    }
	    
        mainmenupanel.style.display = 'none' ;        
	    
	    var zoomImgPanel  = document.getElementById( "zoomImgPanel" ) ;		

        //TODO this seems to cause the image to disappear when we use the new PageManager
        g_bodyPanelHeight       = bodyPanel.style.height  ;
        
	    bodyPanel.style.height  = '500px' ;

	    if( zoomImgPanel == null )
	    {
	        alert( "Zoom in - no body panel" ) ;
	    }
	        
        ShowZoomedImage() ;

	    ToggleVisibility( "zoomImgPanel" ) ;
	            
	    //zoomed  = "true"  ;
	    //original way	 
	    //var  select = "url=TrinityTest.PageContent_Zoom,cmd=zoom,imgId=" + selectedProductId + ",imgName=" + selectedProductImage  ;
        //__doPostBack('__Page', select )	;
    }
    catch( e )
    {
        alert( "ZoomIn - " + e.message ) ;
        
        throw e ;
    }

}

function ShowZoomedImage()
{  
    try
    { 
        var zoomImage  = document.getElementById( "imgzoom" ) ;		    

        if( zoomImage == null )
        {
            alert( "ShowZoomedImage - no imgZoom" ) ;
        }
            
        //bodyPanelImage.src = "ThumbGen.ashx?path=" + imgName + "&height=600&width=700" ;   
        //we are just going to extract the path from the src of the currently selected image
        var imgSrc  = GetCurSelectedImg() ;
        zoomImage.src = "ThumbGen.ashx?" + imgSrc + "&height=500&width=700" ;
       
        //bodypanel.style.height  = bodyPanelImage.height ;
        
        zoomed  = "true"  ;
        
        var editZoomed = document.getElementById( "editZoomed" ) ;		   
        editZoomed.value = "true" ;
    }
    catch( e )
    {
        alert( "ShowZoomedImage - " + e.message ) ;
        
        throw e;
    }
}

function ZoomOut()
{   
    try
    {
	    var zoomImgPanel  = document.getElementById( "zoomImgPanel" ) ;		

        ToggleVisibility( "zoomImgPanel" ) ;

	    var bodyPanel       = document.getElementById( "bodypanel" ) ;		
	    var mainmenupanel  = document.getElementById( "mainmenupanel" ) ;			    
	    
	    
	    if( bodyPanel == null || mainmenupanel == null )
	    {
	        alert( "Zoom in - no body or menu panel" ) ;
	    }
	        
	    ToggleVisibility( "leftcontent" ) ;	
	    ToggleVisibility( "rightcontent" ) ;	

	    bodyPanel.style.height  = g_bodyPanelHeight ;

        ToggleVisibility( "mainmenupanel" ) ;
        	    	   
	    zoomed  = "false" ;
        
        var editZoomed = document.getElementById( "editZoomed" ) ;		   
        editZoomed.value = "false" ;
    }
    catch( e )
    {
        alert( "ZoomOut - " + e.message ) ;
        
        throw e ;
    }
    	   
}

function ToggleVisibility ( elementId )
{
    var target = document.getElementById( elementId );
    
    if (target.style.display == "" || target.style.display == "block")
    {
        target.style.display = "none";
    }
    else
    {
        target.style.display = "block";
    }	
}
function UpdateQty()
{
    try
    {
        var qty = document.getElementById( "editQty" ).value ;

        //ajax 1
	    //var  update = "update," + qty + ","  + selectedProductId + "," + selectedProductImage ;   
	    //ajax 2
	    var  update = "url=Gallery,cmd=update,qty=" + qty + ",prod="  + selectedProductId + ",imgName=" + selectedProductImage ;
	    	    
        __doPostBack('__Page', update )	;
    }
    catch( e )
    {
        alert( "Exception - " + e.message ) ;
    }
    
}
   
function ReadGalleryScroll( gall )
{    
	editGalleryScrollPos.value	= gall.scrollLeft;
}


function onLoad()
{   	
    try
    {		
        if ('Navigator' == navigator.appName) 
            document.forms[0].reset();

        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
        
	    editGalleryScrollPos   = document.getElementById( "editGalleryScrollPos" ) ;
    	
	    gallery		= document.getElementById( "idMainGallery" ) ;    
	    	
    	if( gallery != null && editGalleryScrollPos != null )
        {	   
	        gallery.scrollLeft		= editGalleryScrollPos.value ;
            gallery.style.visibility    = "visible" ;
    	}
    	
	    zoomed      = "false" ;
	    
	    newRq       = true ;
	    	   
        TrinityHistory.push( "url=Basic,class=File,file=Home,title=Welcome,querytype=list,querylist=main,pic=pendants/diamond/technaoro/pnd_dmnd_technaoro_snowflake_large.jpg" ) ;
    }
    catch( e ) 
    {
       alert( "OnLoad Exception - " + e.message ) ;
    }
}

function UpdateGallery(response, context) 
{
    return ;
	var prodTitle       = document.getElementById( "prodTitle" ) ;
	var prodDescription = document.getElementById( "prodDescription" ) ;
	
	var items      = response.split(".") ;
	
	prodTitle.innerHTML	        = items[0] ;
	prodDescription.innerHTML	= items[1] ;	
}

//not used currently
function RequestUpdateGallery() 
{
    var title = document.getElementById("editSelectedGalleryImg");
    title.innerHTML = 'Loading ...';
    WebForm_DoCallback('rightcontent', 0, UpdateGallery, null, null, false);
}




