var bActive = 0;
var aLoaded = Array();

function display_flash() {
    $('#main_nav').children().css('display', 'none');
    $('#main_nav').css('width', '110px');
    $('#main_nav').append('<ul id="ex_nav"><li><a href="#" onmouseover="return expandNav()">'+aData['navigation']+'</a></li></ul>');

    $('#header_flash').css('display', 'block');
    $('#header_flash').css('position', 'absolute');
    $('#header_flash').css('z-index', '-1');
    $('#header_flash').css('top', '20px');
    $('#header_flash').css('background-color', 'black');
    if (parseInt($('#header').position().left == 0)) {
         $('#header_flash').css('left', (parseInt($('#header').position().left)+1)+'px');
    } else {
         $('#header_flash').css('left', (parseInt(document.getElementById('header').offsetLeft)+1)+'px');
    }
    
    if (typeof hasRightVersion != 'undefined' && hasRightVersion) 
        $('#header').css('background-image', 'none');

    $('#headline').css('display', 'none');
    $('#more').css('display', 'none');
    
}

function addNavigationHooks(sId) {
    $(sId+' li a').mouseover(function() {
        if ($(this).parent().parent().parent().parent().attr('tagName') != 'UL') {
            hideAllSubNavs(sId);
            //$(this).parent().children('ul').fadeIn('fast');
            $(this).parent().children('ul').css('display', 'block');
        }
        bActive = 1;
    });  
    
    $(sId+' li').mouseover(function() {
        bActive = 1;
    });

    $(sId+' li').mouseout(function() {
        window.setTimeout('hideAllSubNavs("'+sId+'")', 5000);
        bActive = 0;
    });

    $(sId+' li li a').mouseover(function() {
        var oExp= /^([a-z]+)(\d+)$/;
        oExp.exec(this.rel);

        if (RegExp.$1 == 'cat') {
            aData['#product_cat_idcat'] = RegExp.$2;
            aData['#product_teaser_idcat'] = 0;
            return showCats(parseInt($(this).parent().offset().left)+235, parseInt($(this).parent().offset().top));
        } else if (RegExp.$1 == 'prod'){
            aData['#product_teaser_idcat'] = RegExp.$2;
            aData['#product_cat_idcat'] = 0;
            return showProducts(parseInt($(this).parent().offset().left)+235, parseInt($(this).parent().offset().top));
        }
    });
    
    $(sId+' li li a').click(function() {
        var oExp= /^([a-z]+)(\d+)$/;
        oExp.exec(this.rel);
        
        /*
        if (RegExp.$1 == 'prod'){
            return false;
        }
        */
        
    });
}

$(document).ready(function() {
    addNavigationHooks('#main_nav');
    addNavigationHooks('#top_nav');
    
    $('div.product_layer_2').css('display', 'none');
    $('div.product_layer_3').css('display', 'none');
    
    $('#layer_1').click(function(){
         if ($('div.product_layer_1').css('display') == 'none') {
            if ($('div.product_layer_2').css('display') == 'block') {
                $('div.product_layer_2').fadeOut('fast', function() {
                    $('div.product_layer_1').fadeIn('fast');
                });
            }
        
            if ($('div.product_layer_3').css('display') == 'block') {
                $('div.product_layer_3').fadeOut('fast', function() {
                    $('div.product_layer_1').fadeIn('fast');
                });
            }
        }
        return false;
    });
    
    $('#layer_2').click(function(){
        if ($('div.product_layer_2').css('display') == 'none') {
            if ($('div.product_layer_1').css('display') == 'block') {
                $('div.product_layer_1').fadeOut('fast', function() {
                    $('div.product_layer_2').fadeIn('fast');
                });
            }
        
            if ($('div.product_layer_3').css('display') == 'block') {
                $('div.product_layer_3').fadeOut('fast', function() {
                    $('div.product_layer_2').fadeIn('fast');
                });
            }
        }
        return false;
    });
    
    $('#layer_3').click(function(){
       if ($('div.product_layer_3').css('display') == 'none') {
            if ($('div.product_layer_1').css('display') == 'block') {
                $('div.product_layer_1').fadeOut('fast', function() {
                    $('div.product_layer_3').fadeIn('fast');
                });
            }
        
            if ($('div.product_layer_2').css('display') == 'block') {
                $('div.product_layer_2').fadeOut('fast', function() {
                    $('div.product_layer_3').fadeIn('fast');
                });
            }
        }
        return false;
    });
});

function hideAllSubNavs(sId, force) {
    if (bActive == 0 || force) {
        /*$(sId+' ul li ul').fadeOut('fast');
        
        $('#product_teaser').fadeOut('fast', function() {
            $('#product_teaser').remove();
        });
        $('#cat_teaser').fadeOut('fast', function() {
            $('#cat_teaser').remove();
        });*/
        $('#product_teaser').remove();
        $('#cat_teaser').remove();
        $(sId+' ul li ul').css('display', 'none');
    }
}

