/*                                            _      _    __
                                         _ __| |__ _| |_ / _|___ _ _ _ __
                     B y t e s h i f t  | '_ \ / _` |  _|  _/ _ \ '_| '  \
  --------------------------------------| .__/_\__,_|\__|_| \___/_| |_|_|_|------
                                        |_|
  Copyright 2010, Byteshift.net
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

    * Redistributions of source code must retain the above copyright
      notice and this list of conditions.
    * Redistributions in binary form must reproduce the above
      copyright notice and this list of conditions in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of Byteshift.net nor the names of its
      contributors may be used to endorse or promote products derived from
      this software without specific prior written permission.

  -------------------------------------------------------------------------------
*/
try{ if( ds ){} }catch(e){ ds = {}; }

$(function(){
    ds.init();
});


ds.conf = {
    cw : 0,
    ch : 0,
    fsElements : [],
    bgIndex : 0,
    cpanelVisible : false,
    sliderVisible : false,
    dropdownVisible : false
};

ds.toggleAdmin = function(){
    $("#ds_dropdown").hide();
    ds.conf.dropdownVisible = false;
    if( ds.conf.cpanelVisible == false ){
        $("#ds_overlay").fadeIn(300, function(){
            $("#ds_overlay").fadeTo(300,0.85);
            $("#ds_adminpanel").fadeIn(300);
            ds.conf.cpanelVisible = true;
        });
    }else{
        $("#ds_overlay").fadeOut(300,function(){
            $("#ds_adminpanel").fadeOut(300);
            ds.conf.cpanelVisible = false;
        });
    }
};

/**
 * dS Framework Initialization function
**/
ds.init = function(){

    ds.setAutoTransform("ds_workspace");
    ds.setAutoTransform("ds_adminpanel");
    ds.setAutoTransform("ds_sitecontainer");
    ds.setAutoTransform("ds_sitecontainer_bg_1");
    ds.setAutoTransform("ds_sitecontainer_bg_2");
    ds.setAutoTransform("ds_overlay");

    ds.xhr("platform/cpanel.php","ltz=1",function(e){
        $("#ds_adminpanel").html(e);
    });

    shortcut.add("F9", function(){
        if( ds.dyn_conf.using_ie ){
            alert("Het CMS is niet benaderbaar in Internet Explorer.\nGebruik AUB Google Chrome of Mozilla Firefox.");
        }else{
            ds.toggleAdmin();
        }
    });
    ds.conf.cw  = document.documentElement.clientWidth;
    ds.conf.ch  = document.documentElement.clientHeight;
    if( !ds.conf.cw || !ds.conf.ch ){
        ds.conf.cw  = window.innerWidth;
        ds.conf.ch  = window.innerHeight;
    }
    l = ds.conf.fsElements.length; i = 0;
    for( i = 0; i < l; i++ ){
        el = document.getElementById(ds.conf.fsElements[i]);
        if( el != undefined ){
            ds.transformElement(ds.conf.fsElements[i], ds.conf.cw, ds.conf.ch);
        }
    }
        //$("#ds_workspace").css("overflow","hidden");
    
    
    ds.fireResizeEvent();
    $(window).resize(function(){
        ds.fireResizeEvent();
    });

    ds.xhr("site/main.php","ssid=__t", function(e){
        $("#ds_sitecontainer").html(e);
    });
}

ds.fireResizeEvent = function()
{
    ds.conf.cw  = document.documentElement.clientWidth;
    ds.conf.ch  = document.documentElement.clientHeight;
    if( !ds.conf.cw || !ds.conf.ch ){
        ds.conf.cw  = window.innerWidth;
        ds.conf.ch  = window.innerHeight;
    }
    l = ds.conf.fsElements.length; i = 0;
    if( ds.conf.cw < 1024 || ds.conf.ch < 768){
        //$("#ds_workspace").css("overflow","scroll");
    }else{
        for( i = 0; i < l; i++ ){
            el = document.getElementById(ds.conf.fsElements[i]);
            if( el != undefined ){
                ds.transformElement(ds.conf.fsElements[i], ds.conf.cw, ds.conf.ch);
            }
        }
        //$("#ds_workspace").css("overflow","hidden");
    }

}


/**
 * Simplified element transformation
**/
ds.transformElement = function( element, x, y )
{
    el = document.getElementById(element);
    if( el != undefined ){
        el.style.width = x + 'px';
        el.style.height = y + 'px';
    }
}

/**
 * Add element to full transformation list
**/
ds.setAutoTransform = function( element )
{
    if( ds.conf.fsElements.indexOf(element) == -1 ){
        l = ds.conf.fsElements.length;
        ds.conf.fsElements[l] = element;
    }
}

/**
 * Advanced synchronized XHR system (with callback)
**/
ds.xhr = function( file, param, callback )
{
    $(function(){
        $.ajax({
            type: "POST",
            url: "index.php",
            async: true,
            data: "__xml-rpc="+file+"&"+param,
            success: function(e){
                callback(e);
            }
        });
    });
}

