//
// Displays a list pages: either child or 
// brother pages.
//
// This function is called :
//
// . On page call.
//
// . Following a change of a page status. In 
//   this case it reflects on the list of
//   its children and brothers (forceLiClass 
//   input is used in this case).
//
function DisplayPageList(items, ulIdName, forceLiClass)
{    
    // 
    // Display list of pages. 
    // 
    var str = '';

    for (i=0; i<items.length; i++)
    {
        //
        // Parse page data array.
        //
        catId = items[i][0];
        catLibName = items[i][1];
        liClass = items[i][2];
        catTitle = items[i][3];
        catFocus = items[i][4];
        
        if (forceLiClass != "")
        {
            liClass = forceLiClass;
        }
        
        if (isHtmlMode)
        {
            catUrl =  '../' + catLibName + '/';
        }
        
        else
        {
            catUrl =  AppendUrl(JS_MODULE_WLIB_CATEGORY + '?' + catId);
        }

        str += '<li id="'+catId+'" style="list-style-type: none; list-style-position: outside;">'; // 95%
        str += '<span class="handle">';
        str += '<img class="icon" src="'+imgWLib+'sheet-'+liClass+'.gif" />';
        str += (catFocus) ? 
            '<span class="disabled-link" alt="'+jsld["you are in this page"]+'" title="'+jsld["you are in this page"]+'">'+catTitle+'</span>':
            '<a href="'+catUrl+'">'+catTitle+'</a>';
            
        str += '</span>';
        str += '</li>';
    }

    $("#"+ulIdName).html(str);
    
    //
    // Make sortable.
    //
    if (isSwMode && isAdminOrAuthMember)
    {                                      
        $("#"+ulIdName).sortable(
        { 
            handle : '.handle', 
            update : function () 
            { 
                var order = $("#"+ulIdName).sortable('toArray'); 
                xajax_ajax_update_same_level_cat_order(order);
            } 
        });
    } 
}


//
// Join ml.
//
function JoinMl()
{    
    var f = document.forms['FormJoinMl'];
    
    var isNameEn = (f.isNameEn.value == 1);
    var isNameMust = (f.isNameMust.value == 1);
    var mlId = f.mlId.value;
    var email = f.email.value;
    var name = (isNameEn) ? f.name.value : '';

    email = (email == jsld["write here your email"]) ? '' : email;

    if (name == jsld["name"])
    {
        name = '';
    }
    
    else if (name == jsld["write here your name"])
    {
        name = '';
    }
    
    else if (name == jsld["write here your name (optional)"])
    {
        name = '';
    }
    
    f.name.value = name;
    
    // Case eror - blank (hidden) category ID.
    if (mlId == '')
    {
        DispHidBlockMsg(jsld["command not executed"], 'JoinMlFormMsg');
    } 
    
    // Case eror - blank email.
    else if (email == '')
    {
        DispHidBlockMsg(jsld["blank email"], 'JoinMlFormMsg');
    } 
    
    // Case eror - invalid email.
    else if (!isEmailSyntaxOk(email))
    {
        DispHidBlockMsg(jsld["invalid email"], 'JoinMlFormMsg');
    }
    
    // Case eror - blank name.
    else if (isNameMust && (name == ''))
    {
        DispHidBlockMsg(jsld["you should fill in your name as well"], 'JoinMlFormMsg');
    }
    
    // Check input email in database.
    else
    {
        Hide('JoinMlBtn');
        ShowInline('JoinMlIconServer');

        f.submit();
    }
}
                   

function image_gallery()
{                   
    $('a.gallery').zoomimage
    ({
        border: 0,
        centered: true,
        hideSource: false,
        preload: 'load',
        duration: 0,

        controlsTrigger: 'mouseover',
        shadow: 10,
        controls: true,
        opacity: 0.4,
        
        beforeZoomIn: function(boxID) {
            $('#' + boxID)
                .find('img')
                .css('opacity', 0)
                .animate(
                    {'opacity':1},
                    { duration: 500, queue: false }
                );
        },
        
        beforeZoomOut: function(boxID) {
            $('#' + boxID)
                .find('img')
                .css('opacity', 1)
                .animate(
                    {'opacity':0},
                    { duration: 500, queue: false }
                );
        }
    });
}
         
var gSldshowSrcs = new Array();
var gSldshowCaptions = new Array();
var gSldshowLastSrc = "";
var gSldshowImgInd = 1;
var gSldshowInterval;
 
