﻿// *******************************
// Spice Library
// 1.0.0
// (C) 2008 DG2
// *******************************

// *******************************
// slBackground
// *******************************
function slBackground(obj, strGUID) {
    var popObj = $get('SpiceBackgroundPH');
    if (popObj.style.display == 'none') {
        $cover(obj, popObj);
        popObj.style.display = 'block';
        $request('SpiceBackgroundImagesPH', '/apps/api/8/', 'ID='+strGUID);
    }

    else {
        popObj.style.display = 'none';
    }

}

// *******************************
// slCommitChanges
// *******************************
function slCommitChanges() {
    
    var strSpiceBackgroundColor = $get('DgMAX_pageContent_UcSpice_SpicePageBackgroundColor').style.backgroundColor; 
    var strSpiceBackgroundSelectedURL = $get('SpiceBackgroundSelectedURL').value;
    var strSpiceBackgroundPosition = $get('SpiceBackgroundPosition').value;
    var strSpiceBackgroundScroll = $get('SpiceBackgroundScroll').value;
    var strSpiceBackgroundRepeat = $get('SpiceBackgroundRepeat').value;
    var strSpiceBasicColor = $get('DgMAX_pageContent_UcSpice_SpiceBasicColor').style.backgroundColor;
    var strSpiceSecondColor = $get('DgMAX_pageContent_UcSpice_SpiceSecondColor').style.backgroundColor;
    var strSpiceBorderColor = $get('DgMAX_pageContent_UcSpice_SpiceBorderColor').style.backgroundColor;
    var strSpiceTextFont = $get('DgMAX_pageContent_UcSpice_SpiceTextFont').value;
    var strSpiceTextSize = $get('SpiceTextSize').value;
    var strSpiceTextColor = $get('DgMAX_pageContent_UcSpice_SpiceTextColor').style.backgroundColor;
    var strSpiceLinksColor = $get('DgMAX_pageContent_UcSpice_SpiceLinksColor').style.backgroundColor;
    var strSpiceTitleFont = $get('DgMAX_pageContent_UcSpice_SpiceTitleFont').value;
    var strSpiceTitleColor = $get('DgMAX_pageContent_UcSpice_SpiceTitleColor').style.backgroundColor;
    var strSpiceTitleBackgroundColor = $get('DgMAX_pageContent_UcSpice_SpiceTitleBackgroundColor').style.backgroundColor;
    var strSpiceBoxBackgroundColor = $get('DgMAX_pageContent_UcSpice_SpiceBoxBackgroundColor').style.backgroundColor;
    
    var strData = 'C='+Math.random()+'&BackgroundColor='+Url.encode(strSpiceBackgroundColor)+'&BackgroundPosition='+Url.encode(strSpiceBackgroundPosition)+'&BackgroundScroll='+Url.encode(strSpiceBackgroundScroll)+'&BackgroundRepeat='+Url.encode(strSpiceBackgroundRepeat)+'&BackgroundUrl='+Url.encode(strSpiceBackgroundSelectedURL)+'&BasicColor='+Url.encode(strSpiceBasicColor)+'&SecondColor='+Url.encode(strSpiceSecondColor)+'&BorderColor='+Url.encode(strSpiceBorderColor)+'&TextFont='+Url.encode(strSpiceTextFont)+'&TextColor='+Url.encode(strSpiceTextColor)+'&LinksColor='+Url.encode(strSpiceLinksColor)+'&TitleFont='+Url.encode(strSpiceTitleFont)+'&TitleColor='+Url.encode(strSpiceTitleColor)+'&TitleBackgroundColor='+Url.encode(strSpiceTitleBackgroundColor)+'&BoxBackgroundColor='+Url.encode(strSpiceBoxBackgroundColor)+'&TextFontSize='+Url.encode(strSpiceTextSize);
    $get('DgMAX_mShellCustom').href = '/apps/api/profile/spice/template?'+strData

}

// *******************************
// slClear
// *******************************
function slClear() {

    $get('SpiceTemplateGUID').value = '';
    $get('DgMAX_pageContent_UcSpice_SpiceName').value = '';
    $get('DgMAX_pageContent_UcSpice_SpicePageBackgroundColor').style.backgroundColor = '';
    $get('SpiceBackgroundSelectedURL').value = '';
    $get('SpiceBackgroundPosition').options[0].selected=true;
    $get('SpiceBackgroundScroll').options[0].selected=true;
    $get('SpiceBackgroundRepeat').options[0].selected=true;
    $get('DgMAX_pageContent_UcSpice_SpiceBasicColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceSecondColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceBorderColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceTextFont').options[0].selected=true;
    $get('SpiceTextSize').options[0].selected=true;
    $get('DgMAX_pageContent_UcSpice_SpiceTextColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceLinksColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceTitleFont').options[0].selected=true;
    $get('DgMAX_pageContent_UcSpice_SpiceTitleColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceTitleBackgroundColor').style.backgroundColor = '';
    $get('DgMAX_pageContent_UcSpice_SpiceBoxBackgroundColor').style.backgroundColor = '';
    slCommitChanges();
    
}

