/**
 * author: Fadendaten michael
 */

var slider = false;
  

var highlightOptions = {
    startcolor: "#1A5199",
    endcolor: '#121312',
    duration: 0.5
}

showNavigationMenu = function(menuName){
    $(menuName).show();

    return false;
}

hideNavigationMenu = function(menuName){
    $(menuName).hide();

    return false;
}

addToCart = function(){
    $("cartSummaryRow").highlight(highlightOptions);

    $("addToCartMessage").show().highlight(highlightOptions);
}

scrollHorizontal = function(value, element, slider) {
    element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
}

alignStyleGroup = function() {
    if(!$('styleGroup')){
        return;
    }
    var width = document.viewport.getWidth();
    
    var leftSpace = (width - 1000)/2;

    var isKeylook = $('styleGroup').down('img').classNames().include("styleGroup");


    if(isKeylook){
        leftSpace -= 135; //keylook
    }
    
    width -= leftSpace;
    
    $('styleGroup').setStyle({
        width: width+"px",
        marginLeft: leftSpace+"px"
        });

    var listSpacer = 10;

    if($('boxContainer')){
        listSpacer = 410;
    }

    adjustStyleGroup(listSpacer);
}

//width is the distance which has to be substracted on the right side.
adjustStyleGroup = function(rightSpace) {
    rightSpace = rightSpace || 10

    var width = document.viewport.getWidth()/2 + 500 - rightSpace;
    
    if($('styleGroupList')){
        $('styleGroupList').setStyle({
            width: (width)+"px" ,
            overflow: 'hidden'
        });

        if ($('styleGroupList').scrollWidth <= $('styleGroupList').offsetWidth) {
            $('slider').hide();
        }else{
            $('slider').show();
        }
    }

    $('slider').setStyle({
        marginLeft: ($('styleGroupList').getWidth()/2 - 125)+"px"
    });

    createOrUpdateSlider();

}

createOrUpdateSlider = function(){

    if(slider){
        slider.dispose();
    }

    slider = new Control.Slider('handle', 'slider', {
        onSlide: function(v) {
            scrollHorizontal(v, $('styleGroupList'), slider);
        },
        onChange: function(v) {
            scrollHorizontal(v, $('styleGroupList'), slider);
        }
    });
}



prepareDetailsContainer = function(styleID){
    $$('.styleImageBox').each(function(el){
        el.up().show();
    });

    $('style_'+styleID).up().hide();

    if($('boxContainer')){
        $('boxDetailsContainer').update();
        $('boxContainer').update();
        return;
    }

    var boxContainer = new Element("td", {
        id: "boxContainer",
        style: "padding-top: 2px; padding-left: 2px; padding-right: 0px;"
    });
    var boxDetailsContainer = new Element("td", {
        id: "boxDetailsContainer",
        style: "padding-top: 2px;"
    });

    var children = $('styleGroup').down('tr').childElements();

    if(children.size() == 1){
        
        children[0].insert({
            before: boxContainer
        })
        children[0].insert({
            before: boxDetailsContainer
        })
       
    }else{
        children[0].insert({
            after: boxDetailsContainer
        })
        children[0].insert({
            after: boxContainer
        })
        
    }
    
}

hideStyle = function(styleID){
    $('styleGroupList').down("#style_"+styleID).up().hide()
}




mailTo = function(name){
    var mail = (name+"&AUnile:ch").gsub(":", ".").gsub("&AU", "@");
    location.href = ("mailto:"+mail);
}

