var cache_product_xml = null;
var cache_textures_xml = null;
var pc_catalogue_id = "";
var contador = 1;
var selected_ctp = null;
var selected_product = null;
var selected_product_cate = null;
var selected_image_path_product = null;
var selected_image_path_product_cate = null;
var src_image_gif_cate = null;
var selected_configuration = null;
var selected_item_shape = null;
var array_se = new Array();
var size = {
    x: $(window).width(),
    y: $(window).height()
};

var partes;
var categorias;
var especificaciones_posibles=null;
var parts_selections_for_rev;
var last_textures=null;

function showDescrip() {
    if($('#description-product').css('visibility')=='hidden'){
        $('#description-product').css('visibility','visible');
    }else if($('#description-product').css('visibility')=='visible'){
        $('#description-product').css('visibility','hidden');
    }
}

function showTextMake() {
    $('#made_in_text').css('visibility','visible');
}

function hiddenTextMake() {
    $('#made_in_text').css('visibility','hidden');
}

function showEslogan(lang) {
    switch(lang)
    {
        case "en":
            if ($("#frase_eng").is(":hidden")) {
                $("#frase_eng").slideDown("slow");
            } else {
                $("#frase_eng").hide();
            }
            break;
        case "es":
            if ($("#frase_esp").is(":hidden")) {
                $("#frase_esp").slideDown("slow");
            } else {
                $("#frase_esp").hide();
            }
            break;
    }
}

function hideEslogan() {
    $('#menu_frase').children().each(function(){
        $(this).hide('slow');
    });
}

function initLanguage(lang) {
    $('#menu').html("");
    switch(lang)
    {
        case "en":
            selectedLanguage='en';
            lang_param='eng';
            break;
        case "es":
            selectedLanguage='es';
            lang_param='';
            break;
    }
    $.options = "<div class='option' onclick='javascript:showAuxiliar(\"about\")'><span id='menu_about'></span></div>" +
    //    "<div class='option' onclick='javascript:showAuxiliar(\"news\")'><span id='menu_news'></span></div>" + // Decidido quitarlo
    "<div class='option' onclick='javascript:showCatalog()'><span id='menu_catalog'></span></div>" +
    "<div class='option'><span id='menu_contact' onclick='javascript:showAuxiliar(\"contact\")'></span></div>" +
    "<div class='option'><span id='menu_blog' onclick='javascript:window.open(\"http://ardicoleccion.wordpress.com\")'></span></div>";
    $('#menu').html($.options);
    setMultilanguage();
    $('#auxiliar').removeClass("auxiliar-about");
    $('#auxiliar').hide();
}

function showCatalog() {
    $('#box-products').html("");
    $('#submenu').html("");
    $('#arrows').hide();
    load_colecciones(pc_catalogue_id);
}

function showColection(pc, name) {
    $('#box-products').html("");
    load_products(pc);
    $('#span_return').html("");
    $('#span_return').html(name);
}

function showAuxiliar(mode) {
    $('#auxiliar').html("");
    //  $('#auxiliar').css("height",(size.y-5)+"px");
    $('#menu').html("");
    $('#submenu').html("");
    $('#box-products').html("");
    $('#arrow-top').removeClass("arrow-top");
    $('#arrow-bottom').removeClass("arrow-bottom");
    $('#auxiliar').removeClass("auxiliar-about");
    $('#auxiliar').show("");
    switch(mode)
    {
        case "news":
            $('#menu').append("<div class='option' onclick='javascript:initLanguage(\"default\")'><span id='menu_news'></span></div>");
            $('#auxiliar').html("");
            $('#auxiliar').css("width","400px");
            $('#auxiliar').html("<div id='newsRss'></div>");
            initialize();
            //load_xml_news();
            setMultilanguage();
            break;
        case "contact":
            $('#menu').append("<div class='option' onclick='javascript:initLanguage(\"default\")'><span id='menu_contact'></span></div>");
            $('#auxiliar').css("width","700px");
            $('#auxiliar').html("<div id='div_text_contact'><span id='text_contact'></span>");
            var MapEmbed ='<iframe width="300" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.es/maps?hl=es&amp;q=dise%C3%B1os+tapizados+mu%C3%B1oz&amp;client=firefox-a&amp;ie=UTF8&amp;source=embed&amp;cid=795427928733374864&amp;hq=dise%C3%B1os+tapizados+mu%C3%B1oz&amp;hnear=&amp;ll=39.357794,-0.423489&amp;spn=0.019909,0.025663&amp;z=14&amp;output=embed"></iframe><br /><small>';
            $('#auxiliar').html($('#auxiliar').html()+"<div id='mapEmbed'>"+MapEmbed+"</div>");
            $('#auxiliar').append("<div id='linkMail'><a class='link_email' href='mailto:ardicoleccion@ardicoleccion.com'>ardicoleccion@ardicoleccion.com</a></div>");
            var formToInsert  = "<div id='formSend'>" +
            "<input type='text' id='iName'></input>" +
            "<div id='txt_name' class='textForm'></div>" +
            "<input type='text' id='iEmail'></input>" +
            "<div id='txt_email' class='textForm'></div>" +
            "<input type='text' id='iIssue'></input>" +
            "<div id='txt_subject' class='textForm'></div>" +
            "<textarea rows='8' onkeydown='' class='Textbox' id='iBody'></textarea>" +
            "<div id='txt_body' class='textForm'></div>" +
            "<div id='options_form'><div id='txt_reset' onclick='reset_form()'></div>" +
            "<div id='txt_send' onclick='call_send_product_link()'></div></div>" +
            "<div id='sender></div>" +
            "</div>";
            $('#auxiliar').append(formToInsert);
            //<div id="txt_send" onclick="call_send_product_link()"></div>
            setMultilanguage();
            break;
        case "about":
            $('#auxiliar').addClass("auxiliar-about");
            $('#auxiliar').css("width","400px");
            $('#auxiliar').html("<div id='div_text_about'><span id='text_about'></span>");
            $('#menu').html("<div class='option' onclick='javascript:initLanguage(\"default\")'><span id='menu_about'></span></div>");
            setMultilanguage();
            break;
    }
}

