$ = function() {
	var elements = [];

	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		
		if (typeof element == "string") {
			element = document.getElementById(element);
		}
		
		if (arguments.length == 1) {
			return element;
		}	
 
		elements.push(element);
	}
	
	return elements;
}

$.TagName = function(obj, tagName) {
	return obj.getElementsByTagName(tagName.toUpperCase());
}

Cookie = {
	set: function(key, value, params) {
		var c = [key + "=" + escape(value)];
	
		for (var i in params) {
			if (i == "expires") {
				var d = new Date();
				
				d.setTime(d.getTime() + (86400000 * params[i]));
				
				params[i] = d.toGMTString();
			}
			
			c.push(i + "=" + params[i]);
		}
		
		document.cookie = c.join(";");
	},
	get: function(key) {
		document.cookie.match(new RegExp(key + "=([^;]*);?", "gi"));
		
		return unescape(RegExp.$1);
	},
	erase: function(key) {
		this.set(key, "", {expires: -1});
	}
}

Event = {
	guid: 1,
	key: function(e) {
		return e.keyCode;
	},

	element: function(e) {
		return e.target || e.srcElement;
	},
	add: function(obj, type, func) {
		if (window.addEventListener) {
			obj.addEventListener(type, func, false);
		} else {
			if (!func.$$guid) func.$$guid = this.guid++;
			if (!obj.events) obj.events = {};
			
			var funcs = obj.events[type];
			
			if (!funcs) {
				funcs = obj.events[type] = {};

				if (obj["on" + type]) {
					funcs[0] = obj["on" + type];
				}
			}
			
			funcs[func.$$guid] = func;
			obj["on" + type] = this.handle;
		}
	},
	remove: function(obj, type, func) {
		if (window.removeEventListener) {
			obj.removeEventListener(type, func, true);
		} else {
			if (obj.events && obj.events[type]) {
				delete obj.events[type][func.$$guid];
			}
		}
	},
	get: function(e) {
		if (!e) {
			e = ((this.ownerDocument || this.document || this).parentWindow || window).event;
		}
		
		this.fix(e);
		
		return e;
	},
	stop: function(e) {
		e.preventDefault();
		e.stopPropagation();
	},
	handle: function(e) {
		var funcs;
		var returnValue = true;
		
		e = Event.get(e);
		funcs = this.events[e.type];

		for (var i in funcs) {
			this.$$handleEvent = funcs[i];
			
			if (this.$$handleEvent(e) === false) {
				returnValue = false;
			}
		}
		
		return returnValue;
	},
	fix: function(e) {
		if (!e.preventDefault) {
			e.preventDefault = this.preventDefault;
		}
		
		if (!e.stopPropagation) {
			e.stopPropagation = this.stopPropagation;
		}
	},
	preventDefault: function() {
		this.returnValue = false;
	},
	stopPropagation: function() {
		this.cancelBubble = true;
	}
}

View = {
	replace: function() {
		var view = $("view");
		
		if (view) {
			var images = $.TagName(view, "img");
			
			if (images.length > 0) {
				var current = [
					"/icons/expand.gif",
					"/icons/collapse.gif"
				];
				
				var replacement = [
					{src: "/wps/themes/html/Assind/images/expand.gif", alt: "Expand", className: "expand"},
					{src: "/wps/themes/html/Assind/images/collapse.gif", alt: "Collapse", className: "collapse"}
				];
		
				for (var i=0; i < images.length; i++) {
					for (var j=0; j < current.length; j++) {
						if (images[i].src.indexOf(current[j]) > -1) {
							for (var property in replacement[j]) {
								images[i][property] = replacement[j][property];
							}
						}
					}
				}
			}
		}
	}
}

var menu = {
    levels: [],
    hideTimeout: null,
    
    start: function() {                
        var objHead = document.getElementById("wpsHead");
        
        if (objHead != null) {
            objHead.onmouseout = new Function("menu.hideOnTimeout()");
        }
    },
    verifyId: function(id) {
        var found = false;
        
        for (var i = 0;i < this.levels.length; i++) {
            if (id == this.levels[i]) found = true;
        }
        
        if (!found) this.levels[this.levels.length] = id;
    },
    show: function(id) {
        this.verifyId(id);
    
        this.hideAll();
        
        this.showHide(id, "block");
    },
    showHide: function(id, newProperty) {
        var obj = document.getElementById(id);
            
        if (obj != null) {
            if (obj.style.display != newProperty) obj.style.display = newProperty;
        }
    },
    hideOnTimeout: function(clear) {
        if (this.hideTimeout) window.clearTimeout(this.hideTimeout);
        
        if (!clear) this.hideTimeout = window.setTimeout("menu.hideAll()", 5000);
    },
    hideAll: function(clear) {
        for (var i = 0;i < this.levels.length; i++) {
            this.showHide(this.levels[i], "none");
        }
        
        if (clear) this.hideOnTimeout(clear);
    }
}
var sliding;

function slideSections(e, obj) {
	Event.get(e);
	Event.stop(e);
	
    var cL = document.getElementById("channels");
    
    if (cL != null) {
        if (!cL.style.display || cL.style.display == "none") {
            var x = screenFindPosX(obj);
            var y = screenFindPosY(obj) + obj.offsetHeight;
            
            cL.style.top = y + "px";
            cL.style.left = x + "px";
            cL.style.height = "0px";
            
            sliding = window.setInterval("slideMe()", 15);
            
            obj.className = "wpsSelectedLink";
        } else {
            window.clearInterval(sliding);
            
            cL.style.display = "none";
            
            obj.className = "wpsUnSelectedLink";
        }
    }
}

function slideMe() {
    var obj = document.getElementById("channels");
    
    if (obj) {
        var currHeight = parseInt(obj.style.height);
        
        if (currHeight <= 470) {
            if (obj.style.display != "block") obj.style.display = "block";
            
            obj.style.height = (currHeight + 10) + "px";
        } else {
            window.clearInterval(sliding);
        }
        
    }
}

function screenFindPosX(obj) {
    var currLeft = 0;
    
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            currLeft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x) currLeft += obj.x;
    
    return currLeft;
}

function screenFindPosY(obj) {
    var currTop = 0;
    
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            currTop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y) currTop += obj.y;
    
    return currTop;
}

function openGuritel(section) {
	var baseLink = "http://www.assind.vi.it/guritel/index.html";

	window.open(baseLink + "?" + section, "guritel", "scrollbars");
}

function switchLanguage(lang, url) {
	if (Cookie.get("lang") != lang) {
		Cookie.set("lang", lang, {path: "/"});
		
		if (url && url != location.href) {
			location.href = url;
		} else {
			location.reload();
		}
	}
}

function parseLanguage(lang) {
	var newURL = location.href.replace(/(lang=)[a-z]{2}/i, "$1" + lang);
	
	if (newURL != location.href) {
		location.href = newURL;
	} else if (!new RegExp("lang=" + lang, "i").test(location.href)) {
		location.href = location.pathname + location.search + (location.search == "" ? "?" : "&") + "lang=" + lang;
	}
}









				