function expandNav () {
    $('#ex_nav').fadeOut('normal', function() {
        $('#ex_nav').remove();
        
        $("#main_nav").animate({width: "960px"}, 1000, null, function() {
            $('#main_nav').children().fadeIn();    
        });
    });
    
    $('#headline').fadeIn();
    $('#more').fadeIn();
    
    return false;
}

function showImage(oClicked) {
    if (typeof oClicked != "object") {
        oClicked = $('a[href="'+oClicked+'"]');
    }

    var oPreloadImage = new Image();
    $(oPreloadImage).attr('src', $(oClicked).attr('href'));
    
    var iLoaded = 0;
    for (var i = 0; i < aLoaded.length; ++i) {
        if (aLoaded[i] == oPreloadImage.src) {
            iLoaded = 1;
        }
    }
    
    var aSrc = Array()
    aSrc[0] = oPreloadImage.src;
    aSrc[1] = $(oClicked).prev('a').attr('href');
    aSrc[2] = $(oClicked).next('a').attr('href');
    
    if (iLoaded == 0) {
        $(oPreloadImage).load(function () {
            displayLayer("#image_layer", oPreloadImage.height, oPreloadImage.width, -10 , 0, 0, aSrc, 1);
        });
        aLoaded.push(oPreloadImage.src);
    } else {
        displayLayer("#image_layer", oPreloadImage.height, oPreloadImage.width, -10, 0, 0, aSrc, 1);
    }

    return false;
}

function showContact() {
    displayLayer('#contact', 560, 670, 30, 0, 0, '', 1);
    
    return false;
}

function showImpressum() {
    displayLayer('#impressum', 600, 500, 30, 0, 0, '', 1);
    
    return false;
}

function showProducts(iX, iY) {
    displayLayer('#product_teaser', 460, 540, -10, iX, iY, '', 0);
    return false;
}

function showCats(iX, iY) {
    displayLayer('#cat_teaser', 232, 232, -10, iX, iY, '', 0);
    return false;
}

function showGroup () {
    displayLayer('#group', 110, 390, 0, $('#footer_top').offset().left, $('#footer_top').offset().top+30, '', 0);
    return false;
}

function submitContactForm() {
    var sContactType = $('#contact_form select[name=type]').attr('value');
    var sSalutation = $('#contact_form select[name=salutation]').attr('value');
    var sContact = $('#contact_form input[name=contact]').attr('value');
    var sCompany = $('#contact_form input[name=company]').attr('value');
    var sAdditional = $('#contact_form input[name=additional]').attr('value');
    var sStreet = $('#contact_form input[name=street]').attr('value');
    var sCity = $('#contact_form input[name=city]').attr('value');
    var sCountry = $('#contact_form input[name=country]').attr('value');
    var sPhone = $('#contact_form input[name=phone]').attr('value');
    var sEmail = $('#contact_form input[name=email]').attr('value');
    var sBranch = $('#contact_form select[name=branch]').val();
    var sManufacturer = $('#contact_form select[name=manufacturer]').attr('value');
    var sProductType = $('#contact_form select[name=product_type]').attr('value');
    var sMessage = $('#contact_form textarea[name=message]').val();
    var sNewsletter = $('#contact_form input[name=newsletter]').attr('checked')?1:0;
    var iCheckPrivacy = $('#contact_form input[name=privacy]').attr('checked')?1:0;
    
    if (sContact == '' || sCompany == '' || sStreet == '' || sCity == '' || sCountry == '' || 
        sPhone == '' || sEmail == '' || sBranch == '' || iCheckPrivacy == 0) {
        if(aData['lang'] == 95){
            alert('Please fill out the required fields.');
        } else {
            alert('Bitte füllen Sie alle Pflichtfelder aus.');
        }
    } else {
        if(aData['lang'] == 95){
            $('#contact_form_submit').attr('value', 'Please wait');
        } else {
            $('#contact_form_submit').attr('value', 'Bitte warten');
        }
        $('#contact_form_submit').attr('disabled', 'disabled');
        
        $.get('front_content.php', { 
            idart : aData['ajax'], 
            send_mail: 'true',
            contact_type: sContactType,
            salutation: sSalutation,
            contact_person: sContact,
            company: sCompany,
            addional: sAdditional,
            street: sStreet,
            city: sCity,
            country: sCountry,
            phone: sPhone,
            email: sEmail,
            branch: sBranch,
            manufacturer: sManufacturer,
            product_type: sProductType,
            message: sMessage,
            newsletter: sNewsletter},

            function(data){
                if(aData['lang'] == 95){
                    alert('Thank you for your enquiry.');
                } else {
                    alert('Vielen Dank für Ihre Mitteilung.');
                }
                closeImage();
            }
        );
        
    }

    return false;
}

