﻿// If you don't understand the JavaScript, visit http://jquery.com

window.onload = function()
    {
        OpenMenuAndMakeSiteMap();
        FitPage();
    }

// Fits the height of the pafe (dropping down the footer to the bottom of the page)
function FitPage()
{
    var height;
    // Expanding the page height
    if (document.documentElement.clientHeight > document.body.clientHeight)
    {
        height = document.documentElement.clientHeight;
    }
    else
    {
        height = document.body.clientHeight + 20;
    }
    height -= ($("#Header").height() + $("#Footer").height() + 10); // The height of the page without header, footer and body margin
    $("#VerticalHR").css("height",height); // Setting the height of vertival hr at the MasterPage.master
    $("#ProductHR").css("height",height - 45); // Setting the height of vertival hr at the Product.aspx
    $("#HomeHR").css("height",height - 78 - $("#HomeTop").height()); // Setting the height of vertival hr at the HomePage.aspx
}

// Trims an string. Example: alert("*"+myString.trim()+"*");
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

// Expanding the current's page menu
function OpenMenuAndMakeSiteMap()
{
    var SiteMap = "";
    var sPath = document.URL;
    sPath = sPath.replace("#","");
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    
    var HomeNode = $("#SiteMapPathHomePage").html();
    var Seperator = $("#SiteMapPathSeperator").html();
    
    // Going over all the menus
    $(".MenuItem span").each(
        function()
        {
            if($(this).parent().parent().attr("href") == sPage)
            {   
                SiteMap+=Seperator;
                SiteMap+=$(this).html();
            }
        }
    );
    
    // Going over all the SubMenus
    $(".SubMenuItem a").each(
        function()
        {
            if($(this).attr("href") == sPage)
            {
                $(this).css("color", "Orange");
                $(this).parent().parent().css("display","block");
                prevId = $(this).parent().parent().attr("id"); // Setting this menu item as the prev expanded menu, so it will be closed when another opened
                
                var text = $(this).parents(".MenuItemsSet").children(".MenuItem span").find("span").html();
                var link = $(this).parents(".MenuItemsSet").children(".MenuItem span").attr("href");
                
                // Adding the MenuItem
                SiteMap+=Seperator;
                SiteMap+= "<a href="+link+">"+text+"</a>"
                
                // Adding the SubMenuItem
                SiteMap+=Seperator;
                SiteMap+=$(this).html();
            }
        }
    );
    
    // If SiteMap is not empty, we overrifde the .NET's SiteMapPath
    if(SiteMap != "")
    {
        // Injecting the SiteMap to it's place
        $("#SiteMapPath").html(HomeNode+SiteMap);
    }
}

// Allows only numbers to be entered
function noChars(e)
{
    var keynum;
    if(window.event) // IE
    {
      keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
      keynum = e.which;
    }
    return((keynum>=48&&keynum<=57) || (keynum==45) || (keynum==8)) // 45: "-" 8:"BackSpave"
}

// Allows only chars to be entered
function noNumbers(e)
{
    var keynum;
    if(window.event) // IE
    {
      keynum = e.keyCode;
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
      keynum = e.which;
    }
    return !(keynum>=48&&keynum<=57);
}

var prevId;
// Expands toggle an div with specified id 
function SlideDown(id) 
{
    // Closing the prev menu, if it is not the current menu, and if it is opened
    if (($("#"+prevId).attr("id") != id) && ($("#"+prevId).css("display") == "block"))
    {
        $("#"+prevId).slideUp("slow", "easeout");
    }
    $("#"+id).slideToggle("slow", "easein");
    prevId = id;
}

var prevDivId;
var prevImg1Id;
var prevImg2Id;
// Slides div down and up, and changes the image
function SlideDivToggle(divId, img1Id, img2Id)
{
    // Closing the prev div, if it is not the current menu, and if it is opened
    if (($("#"+prevDivId).attr("id") != divId) && ($("#"+prevDivId).css("display") == "block"))
    {
        $("#"+prevImg1Id).toggle();
        $("#"+prevImg2Id).toggle();
        $("#"+prevDivId).slideUp("slow", "easeout");
    }
    $("#"+img1Id).toggle();
    $("#"+img2Id).toggle();
    $("#"+divId).slideToggle("slow", "easein");
    
    prevDivId = divId;
    prevImg1Id = img1Id;
    prevImg2Id = img2Id;
    
    /*$("#"+img1Id).toggle();
    $("#"+img2Id).toggle();
    $("#"+divId).slideToggle("slow", "easein");*/
}

//================= SideMenu.ascx =================\\
function ShowPopUp(PopUpId)
{
    var width  = $("#"+PopUpId).width();
    var height = $("#"+PopUpId).height();
    var left   = (screen.width  - width)/2;
    var top    = (screen.height - height * 2)/2;
    
    $("#"+PopUpId).css("left", left);
    $("#"+PopUpId).css("top", top);
    $("#Background").css("height", document.body.clientHeight + 10);

    $("#Background").css('opacity',0).fadeTo("slow", 0.60, function(){
            $("#"+PopUpId).fadeIn("slow");
        });
}

function HidePopUp()
{
    $(".PopUp").each(function(){
        if ($(this).css("display")=="block")
        {
            $(this).fadeOut("slow", function(){$("#Background").fadeOut("slow");});
        }
    })
}
//================= SideMenu.ascx =================\\

function SlideToggle(id)
{
    $("#"+id).slideToggle("slow", "easein");
}