﻿function getCatalogItem(itemId)
{
    serviceProxy = new myshoes.dk.Shoehorn();
    
    serviceProxy.set_defaultSucceededCallback(SucceededCallback);
    serviceProxy.set_defaultFailedCallback(FailedCallback);
    
    serviceProxy.getCatalogItemValues(itemId);
    
    var brandId = window.document.getElementById('ctl00_MainPlaceHolder_brandId');
    
    // show ajax loader
    var suggestionOne = window.document.getElementById('suggestionOne');
    var suggestionTwo = window.document.getElementById('suggestionTwo');
    var hrefOne = '<img src="images/ajax-loader-suggestions.gif" border="0" />';
    var hrefTwo = '<img src="images/ajax-loader-suggestions.gif" border="0" />';    
    suggestionOne.innerHTML = hrefOne;
    suggestionTwo.innerHTML = hrefTwo;

    
    brandId.value = itemId;
    setTimeout('getRandomSuggestions(\'' + itemId + '\')', 500);
}

function SucceededCallback(result)
{    
    var shoedescription = window.document.getElementById('shoedescription');
    var brandContent = window.document.getElementById('brandContent');
    var bigImage = window.document.getElementById('bigImage');
    var priceContent = window.document.getElementById('priceContent');
    var sizeContent = window.document.getElementById('sizeContent');
    var colorContent = window.document.getElementById('colorContent');
    var materialContent = window.document.getElementById('materialContent');
    var bigImagPopupContent = window.document.getElementById('bigImagPopupContent');
    var bigImagePopupText = window.document.getElementById('bigImagePopupText');
    var sectionId = window.document.getElementById('ctl00_MainPlaceHolder_sectionId');
    var subSectionId = window.document.getElementById('ctl00_MainPlaceHolder_subSectionId');
    
    brandContent.innerHTML = result[0];
    shoedescription.innerHTML = result[1];
    bigImagePopupText.innerHTML = result[1];
    bigImage.src = result[2];
    
    if(result[4] != '0')
        priceContent.innerHTML = '<a style="text-decoration: line-through; color: Red">' + result[3] + '</a><a>&nbsp;' + result[4] + '.-</a>';
    else
        priceContent.innerHTML = '<a>' + result[3] + '.-</a>';
        
    sizeContent.innerHTML = result[5];
    colorContent.innerHTML = result[6];
    materialContent.innerHTML = result[7];
    
    if(result[8] == null)
        bigImagPopupContent.src = '/images/spacer.gif';
    else
        bigImagPopupContent.src = result[8];
        
    sectionId.value = result[9];
    subSectionId.value = result[10];
}

// Callback function invoked when a call to 
// the  service methods fails.
function FailedCallback(error, userContext, methodName) 
{
    alert(error.get_message());
}

function getRandomSuggestions(catalogId)
{
    var sectionId = window.document.getElementById('ctl00_MainPlaceHolder_sectionId');

    serviceProxy = new myshoes.dk.Shoehorn();
    
    serviceProxy.set_defaultSucceededCallback(SuggestionSucceededCallback);
    serviceProxy.set_defaultFailedCallback(SuggestionFailedCallback);
    
    if(sectionId.value.length > 0)
        serviceProxy.getRandomThumbnails(sectionId.value, catalogId, 2);   
    else
    {
        var suggestionOne = window.document.getElementById('suggestionOne');
        var suggestionTwo = window.document.getElementById('suggestionTwo');
        suggestionOne.innerHTML = '';
        suggestionTwo.innerHTML = '';
    } 
    
}

function SuggestionSucceededCallback(result)
{    
    var suggestionOne = window.document.getElementById('suggestionOne');
    var suggestionTwo = window.document.getElementById('suggestionTwo');
    
    var hrefOne = '<a href="javascript:getCatalogItem(\'' + result[0] + '\');"><img src="' + result[1] + '" border="0" /></a>';
    var hrefTwo = '<a href="javascript:getCatalogItem(\'' + result[2] + '\');"><img src="' + result[3] + '" border="0" /></a>';
    
    
    if(result.length == 4)
    {
        suggestionOne.innerHTML = hrefOne;
        suggestionTwo.innerHTML = hrefTwo;
    }
    else
        suggestionOne.innerHTML = hrefOne;

}

// Callback function invoked when a call to 
// the  service methods fails.
function SuggestionFailedCallback(error, userContext, methodName) 
{
    alert(error.get_message());
}