function returnMenu() {
    $('#all2').hide();
    $('#info-product').hide();
    $('#auxiliar').hide();
    $('#auxiliar').removeClass("auxiliar-about");
    $('#all').show();

    $('#divCoatingApplet').css('display', 'none');
    $('#divImage').css('display', 'block');
    $('#description-product').css('visibility', 'hidden');

    $('#name-product').html("");
    $('#divImage').html("");
    $('#scroll-others-images-product').html("");
    $('#textures-product-scroll').html("");

    clean_textures_scroll();
}

function get_pc_catalogue_id_by_catalogue_name() {
    var auxscape = escape(catalogue_name);

    var call = new servCall("Query");
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Pc_Id_Catalogue'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('source_type', escape(source_type)));

    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "get_pc_catalogue_id_by_catalogue_name";

    loadXml( call, params );
}

function cb_get_pc_catalogue_id_by_catalogue_name(result) {

    var msg = $(result).find("msg").text();

    if (msg == "") {
        alert("CATALOGUE NAME NOT FOUND");
    } else {
        pc_catalogue_id = msg;
    }
	
    $('#all2').hide();
    $('#info-product').hide();
    $('#auxiliar').hide();
    $('#auxiliar').removeClass("auxiliar-about");
    $('#all').show();
    total_imagenes= $(document).find('.background').size();
    $(document).find(".background").each( function() {
        $(this).hide();
    });
    var size = {
        x: $(window).width(),
        y: $(window).height()
    };
    $('body').css('width',size.x+"px");
    $('body').css('height',size.y+"px");
    setElementLanguage("make_for", "sMake", selectedLanguage);
}

function load_colecciones( pc_catalogue_id ) {

    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Products'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('relating', pc_catalogue_id));
    call.arguments.push(new Array('source_type', escape(source_type)));
    call.arguments.push(new Array('language', lang_param));

    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "load_colecciones";

    loadXml( call, params );
}

function cb_load_colecciones(result) {

    $(result).find('result').children('Product_class').each(function(){
        var iid = $(this).attr('id');
        var name = $(this).find('name').text();
        var tname = translateAttribute( result, $(this).find('name').text(), iid, "name" );

        $.colec = "<div class=\"option\"  onclick=showColection('" +  iid + "','" +  tname + "')>" + tname + "</div>";
        $('#submenu').append($.colec);
    });
}

function load_products( pc ) {
    
    $('#box-products').addClass('loading_white');
    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Products'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('relating', pc));
    call.arguments.push(new Array('docrole', 'Miniatura Producto'));
    call.arguments.push(new Array('language', lang_param));
    
    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "load_products";

    loadXml( call, params );
}

function cb_load_products(result) {
    
    product_list = new Array();
    $('#arrows').show();

    $(result).find('result').children('Product_class').each(function(){
        var iid = $(this).attr('id');
        var name = $(this).find('name').text();
        var tname = translateAttribute( result, $(this).find('name').text(), iid, "name" );

        // PRODUCT MAIN IMAGE
        var docrole = 'Miniatura Producto';
        var image_path_product = get_image_path_by_rolelist(result, iid, docrole);
        var image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_SMALL[0] + "&height=" + DIM_PRODUCT_IMAGE_SMALL[1] + "&file=" + image_path_product +
        "&catalogue_name=" + escape(catalogue_name) +
        "&database_id=" + escape(database_id) +
        "&source_type="+ escape(source_type);
        $.image = "<img height='" + DIM_PRODUCT_IMAGE_SMALL[1] + "' width='" + DIM_PRODUCT_IMAGE_SMALL[0] + " 'border='0' src=" + image_src_product + " />";
        $.prod = "<div class='product' id='"+ iid +"'><div class='name'>" + tname + "</div><div class='image'>" + $.image + "</div></div>";
        $('#box-products').append($.prod);
        product_list[product_list.length]=iid;
        $('.product:last-child').unbind('click');
        $('.product:last-child').click(function() {
            load_product(iid);
        });
    });
    init_products_scroll();
    init_scrolls();
    $('#box-products').removeClass('loading_white');
}

