var m_aPreferences = new Array();
var m_aSettings = new Array();
var m_sSystemFilter = 'All';
var m_sOperatorFilter = '';

$(function (){
    preLoadImages();
    swidth = 560;

    $.ajaxSetup({
        async: false,
        dataType: 'json',
        url: '/_assets/data.php'
    });
    
    loadSettings();
    getScrollText();
    
    $('.TopMenu, .ClickHere').click(function() {
        var p_sDivId = $(this).attr('id');
        var p_bReloadProfiles = false;
        
        switch($(this).attr('class')){
            case 'TopMenu':
                m_sSystemFilter = p_sDivId.substr(7);
                m_bCreatelayout = true;
                p_bReloadProfiles = true;
                break;
            case 'ClickHere':
                if(p_sDivId == 'ConsultantLink' || p_sDivId == 'WebPortalLink') {
                    _gaq.push(['_trackEvent', 'External', 'www.crystalerotiek.nl']);
                    return true;
                }
                
                if(p_sDivId.substr(0, 8) == 'LeftMenu'){
                    m_sSystemFilter = p_sDivId.substr(8);
                    m_bCreatelayout = true;
                    p_bReloadProfiles = true;
                }
                break;
        }
        
        _gaq.push(['_trackEvent', 'Filters', m_sSystemFilter]);
        
        if(p_bReloadProfiles) {
            startProfiles();
        }
        
        return false;
    });
})

function preLoadImages(){
    $.preLoadImages('/images/layout/available.png', '/images/layout/available.png');
    $.preLoadImages('/images/layout/bg.png', '/images/layout/bg.png');
    $.preLoadImages('/images/layout/bg_CallMe.png', '/images/layout/bg_CallMe.png');
    $.preLoadImages('/images/layout/bg_content.png', '/images/layout/bg_content.png');
    $.preLoadImages('/images/layout/bg_footer.png', '/images/layout/bg_footer.png');
    $.preLoadImages('/images/layout/bg_ItemsLeft.png', '/images/layout/bg_ItemsLeft.png');
    $.preLoadImages('/images/layout/bg_logo.png', '/images/layout/bg_logo.png');
    $.preLoadImages('/images/layout/bg_navigationBar.png', '/images/layout/bg_navigationBar.png');
    $.preLoadImages('/images/layout/bg_nederland.png', '/images/layout/bg_nederland.png');
    $.preLoadImages('/images/layout/bg_operator.png', '/images/layout/bg_operator.png');
    $.preLoadImages('/images/layout/bg_pagegnumber.png', '/images/layout/bg_pagegnumber.png');
    $.preLoadImages('/images/layout/bg_Steps.png', '/images/layout/bg_Steps.png');
    $.preLoadImages('/images/layout/bg_thema.png', '/images/layout/bg_thema.png');
    $.preLoadImages('/images/layout/bg_webportal.png', '/images/layout/bg_webportal.png');
    $.preLoadImages('/images/layout/BHIcon.png', '/images/layout/BHIcon.png');
    $.preLoadImages('/images/layout/BrownSugar.png', '/images/layout/BrownSugar.png');
    $.preLoadImages('/images/layout/bruinesuiker.png', '/images/layout/bruinesuiker.png');
    $.preLoadImages('/images/layout/busy.png', '/images/layout/busy.png');
    $.preLoadImages('/images/layout/Frame.png', '/images/layout/Frame.png');
    $.preLoadImages('/images/layout/LadyFrame.png', '/images/layout/LadyFrame.png');
    $.preLoadImages('/images/layout/no-pic.png', '/images/layout/no-pic.png');
    $.preLoadImages('/images/layout/original_painting.png', '/images/layout/original_painting.png');
    $.preLoadImages('/images/layout/PauseIcon.png', '/images/layout/PauseIcon.png');
    $.preLoadImages('/images/layout/PauseIcon_i.png', '/images/layout/PauseIcon_i.png');
    $.preLoadImages('/images/layout/Phone.png', '/images/layout/Phone.png');
    $.preLoadImages('/images/layout/Photo.png', '/images/layout/Photo.png');
    $.preLoadImages('/images/layout/PhotoFrame.png', '/images/layout/PhotoFrame.png');
    $.preLoadImages('/images/layout/PhotoFrameBG.png', '/images/layout/PhotoFrameBG.png');
    $.preLoadImages('/images/layout/PlayIcon.png', '/images/layout/PlayIcon.png');
    $.preLoadImages('/images/layout/PlayIcon_i.png', '/images/layout/PlayIcon_i.png');
    $.preLoadImages('/images/layout/ProfileInfo.png', '/images/layout/ProfileInfo.png');
    $.preLoadImages('/images/layout/SearchIcon.png', '/images/layout/SearchIcon.png');
    $.preLoadImages('/images/layout/smallStar.png', '/images/layout/smallStar.png');
    $.preLoadImages('/images/layout/Star.png', '/images/layout/Star.png');
    $.preLoadImages('/images/layout/unavailable.png', '/images/layout/unavailable.png');
}
    