// *******************************
// slCheckRGB
// *******************************
function slCheckRGB(str) {
    
    if (str.toLowerCase() == 'transparent') { 
        return '#TRANSP';
    }
    
    if (str.length == 0) { return '' }
    if ($left(str,1) == '#') { return str }
    
    str = str.replace(/rgb/,'');
    str = str.replace(/\(/,'');
    str = str.replace(/\)/,'');
    str = str.replace(/ /,'');
    arr = str.split(',');
    return '#'+slToHex(arr[0])+slToHex(arr[1])+slToHex(arr[2]);
}

function slToHex(N) {
    if (N==null) return "00";
    N=parseInt(N); if (N==0 || isNaN(N)) return "00";
    N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
    return "0123456789ABCDEF".charAt((N-N%16)/16)
         + "0123456789ABCDEF".charAt(N%16);
}

// *******************************
// slSave
// *******************************
function slSave(strGUID) {

    if ($get('DgMAX_pageContent_UcSpice_SpiceName').value == '') { alert('Geef een naam van het sjabloon op.'); return }
    
    var strSpiceName = $get('DgMAX_pageContent_UcSpice_SpiceName').value;
    var strSpiceBackgroundColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpicePageBackgroundColor').style.backgroundColor);   
    var strSpiceBackgroundSelectedURL = $get('SpiceBackgroundSelectedURL').value;
    var strSpiceBackgroundPosition = $get('SpiceBackgroundPosition').value;
    var strSpiceBackgroundScroll = $get('SpiceBackgroundScroll').value;
    var strSpiceBackgroundRepeat = $get('SpiceBackgroundRepeat').value;
    var strSpiceBasicColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceBasicColor').style.backgroundColor);
    var strSpiceSecondColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceSecondColor').style.backgroundColor);
    var strSpiceBorderColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceBorderColor').style.backgroundColor);
    var strSpiceTextFont = $get('DgMAX_pageContent_UcSpice_SpiceTextFont').value;
    var strSpiceTextSize = $get('SpiceTextSize').value;
    var strSpiceTextColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceTextColor').style.backgroundColor);
    var strSpiceLinksColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceLinksColor').style.backgroundColor);
    var strSpiceTitleFont = $get('DgMAX_pageContent_UcSpice_SpiceTitleFont').value;
    var strSpiceTitleColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceTitleColor').style.backgroundColor);
    var strSpiceTitleBackgroundColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceTitleBackgroundColor').style.backgroundColor);
    var strSpiceBoxBackgroundColor = slCheckRGB($get('DgMAX_pageContent_UcSpice_SpiceBoxBackgroundColor').style.backgroundColor);      
    var strPostData = 'TemplateGUID='+$get('SpiceTemplateGUID').value+'&Name='+Url.encode(strSpiceName)+'&ID='+strGUID+'&Activate='+$get('Activate').checked+'&BackgroundColor='+Url.encode(strSpiceBackgroundColor)+'&BackgroundPosition='+Url.encode(strSpiceBackgroundPosition)+'&BackgroundScroll='+Url.encode(strSpiceBackgroundScroll)+'&BackgroundRepeat='+Url.encode(strSpiceBackgroundRepeat)+'&BackgroundUrl='+Url.encode(strSpiceBackgroundSelectedURL)+'&BasicColor='+Url.encode(strSpiceBasicColor)+'&SecondColor='+Url.encode(strSpiceSecondColor)+'&BorderColor='+Url.encode(strSpiceBorderColor)+'&TextFont='+Url.encode(strSpiceTextFont)+'&TextColor='+Url.encode(strSpiceTextColor)+'&LinksColor='+Url.encode(strSpiceLinksColor)+'&TitleFont='+Url.encode(strSpiceTitleFont)+'&TitleColor='+Url.encode(strSpiceTitleColor)+'&TitleBackgroundColor='+Url.encode(strSpiceTitleBackgroundColor)+'&BoxBackgroundColor='+Url.encode(strSpiceBoxBackgroundColor)+'&TextFontSize='+Url.encode(strSpiceTextSize);
    
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: '/apps/api/profile/spice/save/',
            data: strPostData,
            success: function(data) {
                $get('SpiceTemplateGUID').value  = data;
                slToggleManage(strGUID);
            }
        }); 
    });
}

// *******************************
// slDelete
// *******************************
function slDelete(strGUID, strTemplateGUID) {

    var cDel = confirm('Weet je zeker dat je deze lay-out wilt verwijderen?');
	
	if (cDel) {
	    $get('t'+strTemplateGUID).style.display='none';
	    $request('DoAlert','/apps/api/profile/spice/delete/', 'ID='+strGUID+'&TemplateGUID='+strTemplateGUID);
	    slClear();
	}
	
    else {
        return false;
    }
}