function load_product( pc ) {
    $('#all').hide();
    $('#description-cate').hide();
    stopBackgroundRotative();
    $('#all2').show();
    $('#info-product').show();
    $('#info-product').addClass('loading_white');

    //Borrar capa Cate Gif
    selected_product = null;
    selected_product_cate = null;
    selected_image_path_product = null;
    selected_image_path_product_cate = null;
    $('#revestimiento').css('background-image', 'none');
    $('#revestimiento').unbind('click');

    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Product'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('product', pc));
    //call.arguments.push(new Array('docrole', escape('loading--pdf--Product Main Image--Miniatura Imagen Auxiliar--Miniatura Producto--Imagen Auxiliar--2D Geometry CTP Information')));
    call.arguments.push(new Array('docrole', escape('pdf--Product Main Image')));
    call.arguments.push(new Array('source_type', escape(source_type)));
    call.arguments.push(new Array('language', lang_param));

    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "load_product";

    loadXml( call, params );
}

function cb_load_product(result) {
    cache_product_xml= result;
    //  product_list = new Array();
    $('#scroll-others-images-product').html("");
    other_image_list = new Array();
    var iid;

    $(result).find('result').children('Product_class').each(function(){
        iid = $(this).attr('id');
        //var name = $(this).find('name').text();
        var tname = translateAttribute( result, $(this).find('name').text(), iid, "name" );
        $('#name-product').html(tname);
        //var descr = $(this).find('description').text();
        var tdescr = translateAttribute( result, $(this).find('description').text(), iid, "description" );
        $('#description-product').html(tdescr);
        //text_showPDF
        var docrole = 'pdf';
        var pdf_path_product = get_image_path_by_rolelist(result, iid, docrole);
        var url_pdf = url_serv + "Retrieve_document?mode=&file=" + pdf_path_product+
        "&catalogue_name=" + escape(catalogue_name) +
        "&database_id=" + escape(database_id) +
        "&source_type="+ escape(source_type);
        $('#text_showPDF').unbind('click');
        $('#text_showPDF').click(function() {
            window.open(url_pdf);
        });

        // PRODUCT MAIN IMAGE
        docrole = 'Product Main Image';
        var image_path_product = get_image_path_by_rolelist(result, iid, docrole);
        var image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_NORMAL[0] + "&height=" + DIM_PRODUCT_IMAGE_NORMAL[1] + "&file=" + image_path_product +
        "&catalogue_name=" + escape(catalogue_name) +
        "&database_id=" + escape(database_id) +
        "&source_type="+ escape(source_type);
        $.image = "<img height='" + DIM_PRODUCT_IMAGE_NORMAL[1] + "' border='0' src=" + image_src_product + " />";
        $('#divImage').html($.image);


        //Poner la principal tmb como imagen en chiquitin
        aux_url_small = image_path_product.split('/');
        image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_SMALL_OI[0] + "&height=" + DIM_PRODUCT_IMAGE_SMALL_OI[1] + "&file=" + aux_url_small[0] + "/127X67/" + aux_url_small[2] +
        "&catalogue_name=" + escape(catalogue_name) +
        "&database_id=" + escape(database_id) + "&source_type=source_type";
        $.image_small = "<img height='" + DIM_PRODUCT_IMAGE_SMALL_OI[1] + "' border='0' src=" + image_src_product + " />";
        $.aux_small = "<div class='other-image' id='oi_"+ iid +"'>"+ $.image_small +"</div>";
        $('#scroll-others-images-product').append($.aux_small);
        other_image_list[other_image_list.length]="oi_"+iid;
        $('.other-image:last-child').unbind('click');
        $('.other-image:last-child').click(function() {
            $('#textures-product-scroll').html("");
            clean_textures_scroll();
            /* Cambiar Imagen */
            $('#divImage').html("");
            aux_url = image_path_product.split('/');
            var image_src_product_normal = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_NORMAL[0] + "&height=" + DIM_PRODUCT_IMAGE_NORMAL[1] + "&file=" + aux_url[0] + "/739x486/" + aux_url[2] +
            "&catalogue_name=" + escape(catalogue_name) +
            "&database_id=" + escape(database_id) +
            "&source_type=" + escape(source_type);
            $.image = "<img height='" + DIM_PRODUCT_IMAGE_NORMAL[1] + "' border='0' src=" + image_src_product_normal + " />";
            $('#divImage').append($.image);

            selected_product = iid;
            selected_image_path_product = image_path_product;

            $('#textures-product-scroll').html("");
            clean_textures_scroll();
            $('#description-cate').hide();
            $('#revestimiento').css('display','block');
            $('#textures-product').css('display','none');
            //get_configuration(selected_product);
            $('#divCoatingApplet').css('display', 'none');
            $('#divImage').css('display', 'block');
        });
    });
    draw_images_product(iid);

    $('#revestimiento').css('display','block');
    $('#textures-product').css('display','none');
    $('#textures-product-scroll').html("");
    clean_textures_scroll();
    $('#info-product').removeClass('loading_white');
}

