function showObjectById(ID) {

	var obj = null;

	if (document.getElementById) {
	
		obj = document.getElementById(ID);
		obj.style.visibility = 'visible';
	}
	
	return obj;

}

function showObject(obj) {

	obj.style.visibility = 'visible';

	return obj;

}

function showObject(obj) {

	if (obj != null) {

		obj.style.visibility = 'visible';
	
	}
	
	return obj;

}

function hideObjectById(ID) {

	var obj = null;

	if (document.getElementById) {
	
		obj = document.getElementById(ID);
		obj.style.visibility = 'hidden';
		
	}
	
	return obj;

}

function hideObject(obj) {

	if (obj != null) {

		obj.style.visibility = 'hidden';
	
	}

	return obj;

}

function getObjectById(ID) {

	var obj = null;

	if (document.getElementById) {
	
		obj = document.getElementById(ID);
		
	}
	
	return obj;

}

function swapImageById(ID, imageUrl)
{

	var obj = document.getElementById(ID);
	
	if(obj != null && obj.src)
	{
	
		obj.src = imageUrl;
	
	}

}

// variables
var _dropdownMenuTag;

function initDropdownMenu(menuId, buttonTag, dropdownMenuTag)
{

	// set dropdown menu tag
	_dropdownMenuTag = dropdownMenuTag;

	var objButtonCollection = document.getElementById(menuId).getElementsByTagName(buttonTag);        
	for(var i = 0; i < objButtonCollection.length; i++)
	{        
		var objButton = objButtonCollection[i];        
		for(var j = 0; j < objButton.childNodes.length; j++)
		{
			if(objButton.childNodes.item(j).nodeName == dropdownMenuTag)
			{
				objButton.onmouseover=showDropdownMenu;
				objButton.onmouseout=hideDropdownMenu;
			}
		}
	}
}

function showDropdownMenu()
{
	var objThis = this;	

	for(var i = 0; i < objThis.childNodes.length; i++)
	{
		if(objThis.childNodes.item(i).nodeName == _dropdownMenuTag)			
		{							
			objThis.childNodes.item(i).style.display = "block";
			break;            
		}		
	}

}

function hideDropdownMenu()
{								
	var objThis = this;	
	
	for(var i = 0; i  < objThis.childNodes.length; i++)			
	{
		if(objThis.childNodes.item(i).nodeName == _dropdownMenuTag)
		{				
			objThis.childNodes.item(i).style.display = "none";			
			break;
		}			
	}	
}

function openWindowOnChange(reset, ignoreIndex)
{

	var list = event.srcElement;
	
	if(list.selectedIndex != ignoreIndex && list.options.item(list.selectedIndex).value != "")
	{
	
		eval("window.open(" + list.options.item(list.selectedIndex).value + ")");
	
	}
	
	if(reset)
	{
	
		list.selectedIndex = 0;
	
	}

}

function goToUrlOnChange(defaultUrl, urlBase, defaultStartIndex, defaultEndIndex)
{

	var list = event.srcElement;
	
	if(list.selectedIndex >= defaultStartIndex && list.selectedIndex <= defaultEndIndex)
	{
	
		window.location = defaultUrl;
	
	}
	else
	{
	
		window.location = urlBase + list.options.item(list.selectedIndex).value;
	
	}

}

document.observe("dom:loaded", function() {

    $$("div.highlights").each(function(container) {

        var dur = null;
        var classNames = container.className.split(" ");
        for (var i = 0; i < classNames.length; i++) {
            if (classNames[i].search(/duration-[0-9]+$/i) >= 0) {
                dur = parseInt(classNames[i].substr(9));
                break;
            }
        }

        var list = container.down("ul");
        var ads = list.select("li");
        var count = ads.length;
        if (count <= 1) {
            return;
        }

        var startIndex = 0;
        for (var i = 0; i < count; i++) {
            if (ads[i].visible()) {
                startIndex = i;
                break;
            }
        }

        var getHeight = function(item) {
            return item.offsetHeight;
        };
        list.setStyle({ "height": ads.max(getHeight) + "px" }).makePositioned();
        var top = ads[0].positionedOffset().top;
        ads.invoke("hide");

        var util = {
            selectedIndex: -1,
            duration: dur || 12,
            navItems: [],
            lastTimeout: null,
            nextItem: function() {
                util.selectItemByIndex(util.selectedIndex + 1);
            },
            previousItem: function() {
                util.selectItemByIndex(util.selectedIndex - 1);
            },
            selectItemByOffset: function(offset) {
                util.selectItemByIndex(util.selectedIndex + offset);
            },
            selectItemByIndex: function(index, noFade) {

                if (util.lastTimeout != null) {
                    window.clearTimeout(util.lastTimeout);
                    util.lastTimeout = null;
                }
                // schedule next one
                util.lastTimeout = util.nextItem.delay(util.duration);

                var previousIndex = util.selectedIndex;
                if (!index && index != 0) {
                    index = util.selectedIndex + 1;
                }
                if (index < 0) {
                    index = count - 1;
                }
                if (index >= count) {
                    index = 0;
                }
                if (index == previousIndex) {
                    return;
                }
                util.navItems.invoke("removeClassName", "selected");
                util.navItems[index].addClassName("selected");
                if (window.Effect && !noFade) {
                    if (ads[previousIndex]) {
                        ads[previousIndex].style.position = "absolute";
                        ads[previousIndex].style.top = top + "px";
                        Effect.Fade(ads[previousIndex]);
                    }
                    ads[index].style.top = "";
                    ads[index].style.position = "relative";
                    Effect.Appear(ads[index]);

                } else {
                    ads[index].show();
                    if (ads[previousIndex]) {
                        ads[previousIndex].hide();
                    }
                }
                util.selectedIndex = index;

            }
        };

        var itemNav = new Element("ul").addClassName("highlights-item-menu");
        for (var i = 0, len = count; i < len; i++) {
            var height = ads[i].offsetHeight
            var navItem = new Element("li");
            var navAnchor = new Element("button", { type: "button" }).update(i + 1);
            navItem.insert(navAnchor.observe("click", function(clickEvent, index) {
                clickEvent.preventDefault();
                util.selectItemByIndex(index);
            } .bindAsEventListener(navAnchor, i)));
            itemNav.insert(navItem);
            util.navItems.push(navItem);
        }

        var nextPrev = [-1, 1];
        //var nextPrevNav = new Element("ul").addClassName("highlights-direction-menu");
        for (var i = 0, len = nextPrev.length; i < len; i++) {
            var navItem = new Element("li").addClassName(nextPrev[i] < 0 ? "prev previous" : "next");
            var navAnchor = new Element("button", { type: "button" }).update(nextPrev[i] < 0 ? "Previous" : "Next");
            navItem.insert(navAnchor.observe("click", function(clickEvent, offset) {
                clickEvent.preventDefault();
                util.selectItemByOffset(offset);
            } .bindAsEventListener(navAnchor, nextPrev[i])));
            itemNav.insert(nextPrev[i] < 0 ? { top: navItem} : navItem);

        }

        list.insert({ after: itemNav });

        //show random

        //util.selectItemByIndex(startIndex, true);
        util.selectItemByIndex(0, true);

    });

});