function slideshow() 
{
    var img;
    var img_src;
    var img_caption;
    var img_height;
    var max_height = 0;
    
    var isCaption = false;
    var margin_top_first;
    var margin_top_caption;
    
    //
    // This function is recalled upon saving of 
    // page text. The clear-interval makes sure
    // not two intervals will run simultaneously.
    //
    clearInterval(gSldshowInterval);

    //
    // Set an array of slideshow image 
    // jQuery obejcts.
    //
    var images = $("img.slideshow");
    
    //
    // Case no slideshow images or only a single
    // slideshow image - return.
    //
    if (images.length < 2)
    {
        images.removeClass('slideshow');
        images.show();
        return;
    }
               
    //
    // Go over images:
    //
    // . Initialize global of image sources.
    // . Get max image height.
    //
    for (var i=0; i<images.length; i++)
    {
        img = images.eq(i);       

        img.removeAttr("width");  // If exists - may harm display.
        img.removeAttr("height"); // so that height() will give the true height of the image.

        img_src = img.attr("src");
        img_caption = img.attr("alt");
        img_height = img.height();
        
        gSldshowSrcs.push(img_src);
        gSldshowCaptions.push(img_caption);
        
        max_height = Math.max(max_height, img_height);
        
        if (img_caption != '')
        {
            isCaption = true;
        }
    }
    
    margin_top_caption =  isCaption ? 15 : 0;

    // 
    // Intialize global last-image-source
    // with first image source.
    //
    var first_img = $('img.slideshow:first');
    gSldshowLastSrc = first_img.attr("src");
    
    //
    // Set parent of first image:
    //
    // . position relative, so that following
    //   absolute positioning will be effective.
    //
    // . set height to maximal image height, 
    //   since absolute positioning comes over
    //   following data.
    //
    margin_top_first = parseInt(first_img.css("margin-top"));
    
    first_img.parent().css({'position': 'relative'});
    first_img.parent().css({'height': max_height + 30 + margin_top_first});

    //
    //
    // Hide all slideshow images, while
    // displaying the first one.
    //
    images.hide();
    first_img.show();
    
    //
    // Create DOM element of another image, positioned
    // on the first image, with the same source.
    //
    first_img.before("<div id='SldshowCaption'></div><img id='SldshowNextImg' class='image-with-frame' src='' />");
    $("#SldshowNextImg").attr({src: gSldshowLastSrc});
    $("#SldshowNextImg").css({'position': 'absolute', 'left': '0', top: margin_top_caption + margin_top_first + 'px'});
    first_img.css({'position': 'absolute', 'left': '0', top: margin_top_caption+'px'});
    $("#SldshowCaption").html(gSldshowCaptions[0]);

    //
    // Call slideshow animation.
    //
    gSldshowInterval = setInterval('_slideshow()',3600);            
}
    
function _slideshow() 
{
    var last_img = $('img.slideshow:first');
    var next_src = gSldshowSrcs[gSldshowImgInd];
    var next_caption = gSldshowCaptions[gSldshowImgInd];
    var next_img = $("#SldshowNextImg");

    last_img.attr({src: gSldshowLastSrc});
    last_img.css({'opacity': '1.0'});
    next_img.css({'opacity': '0.0'});
    next_img.attr({src: next_src});
    next_img.animate({opacity: 1.0}, 1000);
    last_img.animate({opacity: 0.0}, 1000);
    $("#SldshowCaption").html(next_caption);
    
    //
    // Set globals: 
    //
    // . last image source
    // . current image index
    //
    gSldshowLastSrc = next_src;
    
    gSldshowImgInd = gSldshowImgInd + 1;
    gSldshowImgInd = gSldshowImgInd % gSldshowSrcs.length;
}

//
// Send contact form.
//
// Placed here since the contact block is 
// called within an inner page, through
// another block.
//
function Contact()
{    
    //
    // Parse form data.
    //
    var f = document.forms['FormContact'];
    
    var catId = f.catId.value;
    var name = f.name.value;
    var emailphone = f.emailphone.value;
    var message = f.message.value;
    var isPhoneFld = (f.phone != undefined);

    var phone = isPhoneFld ? f.phone.value : "";
    
    //
    // Blank default values.
    //
    if (name == jsld["name"])
    {
        name = "";
    }

    if (message == jsld["message"])
    {
        message = "";
    }

    if (phone == jsld["phone"])
    {
        phone = "";
    }

    if (emailphone == jsld["email"])
    {
        emailphone = "";
    }

    if (emailphone == jsld["email/phone"])
    {
        emailphone = "";
    }

    //
    // Error cases.
    //
    var isBlankName = (name == '');
    var isBlankMsg = (message == '');
    var isBlankPhone = (phone == '');
    var isBlankEmailPhone = (emailphone == '');
        
    var isBlankContact = isPhoneFld ? 
        (isBlankEmailPhone && isBlankPhone):
        isBlankEmailPhone;
        
    var isEmailValid = isPhoneFld ? 
        (isEmailSyntaxOk(emailphone) || isBlankEmailPhone):
        (isEmailSyntaxOk(emailphone) || (emailphone.match("@") == null));
        
    if 
    (
        isBlankName ||
        isBlankContact ||
        isBlankMsg
    )
    {
        DispHidBlockMsg(jsld["you should fill in all the fields"], "ContactFormMsg");
        return false;
    } 
    
    else if (!isEmailValid)
    {
        DispHidBlockMsg(jsld["invalid email"], "ContactFormMsg");
        return false;
    }
    
    // Send the message.
    else
    {
        Hide('ContactFormMsg');
        Hide('ContactBtn');
        ShowInline('ContactIconServer');
        
        t = setTimeout("document.forms['FormContact'].submit()", 1 * 1000);
    }
}                               