function draw_images_product(pc){
    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Product'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('product', pc));
    call.arguments.push(new Array('docrole', escape('loading--pdf--Product Main Image--Miniatura Imagen Auxiliar--Miniatura Producto--Imagen Auxiliar--2D Geometry CTP Information')));
    call.arguments.push(new Array('source_type', escape(source_type)));
    call.arguments.push(new Array('language', lang_param));

    var params = new Array(3);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "draw_images_product";
    params[2][0] = "pc";
    params[2][1] = pc;

    loadXml( call, params );
}

function cb_draw_images_product(result,pc){
    var cache_product_xml=result;

    var docrole = 'Miniatura Imagen Auxiliar--2D Geometry CTP Information';
    var array_images = findDocumentAssignmentForProductClassByRole(cache_product_xml,pc,docrole);
    // draw coating2D image
    var df_2d = findDigitalFileByContainedDataType(cache_product_xml, 'Geometry Data');
    var fli_2d = findFileLocationIdentificationByDigitalFile(cache_product_xml, df_2d);

    $(cache_product_xml).find('File_location_identification').each(function(){
        if (fli_2d == $(this).attr('id')){
            var image_path_product = $(this).find('source_id').text() + $(this).find('external_id').text();
            var image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_SMALL[0] + "&height=" + DIM_PRODUCT_IMAGE_SMALL[1] + "&file=" + image_path_product +
            "&catalogue_name=" + escape(catalogue_name) +
            "&database_id=" + escape(database_id) + "&source_type=" + source_type;
            $.image = "<div style='padding:0;margin:0;'><img class='imgCATe' height='" + DIM_PRODUCT_IMAGE_SMALL[1] + "' border='0' src=" + image_src_product + " />";
            $.image += "<img class='logoCATe' src='img/logo-cate-oficial.png' /></div>"
            $.aux = "<div class='other-image' id='"+ df_2d +"'>"+ $.image +"</div>";
            
            //selected_product = pc;
            //selected_image_path_product = image_path_product;
            selected_product_cate = pc;
            selected_image_path_product_cate = image_path_product;

            //cargamos en memoria la información de revestimiento para el producto
            get_specifications(selected_product_cate);

            // draw gif_cate image
            var docrole_loading = 'loading';
            var image_path_loading = get_image_path_by_rolelist(cache_product_xml,pc,docrole_loading);
            var image_src_loading = url_serv + "Retrieve_document?mode=&width=" + DIM_PRODUCT_IMAGE_SMALL[0] + 
            "&height=" + DIM_PRODUCT_IMAGE_SMALL[1] +
            "&file=" + image_path_loading +
            "&catalogue_name=" + escape(catalogue_name) +
            "&database_id=" + escape(database_id) +
            "&source_type=" + source_type;

            src_image_gif_cate=image_src_loading;
            if (src_image_gif_cate!=null){
                $('#revestimiento').css('background-image', 'url("' + src_image_gif_cate + '")');
                $('#revestimiento').css('background-position','left bottom');
                $('#revestimiento').css('background-repeat','no-repeat');
                //Colocar el evento click
                $('#revestimiento').unbind('click');
                $('#revestimiento').click(function() {
                    $('#textures-product').css('display','block');
                    $('#revestimiento').css('display','none');
                    $('#textures-product-scroll').html("");
                    clean_textures_scroll();
                    $('#description-cate').show();
                    selected_product = selected_product_cate;
                    selected_image_path_product = selected_image_path_product_cate;
                    get_configuration(selected_product);
                });
            }
        }
    });


    for(i=0;i<array_images.length;i++){
        var df = findDigitalFileByDocumentAssignment(cache_product_xml, array_images[i]);
        var fli = findFileLocationIdentificationByDigitalFile(cache_product_xml,df);
        var aux_url;

        $(cache_product_xml).find('File_location_identification').each(function(){
            if (fli == $(this).attr('id')){
                var image_path_product = $(this).find('source_id').text() + $(this).find('external_id').text();
                var image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_SMALL_OI[0] + "&height=" + DIM_PRODUCT_IMAGE_SMALL_OI[1] + "&file=" + image_path_product +
                "&catalogue_name=" + escape(catalogue_name) +
                "&database_id=" + escape(database_id) + "&source_type=source_type";
                $.image = "<img height='" + DIM_PRODUCT_IMAGE_SMALL_OI[1] + "' border='0' src=" + image_src_product + " />";
                $.aux = "<div class='other-image' id='"+ df +"'>"+ $.image +"</div>";
                $('#scroll-others-images-product').append($.aux);
                other_image_list[other_image_list.length]=df;
                $('.other-image:last-child').unbind('click');
                $('.other-image:last-child').click(function() {
                    $('#textures-product-scroll').html("");
                    clean_textures_scroll();
                    /* Cambiar Imagen */
                    $('#divImage').html("");
                    aux_url = image_path_product.split('/');
                    var image_src_product_normal = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_PRODUCT_IMAGE_NORMAL[0] + "&height=" + DIM_PRODUCT_IMAGE_NORMAL[1] + "&file=" + aux_url[0] + "/739x486/" + aux_url[2] +
                    "&catalogue_name=" + escape(catalogue_name) +
                    "&database_id=" + escape(database_id) +
                    "&source_type=" + escape(source_type);
                    $.image = "<img height='" + DIM_PRODUCT_IMAGE_NORMAL[1] + "' border='0' src=" + image_src_product_normal + " />";
                    $('#divImage').append($.image);

                    selected_product = pc;
                    selected_image_path_product = image_path_product;

                    $('#textures-product-scroll').html("");
                    clean_textures_scroll();
                    $('#description-cate').hide();

                    /* Poner click Cate cunado seleccione una imagen normal */
                    $('#revestimiento').css('display','block');
                    $('#textures-product').css('display','none');

                    //get_configuration(selected_product);
                    $('#divCoatingApplet').css('display', 'none');
                    $('#divImage').css('display', 'block');
                });
            }
        });
    }
    
    init_scrolls_others_images();
    init_others_images_scroll();
}

