﻿
var moreDetailsButtonTitle = "Flere detaljer";
var lessDetailsButtonTitle = "Luk detaljer";

// This function calls the Web service method and 
// passes the event callback function.  
function DetailedProductViewRequested(value, elementIds)
{            
    Danskenergi.WebClient.layouts.danskenergi.ProductView.GetDetailedProductView(value, SucceededCallbackWithContext, FailedCallback, elementIds, null);
}

// This is the callback function called if the
// Web service succeeded. It accepts the result
// object, the user context, and the method name as
// parameters.
function SucceededCallbackWithContext(result, elementIds, methodName)
{
    var ids = elementIds.split(';');
    var detailedViewHolderId = ids[0];
    var buttonMoreDetailsId = ids[1];
    var hiddenFieldWithParametersId = ids[2];
    var sessid = ids[3];
    
    //  get row with detailed product view    	     
    var detailedViewHolder = document.getElementById(detailedViewHolderId);    
    
    //  hide row with detailed product view 
    detailedViewHolder.parentNode.style.display = "none";
    
    //  add result to inner html 
    detailedViewHolder.innerHTML = result;                
                
    //  get 'more datails' button
    var buttonMoreDetails = document.getElementById(buttonMoreDetailsId);
    
    //  chandge onclick event handler (to be able expand next time)        
    buttonMoreDetails.onclick = function()
    {
        HideDetailedProductView(detailedViewHolderId, buttonMoreDetailsId, hiddenFieldWithParametersId, sessid);
    };    
    
    //  chandge button title            
    buttonMoreDetails.innerHTML = lessDetailsButtonTitle;     
    
    //  animation
    Expand(detailedViewHolderId);
    UpdateExpandedProductListCookieValue($(buttonMoreDetails).prev('p').text(), true);
}

// This is the callback function called if the
// Web service failed. It accepts the error object
// as a parameter.
function FailedCallback(error, detailedViewHolderId)
{               
}

function HideDetailedProductView(detailedViewHolderId, buttonMoreDetailsId, hiddenFieldWithParametersId, sessid)
{
    //  animation
    Collapse(detailedViewHolderId);

    //  get row with detailed product view
    var detailedViewHolder = document.getElementById(detailedViewHolderId);    
    
    //  clear inner html
    //detailedViewHolder.innerHTML = "";    
    //  hide row with detailed product view
    //  detailedViewHolder.parentNode.style.display = "none";
        
    //  get find parameters
    var hiddenfield = document.getElementById(hiddenFieldWithParametersId);    
    
    //  get 'more datails' button
    var buttonMoreDetails = document.getElementById(buttonMoreDetailsId);    
    
    //  chandge onclick event handler (to be able expand next time)
    buttonMoreDetails.onclick = function() {
    DetailedProductViewRequested(hiddenfield.innerHTML, detailedViewHolderId + ';' + buttonMoreDetailsId + ';' + hiddenFieldWithParametersId + ';' + sessid);
    };    
    
    //  chandge button title 
    buttonMoreDetails.innerHTML = moreDetailsButtonTitle;
    UpdateExpandedProductListCookieValue($(buttonMoreDetails).prev('p').text(), false);
}

function Expand(tableCellId)
{    
    $('#' + tableCellId).parent().css('display', '');
    $('#' + tableCellId).children('.moreInfo').css('display', 'none');
    $('#' + tableCellId).children('.moreInfo').slideDown('normal');
}

function Collapse(tableCellId)
{
 //   $('#' + tableCellId).children('.moreInfo').css('display', 'block');
    $('#' + tableCellId).children('.moreInfo').slideUp(function(){
        $('#' + tableCellId).text('');
    });
    
}

function UpdateExpandedProductListCookieValue(detailsId, expand)
{
    var existingCookieValue = readCookie(searchUtility.expandedProductsParameter);
    if(!existingCookieValue)
    {
        existingCookieValue ='';
    }

    if (expand)
    {
        if (existingCookieValue.indexOf(detailsId) == -1)
        {
            existingCookieValue += (detailsId + '|');
        }
    }
    else
    {
        existingCookieValue = existingCookieValue.replace((detailsId + '|'), '');
    }
    
    createCookie(searchUtility.expandedProductsParameter, existingCookieValue, 20);
}

function ExpandProductDetailsByUrlOrCookies()
{
    var expandProductsFromUrl = $.jqURL.get(searchUtility.expandedProductsParameter);
    var expandProductsFromCookie = readCookie(searchUtility.expandedProductsParameter);
    var expandProducts;
    if(expandProductsFromUrl && !expandProductsFromCookie)
    {
        createCookie(searchUtility.expandedProductsParameter, expandProductsFromUrl, 20);
        expandProducts = expandProductsFromUrl;
    }
    else if (expandProductsFromCookie)
    {
        expandProducts = expandProductsFromCookie;
    }
    
    ExpandProductDetailsByValue(expandProducts);
}

function ExpandProductDetailsByValue(expandProductIds)
{
    if(!expandProductIds)
    {
        return;
    }
    
    var productDetailIds = expandProductIds.split('|');
    for(var i = 0; i < productDetailIds.length; i++)
    {
        if(productDetailIds[i] && productDetailIds[i] != '')
        {
            var paragraphWithId = document.getElementById("p" + productDetailIds[i]);
            if(paragraphWithId)                
            {
                $(paragraphWithId).next('a').click();
            }
        }
    }
}