﻿function $(id)
{
    if (document.getElementById)
		return document.getElementById(id);
	else if (document.all)
		return document.all(id);	
}

var System = new Object();

if (navigator.userAgent.indexOf('MSIE')!=-1)
{
    System.IE = true;
}
else if ((typeof(DOMParser)=="undefined")||(navigator.userAgent.toLowerCase().indexOf("safari")!=-1))
{
    System.SF = true;
}
else
{
    System.MZ = true;
}

System.GetScrollLeft = function()
{
    if(document.documentElement.scrollLeft)
    {
        return document.documentElement.scrollLeft;
    }    
    else if(document.body.scrollLeft)
    {
        return document.body.scrollLeft;
    }
    return 0;
}

System.GetScrollTop = function()
{
    if(document.documentElement.scrollTop)
    {
        return document.documentElement.scrollTop;
    }    
    else if(document.body.scrollTop)
    {
        return document.body.scrollTop;
    }
    return 0;
}

System.GetClientWidth = function()
{
    if(window.innerWidth)
    {
        return System.SF ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth);
    }
    else
    {
        return document.documentElement.clientWidth;
    }
}

System.GetClientHeight = function()
{
    if(window.innerHeight)
    {        
        return System.SF ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight);
    }
    else
    {
         return document.documentElement.clientHeight;
    }
}


System.AddEventListener = function(obj, eventname, func)
{
    if(obj.addEventListener)
    {
        obj.addEventListener(eventname, func, true);
    }
    else if(obj.attachEvent)
    {
        obj.attachEvent("on"+eventname, func);
    }
}

System.RemoveEventListener = function(obj, eventname, func)
{
    if(obj.removeEventListener)
    {
        obj.removeEventListener(eventname, func, true);
    }
    else if(obj.detachEvent)
    {
        obj.detachEvent("on"+eventname, func);
    }
}

System.StopEventBubble = function(e)
{
    if(e.stopPropagation)
    {
        e.stopPropagation();
    }
    else
    {
        e.cancelBubble = true;
    }
}

System.CancelEventDefault = function(e)
{
    if(e.preventDefault)
    {
        e.preventDefault();
    }
    else
    {
        e.returnValue = false;
    }
}


System.SetOpacity = function(ctrl, opacity)  //opacity为0-100
{
    if(System.IE)
    {                            
        if(ctrl.filters)
        {            
            if(ctrl.filters.alpha)
            {
                ctrl.filters.alpha.opacity = opacity;        
            }
            else
            {
                ctrl.style.filter = "alpha(opacity=" + opacity + ")";
            }
        }
    }
    else
    {
        ctrl.style.MozOpacity = opacity / 100.0;
    }
}

System.Status = function(text)
{
    window.status = text;
}

function Menu()
{
    
    document.write("<div class='comparewindow' id='comparewindow'><div class='comparewindowtitle'>专业比较</div><div id='compareitems' class='compareitems'></div><div class='comparefooter'><input style='width:60px;' type='button' value='比较' onclick='Compare();'/></div><div>");
    this.Panel = $("compareitems");
    this.Panel.menu = this;  
}


Menu.prototype.Add = function(ID, Text)
{
    var item = document.createElement("div");      
    this.Panel.appendChild(item); 
    
    item.style.width="100%";  
    item.id = ID;
    item.name = ID;    
    item.innerHTML = Text;       
}

Menu.prototype.Delete = function(ID)
{
    var i;
    for(i = this.Panel.childNodes.length - 1; i >= 0; i--)
    {
        if(this.Panel.childNodes[i].id == ID)
        {
            this.Panel.removeChild(this.Panel.childNodes[i]);
            break;
        }
    }
}


Menu.prototype.Clear = function()
{
    var i;
    for(i = this.Panel.childNodes.length - 1; i >= 0; i--) 
    {
        this.Panel.removeChild(this.Panel.childNodes[i]);
    }
}



// JScript File

function FavCategories(){;}

FavCategories.cookiename = "CompareMajors";

FavCategories.Count = function()
{
    var value="", categories;
    value = FavCategories.GetFavCategoriesCookie();
    categories = value.split("$");
    return Math.floor(categories.length / 2);
}

FavCategories.AddFavCategory = function(id, title)
{    
    var i, value="";
    value = FavCategories.GetFavCategoriesCookie();
    if(value.indexOf(id+"$") == -1)
    {
        var categories = value.split("$");
        if(Math.floor(categories.length / 2) < 5)
        {        
            if(value.length != "")
            {
                value += "$" + id + "$" + title;                
            }
            else
            {
                value += id + "$" + title;                
            }
            FavCategories.SetFavCategoriesCookie(value);
            FavCategories.UpdateMenu(null);
        }
        else
        {
            alert("只能添加5个专业");
        }
    }
    else
    {
        alert("已经添加过此专业");
    }
}

FavCategories.DelFavCategory = function(id)
{
    var i, value = "";
    var categories = FavCategories.GetFavCategoriesCookie().split("$");
    if(categories.length > 1)
    {
        for(i = 0; i < categories.length; i+=2)
        {
            if(categories[i] != id)
            {            
                value += categories[i] + "$" + categories[i+1] + "$";
            }
        }
        value = value.substr(0, value.length-1);  //删除最后一个$
        FavCategories.SetFavCategoriesCookie(value);
        FavCategories.UpdateMenu(null);
    }
}

FavCategories.UpdateMenu = function(menu)
{
    menu = menu == null ? FavCategories.Menu : menu;
    if(menu)
    {
        FavCategories.Menu = menu;
        menu.Clear();
        var i, code = "";
        var categories = FavCategories.GetFavCategoriesCookie().split("$");
        if(categories.length > 1)
        {                        
            for(i = 0; i < categories.length; i+=2)
            {
                var text =  "<span style='float:left;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;width:180px;height:25px;line-height:25px;'>" + (Math.floor(i/2)+1).toString() + ". " + categories[i+1] + "</span><span style='float:right'><a href='javascript:FavCategories.DelFavCategory(\"" + categories[i] + "\");'><font color='red'>×</font></a></span>";;
                menu.Add("item"+categories[i], text);                
            }            
        }            
              
    }
}

FavCategories.GetFavCategoriesCookie = function()
{    
    var i;
    var cookies = document.cookie.split(";");
    for(i = 0; i < cookies.length; i++)
    {
        var p = cookies[i].indexOf(FavCategories.cookiename);
        if(p >= 0)
        {                     
            return decodeURI(cookies[i].substr(FavCategories.cookiename.length + 1));            
        }
    }
    return "";
}

FavCategories.SetFavCategoriesCookie = function(value)
{ 
    var exp = new Date();
    var tenyears = exp.getTime() + (10 * 365 * 24 * 60 * 60 * 1000);  //10 years
    exp.setTime(tenyears);
    document.cookie = FavCategories.cookiename + "=" + encodeURI(value) + ";expires=" + exp.toGMTString();
}

function MoveMenu()
{
    $("comparewindow").style.left = (System.GetClientWidth() - 210) + "px";
    $("comparewindow").style.top = (System.GetScrollTop() + (System.GetClientHeight() - 150) / 2) + "px";
    setTimeout(MoveMenu, 50);
}

function Compare()
{
    if(FavCategories.Count() < 2)
    {
        alert("添加至少2个专业才能进行比较");
    }
    else
    {
        window.location = "MajorCompare.aspx";
    }
}

var favmenu = new Menu();
FavCategories.UpdateMenu(favmenu);
MoveMenu();