function setMultilanguage() {
    setElementLanguage("menu_about", "sAbout", selectedLanguage);
    setElementLanguage("menu_news", "sNews", selectedLanguage);
    setElementLanguage("menu_catalog", "sCatalog", selectedLanguage);
    setElementLanguage("menu_contact", "sContact", selectedLanguage);
    setElementLanguage("menu_blog", "sBlog", selectedLanguage);
    setElementLanguage("text_about", "sAboutText", selectedLanguage);

    setElementLanguage("text_contact", "sContactText", selectedLanguage);
    setElementLanguage("txt_name", "sContactFormName", selectedLanguage);
    setElementLanguage("txt_email", "sContactFormEmail", selectedLanguage);
    setElementLanguage("txt_subject", "sContactFormSubject", selectedLanguage);
    setElementLanguage("txt_body", "sContactFormBody", selectedLanguage);
    setElementLanguage("txt_reset", "sContactFormReset", selectedLanguage);
    setElementLanguage("txt_send", "sContactFormSend", selectedLanguage);
    setElementLanguage("descrip_cate", "sDesCATe", selectedLanguage);

    setElementLanguage("text_showDescription", "sShowDescrip", selectedLanguage);
    setElementLanguage("text_showPDF", "sShowPDF", selectedLanguage);
    setElementLanguage("make_for", "sMake", selectedLanguage);
}

/* coating2D */
function get_configuration( product ) {
    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Configuration'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('product', escape(product)));

    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "get_configuration";

    loadXml( call, params );
}

function cb_get_configuration(result) {
    selected_configuration = null;
    selected_configuration = $(result).find('result').children('Product_specification').find('item_context[ref='+ selected_product +']').parent().attr("id");
    if (selected_configuration != null)
        get_ctp( selected_product, selected_image_path_product, selected_configuration );
    else{
        $('#divCoatingApplet').css('display', 'none');
        $('#divImage').css('display', 'block');
    }
}

function get_ctp( product, image_path, configuration ) {
    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Scene'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('product', escape(product)));
    call.arguments.push(new Array('image_path', escape(image_path)));
    call.arguments.push(new Array('source_type', escape(source_type)));
    call.arguments.push(new Array('configuration', escape(configuration)));
    call.arguments.push(new Array('url_web', escape(url_web)));

    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "get_ctp";

    loadXml( call, params );
}