/**
  * Function for displaying layer
  * 
  * @param sId string - HTML ID of layer
  * @param sLayerName string - name of html template
  * @param iHeight integer - height of layer
  * @param iWidth integer - width of layer
  * @param iOffset integer - offset for close Image at the right top side
  * @param iX integer - position of windwo from left (default center, optional)
  * @param iY integer - position of windwo from top (default center, optional)
  * @param sSrc string - name of image in lightbox
  */
function displayLayer (sId, iHeight, iWidth, iOffset, iX, iY, aSrc, bClose) { 
    $(sId).remove();
 
    $(sId).css('display', 'none');
    $.get('layer/layer.html', { },
          function(data){ 
            
            sId_html = sId.replace('#', '');
            data = data.replace('<div', '<div id="'+sId_html+'"');

            $('body').append(data);
            
            if (iHeight > 0) {
                $(sId).css('height', iHeight+'px');
            }
            $(sId).css('width', iWidth+'px');

            if (iX) {
                $(sId).css('left', iX+'px');
            } else {
                $(sId).css('left', ($.clientCoords().w-iWidth)/2+'px');
            }

            if (iY) {
                $(sId).css('top', iY+'px');
            } else {
                $(sId).css('top', ($.clientCoords().s+(($.clientCoords().h-iHeight)/2))+'px');
            }
            
            if (bClose == 0) {
                 $(sId+' img.close').remove();
                 $(sId).click(function () {closeImage();});
            } else {  
                $(sId+' img.close').css('left', (iWidth+iOffset)+'px');
                $(sId+' img.close').css('top', '-10px');
                $(sId+' img.close').css('z-index', 100000);
            }
            
            if (aSrc) {
                $(sId).css('background-image', 'url("'+aSrc[0]+'")');
                if (aSrc[1]) {
                   $(sId).append('<a class="img_but_p" style="display:block" href="javascript:" onclick="return showImage(\''+aSrc[1]+'\')">Previous</a>"') 
                }
                if (aSrc[2]) {
                   $(sId).append('<a class="img_but_n" style="display:block" href="javascript:" onclick="return showImage(\''+aSrc[2]+'\')">Next</a>"') 
                }
                //$(sId).append('<img src="'+sSrc+'" alt="" style="cursor:pointer;" onclick="closeImage()"/>');
            }
            
            if (aData[sId] != 0){
                $.get('front_content.php', {fk_idart:aData[sId], idart : aData['ajax'], lang: aData['lang'], fk_product:aData['#product_teaser_idcat'], fk_cat:aData['#product_cat_idcat']},
                    function(data){ 
                        if (data != '' && data.length > 1) {
                            $(sId + ' div[class="product"]').remove();
                            $(sId + ' div[class="product empty"]').remove();
                            $(sId + ' ul').remove();
                            $(sId).append(data);
                            if (sId == '#product_teaser' || sId == '#cat_teaser') {
                                $(sId).css('display', 'block');
                            } else {
                                $(sId).fadeIn('fast');
                            }
                            
                            if ($('#contact_form_submit')) {
                                $('#contact_form_submit').unbind();
                                $('#contact_form_submit').click(function() {
                                    return submitContactForm();
                                });
                            }
                        }
                    }
                );
            } else {
                if (sId == '#product_teaser' || sId == '#cat_teaser') {
                    $(sId).css('display', 'block');
                } else {
                    $(sId).fadeIn('fast');
                }
            }

            
            if (sId == '#product_teaser' || sId == '#cat_teaser') {
                $('div.product').click(function(){closeImage(); hideAllSubNavs('#main_nav', 1); return false;});
                
                $(sId + 'li a').click(function() {
                    hideAllSubNavs('#main_nav', 1);
                });
                
                $(sId).css('height', 'auto');
                
                $(sId).mouseover(function() {
                    bActive = 1;
                });
                
                $(sId).mouseout(function() {
                    window.setTimeout('hideAllSubNavs("#main_nav")', 5000);
                    bActive = 0;
                });
            }
          }
    );
}

$.clientCoords = function(){
    if(window.innerHeight || window.innerWidth){
        return {w:window.innerWidth, h:window.innerHeight, s:window.pageYOffset}
    }
    return {
        w:document.documentElement.clientWidth,
        h:document.documentElement.clientHeight,
        s:document.documentElement.scrollTop
    }
}


function closeImage() {
    $('#image_layer').fadeOut('fast', function() {$('#image_layer').remove();});
    $('#contact').fadeOut('fast', function() {$('#contact').remove();});
    $('#product_teaser').remove();
    //$('#product_teaser').fadeOut('fast', function() {$('#product_teaser').remove();});
    $('#impressum').fadeOut('fast', function() {$('#impressum').remove();});
    $('#group').fadeOut('fast', function() {$('#group').remove();});
    return false;
}