function loadSettings(){
    $.ajax({
        data: ({
            action: 'settings'
        }),
        error: function(event, request, settings){
            var t = '';
            for(var p in event){
                t += event[p] + '\n';
            }
            alert('Event: ' + t + '\nRequest: ' + request + '\nSettings: ' + settings)
        },
        success: function (data){
            if(data.result == 'ok'){
                m_aSettings = data.data;
                
                document.title = m_aSettings.title;
                
                $('#StepNumber1').html(m_aSettings.stepNumber1);
                $('#StepDescription1').html(m_aSettings.stepDescription1);
                $('#StepNumber2').html(m_aSettings.stepNumber2);
                $('#StepDescription2').html(m_aSettings.stepDescription2);
                $('#StepNumber3').html(m_aSettings.stepNumber3);
                $('#StepDescription3').html(m_aSettings.stepDescription3);
                $('#CallIsSeeContainer').html(m_aSettings.CallIsSee);
                $('#Slogan').html(m_aSettings.Slogan);
                $('.PageLabel').html(m_aSettings.PageLabel);
                $('#CallMe').html(m_aSettings.CallMe);
                //$('#CallMePhonenumber').html(m_aSettings.phonenumber);
                $('#WebPortalText').html(m_aSettings.WebPortalText);
                $('#ConsultantText').html(m_aSettings.ConsultantText);
                $('#LabelPhone').html(m_aSettings.LabelPhone + ':');
                $('#TextPhone').html(m_aSettings.TextPhone);
                $('#LabelEmail').html(m_aSettings.LabelEmail + ':');
                $('#TextEmail').html(m_aSettings.TextEmail);
                $('#LabelWebcam').html(m_aSettings.LabelWebcam + ':');
                $('#TextWebcam').html(m_aSettings.TextWebcam);
                $('#LabelSMS').html(m_aSettings.LabelSMS + ':');
                $('#TextSMS').html(m_aSettings.TextSMS);
                $('#LabelPhoto').html(m_aSettings.LabelPhoto + ':');
                $('#TextPhoto').html(m_aSettings.TextPhoto);
                $('#LabelVideo').html(m_aSettings.LabelVideo + ':');
                $('#TextVideo').html(m_aSettings.TextVideo);
                $('.ClickHere').html(m_aSettings.ClickHere);
                $('#WebPortalLink').html(m_aSettings.ClickHere);
                $('#TopMenuAll').html(m_aSettings.LabelAll);
                $('#TopMenuEmail').html(m_aSettings.LabelEmail);
                $('#TopMenuPhone').html(m_aSettings.LabelPhone);
                $('#TopMenuPhoto').html(m_aSettings.LabelPhoto);
                $('#TopMenuSms').html(m_aSettings.LabelSMS);
                $('#TopMenuWebcam').html(m_aSettings.LabelWebcam);
                $('#TopMenuVideo').html(m_aSettings.LabelVideo);
                
                m_iCategory = parseInt(m_aSettings.categoryToLoad);
            }
        }
    })
}

function getScrollText(){
    $.ajax({
        url: '/_assets/scrollers.php',
        data: ({
            domain: location.hostname
        }),
        dataType: 'xml',
        error: function(event, request, settings){
            alert('Event: ' + event + '\nRequest: ' + request + '\nSettings: ' + settings)
        },
        success: function(data){
            var i = 0;
            $(data).find('item').each(function() {
                var p_sText = $(this).find('text').text();
                            
                singletext[i] = '<nobr class="tann">' + p_sText + '</nobr>';
                i++;
            });
            startScroller();
        },
        type: 'POST'
    })
}