/**
 * Switch background/ambi. (image, ambi-image)
**/
ds.wallpaper = {

    ci : 2,

    set : function( image, ambi )
    {
        if( ds.dyn_conf.using_ie ){
            $("#ds_sitecontainer_bg_1").css('background',"url('site/visual/bgi/"+ambi+"') repeat-x");
            $("#ds_bg_1").css('background',"url('site/visual/bgi/"+image+"') no-repeat");
        }else{
            oldci = ds.wallpaper.ci;
            if( ds.wallpaper.ci == 1 ){
                ds.wallpaper.ci = 2;
            }else{
                ds.wallpaper.ci = 1;
            }

            thisID = "#ds_bg_"+oldci;
            newID = "#ds_bg_"+ds.wallpaper.ci;

            thisbgId = "#ds_sitecontainer_bg_" + oldci;
            newbgId = "#ds_sitecontainer_bg_" + ds.wallpaper.ci;

            v = '<img src="site/visual/bgi/'+image+'" alt="" style="width:1024px;height:768px;" id="ds_bg_'+ds.wallpaper.ci+'_wp"/>';
            $(newID).html(v);
            $(newbgId).css('background',"url('site/visual/bgi/"+ambi+"') repeat-x");
            setTimeout(function(){
                $(newID).css('z-index','2');
                $(thisID).css('z-index','1');
                $(newbgId).css('z-index','1');
                $(thisbgId).css('z-index','0');
                $(newbgId).fadeIn(800, function(){
                    $(thisbgId).fadeOut(100);
                });
                $(newID).fadeIn(800);
                $(thisID).fadeOut(100);
            },100);
        }
    }
}

/**
 * Open content
**/
ds.openContent = function( page, menuid ){
    document.getElementById("sidemenu_zoekmerk").className = "sidemenu_item";
    $("#ds_brandmenu").hide();
    $("#ds_dropdown").hide();
    ds.conf.dropdownVisible = false;
    ds.conf.sliderVisible = false;
    ds.xhr("site/content.php","page="+page+"&menuid="+menuid, function(e){
        $(".topmenu_item").css("color","#fff");
        $(".sidemenu_item").css("color","#fff");
        $("#"+menuid).css("color","#f33");
        $("#ds_hidden").html(e);
    })
}

ds.showBootsSlider = function( brand_id, q )
{
    $("#ds_dropdown").hide();
    ds.conf.dropdownVisible = false;
    if( ds.dyn_conf.using_ie ){
        ds.xhr("site/etalage.php","brand_id="+brand_id+"&q="+q,function(e){
            $("#ds_e_content").html(e);
            $("#ds_etalage").show();
            //ds.conf.sliderVisible = true;
        });
    }else{
        ds.xhr("site/etalage.php","brand_id="+brand_id+"&q="+q,function(e){
            $("#ds_e_content").html(e)
            $("#ds_etalage").fadeIn(250);
            //ds.conf.sliderVisible = true;
        });
    }
}

ds.showProductFrame = function( article_id )
{
    $("#ds_dropdown").hide();
    $("#ds_brandmenu").hide();
    ds.conf.dropdownVisible = false;
    if( ds.dyn_conf.using_ie ){
        ds.xhr("site/product.php","article_id="+article_id,function(e){
            $("#ds_e_productView").html(e);
            $("#ds_productView").show();
        });
    }else{
        ds.xhr("site/product.php","article_id="+article_id,function(e){
            $("#ds_e_productView").html(e)
            $("#ds_productView").fadeIn(250);
        });
    }
}

ds.hideProductFrame = function()
{
    $("#ds_brandmenu").hide();
    $("#ds_dropdown").hide();
    ds.conf.dropdownVisible = false;
    if( ds.dyn_conf.using_ie ){
        $("#ds_productView").hide();
        ds.conf.sliderVisible = false;
    }else{
        $("#ds_productView").fadeOut(250);
        ds.conf.sliderVisible = false;
    }
}

ds.toggleDropdown = function()
{
    $("#ds_brandmenu").hide();
    if( !ds.conf.dropdownVisible ){
        $("#ds_dropdown").show();
        ds.conf.dropdownVisible = true;
    }else{
        $("#ds_dropdown").hide();
        ds.conf.dropdownVisible = false;
    }
}

ds.adjustBrandsList = function( special )
{
    $(function(){
        $("#ds_dropdown").hide();
        ds.xhr( "site/brands.php", "select="+special, function(e){
            $("#ds_bm_brandlist_inner").html(e);
        });
        ds.conf.dropdownVisible = false;
        if( ds.conf.sliderVisible == false ){
            document.getElementById("sidemenu_zoekmerk").className = "sidemenu_item_open";
            if( ds.dyn_conf.using_ie ){
                $("#ds_brandmenu").show();
            }else{
                $("#ds_brandmenu").fadeIn(250);
            }
            ds.conf.sliderVisible = true;
        }else{
            document.getElementById("sidemenu_zoekmerk").className = "sidemenu_item";
            if( ds.dyn_conf.using_ie ){
                $("#ds_brandmenu").hide();
            }else{
                $("#ds_brandmenu").fadeOut(250);
            }
            ds.conf.sliderVisible = false;
        }
    });
}