function cb_get_ctp(result) {
    var ctp = $(result).find("ctp").text();
    selected_item_shape = $(result).find("is").text();

    array_se = new Array();
    $(result).find('shape_element').each(function(){
        var id = $(this).find('id').text();
        var element_name = $(this).find('element_name').text();
        var array = new Array(id, element_name);

        array_se.push(array);
    });

    if (ctp == "") {
        $('#divCoatingApplet').css('display', 'none');
        $('#divImage').css('display', 'block');
    } else {
        // cambiado para ir en chrome
        var params = "<applet codebase='http://80.169.75.106/' code='es.aidima.coatingtool.main.coatingImage2D.class' archive='Applet/Coating2DWeb.jar,Applet/lib/castor-1.3.jar,Applet/lib/castor-1.3-core.jar,Applet/lib/castor-1.3-xml-schema.jar,Applet/lib/commons-logging-1.1.jar,Applet/lib/ctplib.jar,Applet/lib/java-image-scaling-0.8.4.jar,Applet/lib/javautilscoating.jar' width='555' height='370' name='CoatingApplet'>"+
        "<param name='backgroundColor' value='#000000'/>" +
        "<param name='backgroundImage' value='Applet/img/loading.gif'/>" +
        "<param name='url_serv' value='" + url_serv + "'/>" +
        "<param name='dataBaseName' value='" + database_name + "'/>" +
        "<param name='catalogueName' value='" + catalogue_name + "'/>" +
        "<param name='imagePath' value='" + selected_image_path_product + "'/>" +
        "<param name='ctpPath' value='" + ctp + "'/>" +
        "<param name='debugging' value='ACTIVE'/>" +
        "<param name='databaseid' value='ardi'/>" +
        "<param name='image' value='" + escape(selected_image_path_product) + "'/>" +
        "</applet>";

        $('#divCoatingApplet').html( "" );
        $('#divCoatingApplet').html( params );

        $('#divImage').css('display', 'none');
        $('#divCoatingApplet').css('display', 'block');
    }
}

// Functions for Finished Load CTP
function loadSceneFinished(result){
//    $('#divLoadingCoatingApplet').css('display','none');
//    $('#divLoadingCoatingApplet').css('visibility','hidden');
//    $('#divCoatingApplet').css('visibility','visible');
}

function partSelectionChanged(options){
    var parts_selections = new Array();
    if(options!=-1){
        var groups = options.split(';');
        for (var c=0; c<groups.length; c++) {
            var group = groups[c];
            for(var i=0; i<array_se.length; i++) {
                var elem = array_se[i];
                if (elem[0] == group) {
                    parts_selections.push(elem[1]);
                }
            }
        }
        if (parts_selections.length > 0) {
            parts_selections_for_rev=parts_selections;
            especificaciones_posibles=filter_textures(parts_selections);
            draw_textures_product();
        }else{}
    }else{
        especificaciones_posibles = null;
        $('#textures-product-scroll').html("");
        clean_textures_scroll();
    }
}

function filter_textures(parts_selections){
    var posibles_aux = new Array();
    for(var j=0; j<parts_selections.length; j++) {
        for(var i=0; i< partes.length; i++) {
            if(parts_selections[j]==partes[i].id){
                var k;
                if(posibles_aux.length>0){
                    var aux=new Array();
                    for(k=0; k< partes[i].especificaciones.length; k++) {
                        var l;
                        for(l=0; l< posibles_aux.length && (partes[i].especificaciones[k].id!=posibles_aux[l]); l++){}
                        if(l!=posibles_aux.length){
                            aux.push(partes[i].especificaciones[k].id);
                        }
                    }
                    posibles_aux = aux;
                }else{
                    for(k=0; k< partes[i].especificaciones.length; k++) {
                        posibles_aux.push(partes[i].especificaciones[k].id)
                    }
                }
            }
        }
    }
    return posibles_aux;
}

function draw_textures_product() {
    textures_list = new Array();
    for(var c=0; c<categorias.length; c++) {
        for(var d=0; d < categorias[c].especificaciones.length; d++) {
            var id_text= categorias[c].especificaciones[d].id;
            var name_text = categorias[c].especificaciones[d].nombre;
            var img_tex = categorias[c].especificaciones[d].img;
            var co = img_tex.indexOf('file=')+5;
            var ac = img_tex.indexOf('&',co);
            var img_tex_file = img_tex.substring(co,ac);
            var anc_tex = categorias[c].especificaciones[d].ancho;
            var alt_tex = categorias[c].especificaciones[d].alto;
            if(!(texture_posible_with_part(categorias[c].especificaciones[d]))){
                $.image = "<img title='"+ name_text +"' alt='"+ name_text +"' width='" + DIM_TEXTURE_IMAGE[0] + "' height='" + DIM_TEXTURE_IMAGE[1] + "' border='0' src=" + img_tex + " />";
                $.aux = "<div class='texture-image' style='filter: alpha(opacity = 20);opacity:0.2; cursor:default' id='"+ id_text +"'>"+ $.image +"</div>";
                $('#textures-product-scroll').append($.aux);
                textures_list[textures_list.length]=id_text;
            }else{
                $.image = "<img title='"+ name_text +"' alt='"+ name_text +"' width='" + DIM_TEXTURE_IMAGE[0] + "' height='" + DIM_TEXTURE_IMAGE[1] + "' border='0' src=" + img_tex + " />";
                $.aux = "<div class='texture-image' id='"+ id_text +"' onclick=\"javascript:doCoat(\'"+ id_text +"\',\'"+ img_tex_file +"\',\'"+ anc_tex +"\',\'"+ alt_tex +"\');\">"+ $.image +"</div>";
                $('#textures-product-scroll').append($.aux);
                textures_list[textures_list.length]=id_text;
            }
        }
    }
    $('#textures-product').removeClass('loading_white');
    init_scrolls_textures();
    init_textures_scroll();
}