// *******************************
// slApply
// *******************************
function slApply(strGUID, strTemplateGUID, blnSite) {

    if (blnSite == 0) {
        $request('pSpiceManage','/apps/api/profile/spice/apply/', 'ID='+strGUID+'&TemplateGUID='+strTemplateGUID);
    }
    
    else {
        $request('pSpiceManage','/apps/api/profile/spice/apply/', 'ID='+strGUID+'&TemplateGUID='+strTemplateGUID+'&Site=1');
    }
}

// *******************************
// slDeApply
// *******************************
function slDeApply(strGUID, strTemplateGUID, blnSite) {

    if (blnSite == 0) {
        $request('pSpiceManage','/apps/api/profile/spice/deapply/', 'ID='+strGUID+'&TemplateGUID='+strTemplateGUID);
    }
    
    else {
        $request('pSpiceManage','/apps/api/profile/spice/deapply/', 'ID='+strGUID+'&TemplateGUID='+strTemplateGUID+'&Site=1');
    }
}

// *******************************
// slToggleManage
// *******************************
function slToggleManage(strGUID) {
    $get('pSpiceEdit').style.display = 'none';
    $get('pSpiceEditSave').style.display = 'none';
    $get('pSpiceManage').style.display = 'block';
    
    $request('pSpiceManage','/apps/api/profile/spice/manageview/', 'ID='+strGUID);
}

// *******************************
// slToggleEdit
// *******************************
function slToggleEdit() {
    $get('pSpiceEdit').style.display = 'block';
    $get('pSpiceEditSave').style.display = 'block';
    $get('pSpiceManage').style.display = 'none';
    $get('DgMAX_pageContent_UcSpice_pSpiceManageA').className = 'baColor';
}

// *******************************
// slEdit
// *******************************
function slEdit(strGUID, strName, strBackgroundColor, strBackgroundURL, strBackgroundPosition, strBackgroundScroll, strBackgroundRepeat, strBasicColor, strSecondColor, strBorderColor, strTextFont, strTextColor, strLinksColor, strTitleFont, strTitleColor, strTitleBackgroundColor, strBoxBackgroundColor, strTextFontSize) {

    slClear();
    slToggleEdit();
    $get('SpiceTemplateGUID').value = strGUID;
    $get('DgMAX_pageContent_UcSpice_SpiceName').value = strName;
    if ($left(strBackgroundColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpicePageBackgroundColor').style.backgroundColor = strBackgroundColor; }
    $get('SpiceBackgroundSelectedURL').value = strBackgroundURL;

    var sObj = $get('SpiceBackgroundPosition');
    for(var i=0; i<sObj.length; i++) { 
        if(sObj.options[i].value==strBackgroundPosition) { 
            sObj.options[i].selected=true; 
        } 
    }
    
    sObj = $get('SpiceBackgroundScroll');
    for(var i=0; i<sObj.length; i++) { 
        if(sObj.options[i].value==strBackgroundScroll) { 
            sObj.options[i].selected=true; 
        } 
    }
    
    sObj = $get('SpiceBackgroundRepeat');
    for(var i=0; i<sObj.length; i++) { 
        if(sObj.options[i].value==strBackgroundRepeat) { 
            sObj.options[i].selected=true; 
        } 
    }

    if ($left(strBasicColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceBasicColor').style.backgroundColor = strBasicColor; }
    if ($left(strSecondColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceSecondColor').style.backgroundColor = strSecondColor; }
    if ($left(strBorderColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceBorderColor').style.backgroundColor = strBorderColor; }

    sObj = $get('DgMAX_pageContent_UcSpice_SpiceTextFont');
    for(var i=0; i<sObj.length; i++) { 
        if(sObj.options[i].value==strTextFont) { 
            sObj.options[i].selected=true; 
        }
    }
    
    sObj = $get('SpiceTextSize');
    for(var i=0; i<sObj.length; i++) { 
        if(sObj.options[i].value==strTextFontSize) { 
            sObj.options[i].selected=true; 
        } 
    }
    
    if ($left(strTextColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceTextColor').style.backgroundColor = strTextColor; } 
    if ($left(strLinksColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceLinksColor').style.backgroundColor = strLinksColor; }

    sObj = $get('DgMAX_pageContent_UcSpice_SpiceTitleFont');
    for(var i=0; i<sObj.length; i++) { 
        if(sObj.options[i].value==strTitleFont) { 
            sObj.options[i].selected=true; 
        } 
    }

    if ($left(strTitleColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceTitleColor').style.backgroundColor = strTitleColor; }
    if ($left(strTitleBackgroundColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceTitleBackgroundColor').style.backgroundColor = strTitleBackgroundColor; }
    if ($left(strBoxBackgroundColor,1) != ' ') { $get('DgMAX_pageContent_UcSpice_SpiceBoxBackgroundColor').style.backgroundColor = strBoxBackgroundColor; }
    setTimeout('slCommitChanges()',500);
}