﻿/// <reference path="jquery-1.4.2.js" />
/// <reference path="jquery-1.4.2-vsdoc.js" />

$(document).ready(function() {

    //*******MasterPage scripts*******

    //preload images for Main Menu
    (function($) {
        var cache = [];
        // Arguments are image paths relative to the current page.
        $.preLoadImages = function() {
            var args_len = arguments.length;
            for (var i = args_len; i--; ) {
                var cacheImage = document.createElement('img');
                cacheImage.src = arguments[i];
                cache.push(cacheImage);
            }
        }
    })(jQuery)

    jQuery.preLoadImages('images/layout/menu/main_over.jpg', 'images/layout/menu/main_on.jpg', 'images/layout/menu/forum_over.jpg', 'images/layout/menu/forum_on.jpg', 'images/layout/menu/downloads_over.jpg', 'images/layout/menu/downloads_on.jpg', 'images/layout/menu/hebrew_over.jpg', 'images/layout/menu/hebrew_on.jpg', 'images/layout/menu/fm2010_over.jpg', 'images/layout/menu/fm2010_on.jpg', 'images/layout/menu/fm2011_over.jpg', 'images/layout/menu/fm2011_on.jpg', 'images/layout/menu/fmlive_over.jpg', 'images/layout/menu/fmlive_on.jpg');

    //dialog for password recovery
    var dialog = $("#divPassword").dialog(
    {
        title: 'שחזור משתמש/סיסמה',
        autoOpen: false,
        resizable: false,
        modal: true,
        width: 400
    });

    //append the dialog to the ASP.NET form (by default, it is seperated)
    dialog.parent().appendTo($("form:first"));

    dialog.children().show();

    $("a:contains('סיסמה')").click(function() {
        $('#divPassword').dialog('open');
    });

    var inprog = 0;

    //small menus - ResearchItems
    var position = $("#lnkResearchItems").position();
    position.top += $("#lnkResearchItems").height();
    position.left += $("#lnkResearchItems").width() - $("#divResearchItems").width();
    $("#divResearchItems").css({ left: position.left, top: position.top });

    $("#divMenuResearchItems").hover(function() {
        if (inprog == 0) {
            inprog = 1;
            $("#divResearchItems").slideDown();
        }
    }, function() {
        $("#divResearchItems").slideUp(400, function() { inprog = 0; });
    });

    //small menus - AddItems
    var position = $("#lnkAddItems").position();
    position.top += $("#lnkAddItems").height();
    position.left += $("#lnkAddItems").width() - $("#divAddItems").width();
    $("#divAddItems").css({ left: position.left, top: position.top });

    $("#divMenuAddItems").hover(function() {
        if (inprog == 0) {
            inprog = 1;
            $("#divAddItems").slideDown();
        }
    }, function() {
        $("#divAddItems").slideUp(400, function() { inprog = 0; });
    });

    //show random content in the left header box (Blogs/Articles/Downloads...)
    var overRandomContentDiv = false;
    var timer;
    timer = window.setInterval(function() {
        //if the user is not hover the div
        if (overRandomContentDiv == false) {
            var randomnumber = Math.floor(Math.random() * 4);

            //get all random content boxes
            var contentBoxes = $("[id^='divLatestContent_']");
            var currentlyVisibleBox = contentBoxes.index($("[id^='divLatestContent_']:visible"));

            //hide current boxes and show new content box (only if different from current box)
            if (currentlyVisibleBox != randomnumber) {
                contentBoxes.hide();
                contentBoxes.eq(randomnumber).fadeIn('slow');
            }
        }
    }, 5000);

    //overRandomContentDiv = whether the user is hovering the RandomContent div
    $("#divRandomContent").hover(
    function() {
        overRandomContentDiv = true;
    },
    function() {
        overRandomContentDiv = false;
    });

    //Forum Posts (עכשיו בפורומים) - changing the currently show post
    var current = 1;
    var overForumPostsDiv = false;
    var timerForum;
    timerForum = window.setInterval(function() {
        //if the user is not hover the div
        if (overForumPostsDiv == false) {
            var itemsCount = $('#divForumPosts').attr('items');
            var posts = $(".divForumPost");

            //hide current post and show the new post
            posts.hide();
            posts.eq(current).show('drop');

            //increase the counter
            current++;
            if (current >= itemsCount) {
                current = 0;
            }
        }
    }, 5000);

    $(".divForumPost:first").show();

    //overForumPostsDiv = whether the user is hovering the ForumPosts div
    $("#divForumPosts").hover(
    function() {
        overForumPostsDiv = true;
    },
    function() {
        overForumPostsDiv = false;
    });

    //*******MasterPage scripts*******
});