function texture_posible_with_part(esp) {
    if(especificaciones_posibles!=null){
        var l;
        for(l=0; l< especificaciones_posibles.length && (especificaciones_posibles[l]!=esp.id); l++){}
        if(l!=especificaciones_posibles.length) return true;
        else return false;
    }else return false;
}

function save_texture_used_in_parts(parts_selections,id_texture){
    for(var i=0; i<parts_selections.length; i++) {
        for(var j=0; j< partes.length; j++) {
            if(parts_selections[i]==partes[j].id){
                partes[j].textura_res = id_texture;
            }
        }
    }
}

/*****************************/
// Functions for DoubleClick
/*****************************/
function partSelectionChangedDC( options ) {
    $('.loading_textures').css('visibility','visible');
    $('.scroll-pane').css('visibility','hidden');
    var parts_selections = new Array();
    if(options!=-1){
        var groups = options.split(';');
        for (var c=0; c<groups.length; c++) {
            var group = groups[c];
            for(var i=0; i<array_se.length; i++) {
                var elem = array_se[i];
                if (elem[0] == group) {
                    parts_selections.push(elem[1]);
                }
            }
        }
        if (parts_selections.length > 0) {
            parts_selections_for_rev=parts_selections;
            especificaciones_posibles=filter_textures(parts_selections);
            reCoatWithLastTexture(parts_selections);
        }else{}
    }else{
        especificaciones_posibles = null;
        $('#textures-product-scroll').html("");
        clean_textures_scroll();
    }
}

function reCoatWithLastTexture(parts_selections) {
    if (last_textures!=null && last_textures!=""){
        var id_texture = last_textures[0] ;
        //var name_texture = last_textures[1];
        //var image_path_texture = last_textures[2];
        var image_path_revestir = last_textures[3];

        var d=0;
        for(d=0; d< especificaciones_posibles.length && (id_texture != especificaciones_posibles[d]) ; d++) {}
        if(d!=especificaciones_posibles.length){
            recoat();
            save_texture_used_in_parts(parts_selections_for_rev,id_texture);
        }else
            alert('La &uacute;ltima textura que se utilizo no se puede utilizar en esta parte');
    }else{
        alert('No has utilizado aun ninguna textura');
    }
}


// Functions for Load Textures in Memory
function Parte(id, nombre, tex_res) {
    this.id = id;
    this.nombre = nombre;
    this.especificaciones = new Array();
    this.textura_res = tex_res;
}

function Categoria(id, nombre) {
    this.id = id;
    this.nombre = nombre;
    this.especificaciones = new Array();
}

function Especificacion(id, nombre) {
    this.id = id;
    this.nombre = nombre;
    this.img = null;
    this.ancho = null;
    this.alto = null;
}

function get_specifications(product) {
    var call = new servCall("Query") ;
    call.method = "GET";
    call.arguments.push(new Array('action', 'Get_Specifications'));
    call.arguments.push(new Array('mode', 'product-parts'));
    call.arguments.push(new Array('catalogue_name', escape(catalogue_name)));
    call.arguments.push(new Array('database_id', escape(database_id)));
    call.arguments.push(new Array('product', escape(product)));
    call.arguments.push(new Array('source_type', escape(source_type)));
    call.arguments.push(new Array('docrole', 'Texture Image File'));
    var params = new Array(2);
    for (i= 0; i < params.length; ++ i)
        params [i] = new Array(2);
    params[0][0] = "url_serv";
    params[0][1] = url_serv;
    params[1][0] = "func";
    params[1][1] = "get_specifications";
    loadXml( call, params );
}

