﻿// -------------------------------------------------------------------------| $(document).ready
$(document).ready(function()
{

    with($.theknot.uw.view.photoAlbumViewer)
    {
        setupClickEvents();
        getLoadingImageUrl();
    }

});

// -------------------------------------------------------------------------| Namespace definitions.
$.theknot.uw.view.photoAlbumViewer = function(){};

// -------------------------------------------------------------------------| Body
$.extend($.theknot.uw.view.photoAlbumViewer,
{
    // -----------------------------------------------------| Properties
    regexExtractLoadingImageUrl : /^(url\(){1}([\''\""])?([a-z:\/\.\-_]*)([\''\""])?(\)){1}/i,
    loadingImageUrl : '',
    
    fieldSelectors : 
    {
        imgThumb : 'img[id$=_imgPhotoAlbumImageThumb]',
        linkThumb : 'a[id$=_hlnkShowImage]',
        modalContainer : 'div#popupModal',
        allModalContainers : 'div[id$=opupModal]',
        modalTable : 'table#tblPhotoAlbumPopupModal',
        modalImage : 'img.main_image',
        modalImageDesc : 'p#pImgDesc',
        modalCounter : 'div.counter',
        modalPrevLink : 'a.prev',
        modalNextLink : 'a.next',
        modalCloseButton : 'a#hlnkCloseModal',
        LoadingImageSelector : 'div.loading:first',
        allLoadingImages : 'div[id$=LoadingImg]'
    },
    
    constants :
    {
        SiteId : $.theknot.uw.view.commonGuestviewElements.Site.Id,
        UserId : $.theknot.uw.view.commonGuestviewElements.Site.UserId,
        
        PhotoAlbumViewerService : '/view/webservices/PhotoAlbumViewer.ashx',
        ServiceParmsFormat : 'sid={0}&uid={1}&aid={2}&aiid={3}'
        
    },
    
    // -----------------------------------------------------| setupPrevAndNextIEFix()
    getLoadingImageUrl : function()
    {
        if($(this.fieldSelectors.LoadingImageSelector).length>0)
        {
            var arrImgUrl = $(this.fieldSelectors.LoadingImageSelector).css('background-image').match(this.regexExtractLoadingImageUrl);
            if(arrImgUrl != null && arrImgUrl.length == 6)
            {
                this.loadingImageUrl = arrImgUrl[3];
            }
        }
    },
    
    setupClickEvents : function()
    {
        with($.theknot.uw.view.photoAlbumViewer.fieldSelectors)
        {
            // Thumbnail Click
            $(linkThumb).click(function()
            {
                var AlbumId = $(this).attr('albumId');
                var AlbumImageId = $(this).attr('albumImageId');
                $.theknot.uw.view.photoAlbumViewer.ShowImage(this, AlbumId, AlbumImageId);
			});
			$(linkThumb).focus(function(){ this.blur(); });
			
			// Close Button Click
			$(modalContainer).find(modalCloseButton).click(function()
			{
			    //$(modalContainer).find(modalImage).attr('src', '/images/load-ani.gif');
			    with($(modalContainer).find(modalImage))
			    {
			        if($.theknot.uw.view.photoAlbumViewer.loadingImageUrl != '')
                    {
                        attr('src', $.theknot.uw.view.photoAlbumViewer.loadingImageUrl);
                    }
                    else
                    {
                        attr('src', $.theknot.uw.view.photoAlbumViewer.spacerImage);
                    }
                }
			    $(modalContainer).hide();
			});
        }
    },
    
    ShowImage: function(obj, AlbumId, AlbumImageId)
	{
	    with($.theknot.uw.view.photoAlbumViewer.fieldSelectors)
        {
            //$(modalContainer).find(modalImage).attr('src', '/images/load-ani.gif');
            with($(modalContainer).find(modalImage))
		    {
		        if($.theknot.uw.view.photoAlbumViewer.loadingImageUrl != '')
                {
                    attr('src', $.theknot.uw.view.photoAlbumViewer.loadingImageUrl);
                }
                else
                {
                    attr('src', $.theknot.uw.view.photoAlbumViewer.spacerImage);
                }
            }
	        //$(allModalContainers).hide();
	        // Hide the Flickr modal if it is visible.
	        $('div#divFlickrPopupModal').hide();
	    }
		this._GetImageInfo(AlbumId, AlbumImageId);
	},
	
	_GetImageInfo: function(AlbumId, AlbumImageId, successCallback, errorCallback, completeCallback)
	{
	    var parms = ''; 
	    with($.theknot.uw.view.photoAlbumViewer.constants)
	    {
	        parms = $.theknot.formatStr(ServiceParmsFormat, SiteId, UserId, AlbumId, AlbumImageId);
	    }
	    
		$.ajax(
		{
			type: 'GET',
			url: $.theknot.uw.view.photoAlbumViewer.constants.PhotoAlbumViewerService,
			data: parms,
			dataType: 'json',
			cache: false,
			success: function(data, textStatus)
			{
			    with($.theknot.uw.view.photoAlbumViewer.fieldSelectors)
			    {
				    var pop = $(modalContainer);
				    var popImg = $(modalContainer).find(modalImage);
				    var popImgDesc = $(modalContainer).find(modalImageDesc);
				    var popNext = $(modalContainer).find(modalNextLink);
				    var popPrev = $(modalContainer).find(modalPrevLink);
				    var popCounter = $(modalContainer).find(modalCounter);
    				
    				var descText =  unescape(data.description).replace(/\n{3,}/g, '\n').replace(/\n/g, '<br />');
    				popImgDesc.html(descText);
    				
				    popNext.unbind('click');
				    popNext.click(function()
				    {
					    $.theknot.uw.view.photoAlbumViewer.ShowImage(this, AlbumId, data.nextImageId);
				    });
				    popNext.focus(function(){ this.blur(); });
    				
				    popPrev.unbind('click');
				    popPrev.click(function()
				    {
					    $.theknot.uw.view.photoAlbumViewer.ShowImage(this, AlbumId, data.prevImageId);
				    });
				    popPrev.focus(function(){ this.blur(); });
    				
				    popCounter.text($.theknot.formatStr('Photo {0} of {1}', ((data.imageIndex*1)+1), data.totalImages));
				    
				    if($.browser.msie)
                    {
                        pop.find('table:first').css('top', document.documentElement.scrollTop - 10);
                    }
                    else
                    {
                        pop.find('table:first').css('top', window.scrollY - 10);
                    }
                    pop.find('table:first').css('left', '50%');
				    
				    pop.show();
				    //alert($.theknot.uw.view.photoAlbumViewer.loadingImageUrl);
				    popImg[0].src = data.imagePath;
				}
				
				if($.isFunction(successCallback))
				{
				    successCallback(data, textStatus);
				}
			},
			
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				if($.isFunction(errorCallback))
				{
				    errorCallback(XMLHttpRequest, textStatus, errorThrown);
				}
			},
			
			complete : function(XMLHttpRequest, textStatus)
			{
			   if($.isFunction(completeCallback))
				{
				    completeCallback(XMLHttpRequest, textStatus);
				}
			}
		});
		
	}
    
});
