var categoryID=0;

function initNavigation(){
    $('.attributes').hide().children('a').click(function(event){
        event.preventDefault();
        if($('#normal').length >0){
            var gid=$(this).urlParamValue('aid');
            ajaxGetMaterialsByApplicationArea(gid);
        }else{
            var option=getCorrectHrefData(this);
            switch(option){
                case 'addMaterial':
                    ajaxGetNewMaterialPage();
                    break;
            }
        }
    });
    $('#attributes a').hover(
        function() {
            $(this).addClass('hoverEffect');
        },
        function() {
            $(this).removeClass('hoverEffect');
        }
        );
    $('#attributes a').click(
        function() {
            $('#attributes a').removeClass('selectedEffect');
            $(this).addClass('selectedEffect');
            $('#attributes a').addClass('listColor');
            $(this).removeClass('listColor');
        }
        );
    $('#naviLeft .head').click(
        function() {
            $('#attributes a').removeClass('selectedEffect');
            $('#attributes a').addClass('listColor');
        }
        );
  
    $('.head, .controlHead, #materialProperty').click(function(event){
        event.preventDefault();
        if($('a#opener').length ==0 && $('#normal').length >0){
            ajaxReloadVideos();
        }

        $('.attributes:visible').slideToggle(500);
        $(this).next('div:hidden').slideToggle(500);
        
        if($('#normal').length >0){
            ajaxGetMaterialsByCategory($(this).urlParamValue('id'));
        }else{
            if(getCorrectHrefData(this)!=""){
                var option=getCorrectHrefData(this);
                switch(option){
                    case 'addProperties':
                        ajaxGetPropertiesPage();
                        break;
                    case 'userControl':
                        ajaxGetUserListPage();
                        break;
                }
            }
        }
    });
    $('#naviLeft .head span').cycle({
        speed:5000,
        fx:'fade'
    });
    if($('#normal').length >0){
        $('a.loginRegistration').click(function(event){
            event.preventDefault();
            var option=$(this).urlParamValue('action');
            switch(option){
                case 'login':
                    ajaxGetLoginPage();
                    break;
                case 'registration':
                    ajaxGetRegistrationPage();
                    break;
            }
        });
    }
    $('#searchedit').click(function(event){
        event.preventDefault();
        var material=$(this).prev('input').val();
        if($('html').attr('lang')=='de'){
            $(this).prev('input').val('Materialcode');
        }else{
            $(this).prev('input').val('Codice materiale');
        }
        ajaxGetEditMaterialPage(material);
    });
    $('#searchterm').focus(function(){
        $(this).val("");
    }).blur(function(){
        var field=$(this);
        setTimeout(function(){
            field.val("Materialcode");
            if($('html').attr('lang')=='de'){
                field.val("Materialcode");
            }else{
                field.val("Codice materiale");
            }
        },500);
    });
    $('.se').keypress(function(event){
        if(event.keyCode == 13) {
            var material=$(this).val();
            if($('html').attr('lang')=='de'){
                $(this).prev('input').val('Materialcode');
            }else{
                $(this).prev('input').val('Codice materiale');
            }
            ajaxGetEditMaterialPage(material);
        }
    });
    $('#contact').click(function(event){
        event.preventDefault();
        $('#content').load('account/contactPage.jsp');
        $('#naviRight').load('navigation/navigationRightPage.jsp',{
            'action':'contact'
        });
    });
}

function ajaxGetMaterialsByCategory(category){
    if(category==categoryID){
        categoryID=0;
        category=0;
    }else{
        categoryID=category;
    }
    $.ajax({
        type: "GET",
        url: "gallery/galleryPage.jsp",
        dataType:"html",
        data: "action=group&id="+category,
        beforeSend:function(){
            $('#ajaxLoad').show().next().hide();
        },
        success:function(data){
            if(categoryID==0){
                minimizeMaterialGallery();
            }else{
                expandMaterialGallery();
            }
            $("#content").html(data);
            initGallery();
        },
        error:
        function (xhr){
            alert(xhr.status+" "+xhr.statusText);
        }
    });
}

function ajaxGetMaterialsByApplicationArea(apparea){
    $.ajax({
        type: "GET",
        url: "gallery/galleryPage.jsp",
        dataType:"html",
        data: "action=group&id="+categoryID+"&aid="+apparea,
        beforeSend:function(){
            $('#ajaxLoad').show().next().hide();
        },
        success:
        function(data){
            expandMaterialGallery();
            $("#content").html(data);
            initGallery();
        },
        error:
        function (xhr){
            alert(xhr.status+" "+xhr.statusText);
        }
    });
}


	