function cb_get_specifications(result) {
    try{
        partes = new Array();
        $(result).find('Product_class level_type:contains("PART")').each(function(){
            var esp_part = get_specifications_by_part(result,$(this).parent().attr('id'));
            var p = new Parte($(this).parent().find('id').text(),$(this).parent().find('name').text(),'Nins_text_used');
            p.especificaciones = esp_part;
            partes.push(p);
        });
        categorias = new Array();
        $(result).find('Specification_category').each(function(){
            if($(this).find('id').text()!='OPCIONES'){
                var esp_categ = get_specifications_by_categoria(result,$(this).attr('id'));
                var p = new Categoria($(this).attr('id'),$(this).find('id').text());
                p.especificaciones = esp_categ;
                categorias.push(p);
            }
        });
    }catch(e){
        alert(e);
    }
}

function get_specifications_by_part(result,id_compare) {
    var especifications = new Array();
    var spc;
    $(result).find('Product_specification item_context[ref='+id_compare+']').each(function(){
        $(this).parent().find('defining_specifications').find('Specification').each(function(){
            spc = new Especificacion($(this).attr('ref'),'');
            especifications.push(spc);
        });
    });
    return especifications;
}

function get_specifications_by_categoria(result,id_compare) {
    var especifications = new Array();
    var spc;
    $(result).find('Specification').each(function(){
        if(id_compare == $(this).find('category').attr('ref')){
            spc = new Especificacion($(this).attr('id'),$(this).find('name').text());
            especifications.push(fill_texture_info(result,spc));
        }
    });
    return especifications;
}

function fill_texture_info(result,spc) {
    var image_path_product, image_src_product, df, fli, rapport;
    $(result).find('Document_assignment is_assigned_to Specification[ref='+ spc.id +']').each(function(){
        df = $(this).parent().parent().find('assigned_document').find('Digital_file').attr('ref');
        fli = findFileLocationIdentificationByDigitalFile(result,df);
        $(result).find('File_location_identification[id=' + fli + ']').each(function(){
            image_path_product = $(this).find('source_id').text() + $(this).find('external_id').text();
            image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_TEXTURE_IMAGE[0] + "&height=" + DIM_TEXTURE_IMAGE[1] + "&file=" + image_path_product +
            "&catalogue_name=" + escape(catalogue_name) +
            "&database_id=" + escape(database_id) +
            "&source_type=" + escape(source_type);
        });
        rapport = findDfRapport(result, df);
        spc.ancho = rapport[0];
        spc.alto = rapport[1];
    });
    spc.img = image_src_product;
    return spc;
}

function get_image(tipo,id){
    var image_src_product=null;
    image_src_product = url_serv + "Retrieve_document?mode=thumb&width="+ DIM_AREA[0] +
    "&height="+ DIM_AREA[1] +
    "&iid=" + id +
    "&catalogue_name=" + escape(catalogue_name) +
    "&database_id=" + escape(database_id) +
    "&source_type=" + escape(source_type) +
    "&docrole=" + escape(tipo);
    return image_src_product;
}

/*function draw_textures_product(result){
    var cache_textures_xml = result;
    console.log(XmlToString(result));
    textures_list = new Array();

    $(cache_textures_xml).find('Specification').each(function(){
        var id_sp = $(this).attr('id');
        var id = $(this).find('id').text();
        var name = $(this).find('name').text();

        $(cache_textures_xml).find('Document_assignment').each(function(){
            if (id_sp == $(this).find('is_assigned_to').find('Specification').attr('ref')){

                var df = $(this).find('assigned_document').find('Digital_file').attr('ref');
                var fli = findFileLocationIdentificationByDigitalFile(cache_textures_xml,df);

                $(cache_textures_xml).find('File_location_identification').each(function(){
                    if (fli == $(this).attr('id')){
                        var image_path_product = $(this).find('source_id').text() + $(this).find('external_id').text();
                        var image_src_product = url_serv + "Retrieve_document?mode=thumb&width=" + DIM_TEXTURE_IMAGE[0] + "&height=" + DIM_TEXTURE_IMAGE[1] + "&file=" + image_path_product +
                        "&catalogue_name=" + escape(catalogue_name) +
                        "&database_id=" + escape(database_id) +
                        "&source_type=" + escape(source_type);

                        $.image = "<img title='"+ name +"' alt='"+ name +"' width='" + DIM_TEXTURE_IMAGE[0] + "' height='" + DIM_TEXTURE_IMAGE[1] + "' border='0' src=" + image_src_product + " />";
                        $.aux = "<div class='texture-image' id='"+ id_sp +"'>"+ $.image +"</div>";
                        $('#textures-product-scroll').append($.aux);
                        textures_list[textures_list.length]=id_sp;
                        $('.texture-image:last-child').unbind('click');
                        $('.texture-image:last-child').click(function() {
                            doCoat(id,image_path_product,'10','10');
                        });

                    }
                });
            }
        });
    });
    $('#textures-product').removeClass('loading_white');
    init_scrolls_textures();
    init_textures_scroll();
}*/
