﻿var animationData = [];
animationData["faces"] = {}; animationData["logo"] = {}; animationData["dots"] = {};

/* Animation Data. */
animationData["faces"].rate = 0.125;
animationData["faces"].interval = 100;
animationData["faces"].delay = 2400;

animationData["logo"].rateHide = 20;
animationData["logo"].intervalHide = 100;
animationData["logo"].rateShow = 20;
animationData["logo"].intervalShow = 100;
animationData["logo"].hideDelay = 1000;
animationData["logo"].showDelay = 2000;
animationData["logo"].dotDelay = 1000;
animationData["logo"].initialDelay = 3000;

animationData["dots"].rate = 5;
animationData["dots"].interval = 35;


function animate(object, animation, interval, onComplete) {
	var done = true;
    
	for (i in animation) {
		if (animation.hasOwnProperty(i) && i !== "id" && i !== "hasClipping") {
			// Set initial conditions once.
			var start = animation[i].start;
			var end = parseFloat(animation[i].end);
			var rate = parseFloat(animation[i].rate);
			var styleValue;
			
			if (!animation[i].isInitialized) {
				if (i === "opacity") {
					setOpacity(object, start);
				}
				else {
                    if (i.search(/^clip.*/) === -1)
                    {
                        object.style[i] = start;
                    }
                    else
                    {
                        animation.hasClipping = true;
                        animation[i].currentValue = parseFloat(animation[i].start);
                    }
					if (!animation[i].units) {
						animation[i].units = start.match(/[^\s0-9].*/);
					}
				}
				animation[i].isInitialized = true;
			}
			
			if (i === "opacity") {
				styleValue = getOpacity(object);
			}
			else {
                if (i.search(/^clip.*/) === -1)
                {
                    styleValue = parseFloat(object.style[i]);
                }
                else
                {
                    styleValue = animation[i].currentValue;
                }
			}
			
			styleValue += rate;
			
			if ((rate > 0 && styleValue <= end) || (rate < 0 && styleValue >= end))	{	
				if (i === "opacity") {
					setOpacity(object, styleValue);
				} else {
                    if (i.search(/^clip.*/) === -1)
                    {
                        object.style[i] = (styleValue) + animation[i].units;
                    }
                    else
                    {
                        animation[i].currentValue = styleValue;
                    }
				}
				done = false;
			}
			else {
				if (i === "opacity") {
					setOpacity(object, end);
				}
				else {
                    if (i.search(/^clip.*/) === -1)
                    {
                        object.style[i] = end + animation[i].units;
                    }
                    else
                    {
                        animation[i].currentValue = end;
                    }
				}
				// Loop or rewind... call onComplete?
				if (animation[i].loop) {
					animation[i].isInitialized = false;
					done = false;
				}
				if (animation[i].reverse) {
					animation[i].start = end;
					animation[i].end = start;
					animation[i].rate = -1 * parseFloat(rate);
					animation[i].isInitialized = false;
					if (!animation[i].loop)	{
						animation[i].reverse = false;
					}
					done = false;
				}
			}
            //console.log("property: %f. start: %f. end: %f. rate: %f. value: %f", i, start, end, rate, animation[i].currentValue);
        }
	}
    
    if (animation.hasClipping)
    {
        var clipTop = animation["clipTop"] ? animation["clipTop"].currentValue + animation["clipTop"].units : "auto";
        var clipRight = animation["clipRight"] ? animation["clipRight"].currentValue + animation["clipRight"].units : "auto";
        var clipBottom = animation["clipBottom"] ? animation["clipBottom"].currentValue + animation["clipBottom"].units : "auto";
        var clipLeft = animation["clipLeft"] ? animation["clipLeft"].currentValue + animation["clipLeft"].units : "auto";
        
        object.style.clip = "rect(" + clipTop + "," + clipRight + "," + clipBottom + "," + clipLeft + ")";
        //console.log( "rect(" + clipTop + "," + clipRight + "," + clipBottom + "," + clipLeft + ")");
    }
    
	if (!animation.id) {
		animation.id = setInterval(function () {animate(object, animation, interval, onComplete);}, interval);
	}
	if (done) {
		clearInterval(animation.id);
		if (onComplete) {
			onComplete(object, animation, interval);
		}
	}
	return animation.id;
}

function setOpacity(element, alpha) 
{
	var style = element.style;
	if (style.opacity !== undefined) {
		style.opacity = alpha;
	}
	else if (style.filter !== undefined) 
	{
        if (element.filters && element.filters.alpha && !isNaN(parseInt(element.filters.alpha.opacity,10)))
        {
            element.filters.alpha.opacity = (alpha * 100);
        }
        else
        {
            style.filter = "alpha(opacity=" + (alpha  * 100) + ")";
        }
	}
	else if (style.MozOpacity !== undefined) {
		style.MozOpacity = alpha;
	}
}

function getOpacity(element) {
	var style = element.style;
	if (style.opacity !== undefined) {
		return parseFloat(style.opacity);
	}
	if (style.filter !== undefined) {
        return parseFloat(element.filters.alpha.opacity) / 100;
	}
	if (style.MozOpacity !== undefined) {
		return parseFloat(style.MozOpacity);
	}
}

function init()
{
    if (document.body.id === "main") {	
        function animateL1(dot)	{
            animate(dot, {"left" : {"start" : "242px", "end" : "223px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL2);
        }
        function animateL2(dot)	{
            animate(dot, {"top" : {"start" : "80px", "end" : "124px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL4);
        }
        function animateL3(dot)	{
            animate(dot, {"left" : {"start" : "242px", "end" : "223px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL4);
        }
        function animateL4(dot)	{
            animate(dot, {"top" : {"start" : "124px", "end" : "169px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL6);
        }
        function animateL5(dot)	{
            animate(dot, {"left" : {"start" : "242px", "end" : "223px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL6);
        }		
        function animateL6(dot)	{
            animate(dot, {"left" : {"start" : "223px", "end" : "218px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL7);
        }
        function animateL7(dot)	{
            animate(dot, {"top" : {"start" : "169px", "end" : "198px", "rate" : 1.5 * animationData["dots"].rate},
                "left" : {"start" : "218px", "end" : "199px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL7b);
            function animateL7b() {
                animate(dot, {"top" : {"start" : "198px", "end" : "227px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL7c);
            }
            function animateL7c() {
                animate(dot, {"left" : {"start" : "199px", "end" : "191px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL7d);
            }
            function animateL7d() {
                animate(dot, {"top" : {"start" : "227px", "end" : "327px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, hideDot);
            }
        }
        function animateL8(dot) {
            animate(dot, {"left" : {"start" : "242px", "end" : "223px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL8b);
            function animateL8b() {
                animate(dot, {"top" : {"start" : "214px", "end" : "243px", "rate" : 1.5 * animationData["dots"].rate},
                    "left" : {"start" : "223px", "end" : "207px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL8c);
            }
            function animateL8c() {
                animate(dot, {"left" : {"start" : "207px", "end" : "198px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL8d);
            }
            function animateL8d() {
                animate(dot, {"top" : {"start" : "243px", "end" : "332px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, hideDot);
            }
        }
        function animateL9(dot) {
            animate(dot, {"left" : {"start" : "157px", "end" : "167px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL9b);
            function animateL9b() {
                animate(dot, {"top" : {"start" : "153px", "end" : "167px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL9c);
            }
            function animateL9c() {
                animate(dot, {"left" : {"start" : "167px", "end" : "184px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL9d);
            }
            function animateL9d() {
                animate(dot, {"top" : {"start" : "167px", "end" : "199px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL11);
            }
        }
        function animateL10(dot) {
            animate(dot, {"left" : {"start" : "173px", "end" : "184px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL11);
        }
        function animateL11(dot) {
            animate(dot, {"top" : {"start" : "190px", "end" : "321px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, hideDot);
        }
        function animateL12(dot) {
            animate(dot, {"left" : {"start" : "173px", "end" : "184px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, animateL13);
        }
        function animateL13(dot) {
            animate(dot, {"top" : {"start" : "218px", "end" : "321px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, hideDot);
        }
		
		function animateL14(dot) {
			animate (dot, {"left" : {"start" : "220px", "end" : "198px", "rate" : -animationData["dots"].rate}}, animationData["dots"].interval, animateL14b);
            function animateL14b() {
                animate(dot, {"top" : {"start" : "265px", "end" : "332px", "rate" : animationData["dots"].rate}}, animationData["dots"].interval, hideDot);
            }
		}
        
        function hideDot(dot) {
            dot.style.visibility = "hidden";
            dot.isAnimating = false;
        }
        
        var dotPositions = [];
        dotPositions["about_link"] = {"top" : "80px", "left" : "242px", "startAnimation" : animateL1};
        dotPositions["courses_link"] =  {"top" : "124px", "left" : "242px", "startAnimation" : animateL3};
        dotPositions["contact_link"] = {"top" : "169px", "left" : "242px", "startAnimation" : animateL5};
        dotPositions["clients_link"] = {"top" : "153px", "left" : "157px", "startAnimation" : animateL9};
        dotPositions["advice_link"] = {"top" : "214px", "left" : "242px", "startAnimation" : animateL8};
		dotPositions["gallery_link"] = {"top" : "190px", "left" : "173px", "startAnimation" : animateL10};
        dotPositions["forum_link"] = {"top" : "218px", "left" : "173px", "startAnimation" : animateL12};
		
        dotPositions["jobs_link"] = {"top" : "265px", "left" : "220px", "startAnimation" : animateL14};
		
        var nav_links = document.getElementById("navigation_links").getElementsByTagName("LI");
        
        for (var i = 0; i < nav_links.length; i++) {
            var id = nav_links[i].id;
            var a_link = nav_links[i].firstChild;
            while (!a_link && a_link.nodeName != "a") {
                a_link = a_link.nextSibling;
            }
            dot = document.createElement("IMG");
            dot.id = id + "_dot";
            dot.src = "images/dot.png";
            dot.className = "dot pngNoShow";
            document.getElementById("main-navigation").appendChild(dot);
                
            a_link.onmouseover = function(e) {
                if (!e) {
                    var e = window.event;
                }
                var id;
                if (e.target) {
                    id = e.target.parentNode.id;
                }
                else if (e.srcElement) {
                    id = e.srcElement.parentNode.id;
                }
                else {
                    return;
                }
                
                var dot = document.getElementById(id + "_dot");
                if (!dot.isAnimating) {
                    dot.isAnimating = true;
                    dot.style.top = dotPositions[id].top;
                    dot.style.left = dotPositions[id].left;
                    dot.style.visibility = "visible";
                    dotPositions[id].startAnimation(dot);
                }
            };
        }
    
		var faces1 = document.getElementById("faces1");
		var faces2 = document.getElementById("faces2");
		var faces3 = document.getElementById("faces3");
		
		if (faces2.style.opacity !== undefined || faces2.style.filter !== undefined || faces2.style.MozOpacity !== undefined)
		{
			faces2.style.visibility = "visible";
			faces3.style.visibility = "visible";
		}
		
		function anime1()
		{	
			animate(faces1, {"opacity" : {"start" : "1.0", "end" : "0", "rate" : -animationData["faces"].rate}}, animationData["faces"].interval);
			animate(faces2, {"opacity" : {"start" : "0", "end" : "0.99", "rate" : animationData["faces"].rate}}, animationData["faces"].interval, function () {setTimeout(anime2, animationData["faces"].delay)});
		};
		
		function anime2()
		{	
			animate(faces2, {"opacity" : {"start" : "1.0", "end" : "0", "rate" : -animationData["faces"].rate}}, animationData["faces"].interval);
			animate(faces3, {"opacity" : {"start" : "0", "end" : "0.99", "rate" : animationData["faces"].rate}}, animationData["faces"].interval, function () {setTimeout(anime3, animationData["faces"].delay)});
		};
		
		function anime3()
		{	
			animate(faces3, {"opacity" : {"start" : "1.0", "end" : "0", "rate" : -animationData["faces"].rate}}, animationData["faces"].interval);
			animate(faces1, {"opacity" : {"start" : "0", "end" : "0.99", "rate" : animationData["faces"].rate}}, animationData["faces"].interval, function () {setTimeout(anime1, animationData["faces"].delay)});
		};
		
		setTimeout(anime1, animationData["faces"].delay);
        
        function signScrollStart()
        {
            var sign = document.getElementById("sign_content");
            var signScrollTimeout;
            var signScroll = setInterval(function ()
            {    
                var firstQuote = sign.firstChild
                while (!firstQuote.className || firstQuote.className !== "quote")
                {
                    firstQuote = firstQuote.nextSibling;
                }
                if (sign.scrollTop > firstQuote.offsetHeight)
                {
                    sign.appendChild(firstQuote);
                    sign.scrollTop = 1;
                    clearInterval(signScroll);
                    signScrollTimeout = setTimeout(signScrollStart, 1000);
                }
                sign.scrollTop += 1;
            }, 100);
            sign.onmouseover = function () {clearInterval(signScroll);clearTimeout(signScrollTimeout);}
            sign.onmouseout = function () {signScrollStart();}
        }
        if (document.getElementById("sign_content"))
        {
            signScrollStart();
        }
		
		var hidden = getElementsByClassName("hidden");
		for (var i = 0; i < hidden.length; i++)
		{
			var hiddenTitle = hidden[i].previousSibling;
			while (hiddenTitle.nodeType != "1")
			{
				hiddenTitle = hiddenTitle.previousSibling;
			}
			hiddenTitle.className = "arrow";
			hiddenTitle.onmouseover = function() {this.style.color = "#53acdd";}
			hiddenTitle.onmouseout = function() {this.style.color = "#606160";}
				
			hiddenTitle.onmouseup = function()
			{
				var titleTopOffsetElement = this;
				var titleTop = this.offsetTop;
				while (titleTopOffsetElement.offsetParent.id != "content")
				{
					titleTopOffsetElement = titleTopOffsetElement.offsetParent;
					titleTop += titleTopOffsetElement.offsetTop;
				}
				
				titleTop = titleTop + 16 + "px";
				//alert(titleTop);
				
				var element = this.nextSibling;
				while (element.nodeType != "1")
				{
					element = element.nextSibling;
				}
				animate(element, 
					{
						"clipTop" : {"start": titleTop, "end" : "0", "rate" :"-50"},
						"clipBottom" : {"start": titleTop, "end" : "310", "rate" :"50"},
						"clipLeft" : {"start" : "450px", "end" : "0", "rate" : "-90"},
						"opacity" : {"start" : "0", "end" : "1", "rate" : "0.2"}
					}, 50, function(element) {if (element.style.filter !== undefined) element.style.removeAttribute('filter');}
				);
				element.style.display = "block";
				
				var closeElement = document.createElement("p");
				closeElement.className = "closeLink";
				closeElement.appendChild(document.createTextNode("x סגור"));
				element.parentNode.appendChild(closeElement);
				
				closeElement.onmouseup = function()
				{
					this.parentNode.removeChild(this);
					animate(element, 
						{
							"clipTop" : {"start": "0px", "end" : titleTop, "rate" :"50"},
							"clipBottom" : {"start": "310px", "end" : titleTop, "rate" :"-50"},
							"clipLeft" : {"start" : "0px", "end" : "450px", "rate" : "90"},
							"opacity" : {"start" : "1", "end" : "0", "rate" : "-0.2"}
						}, 50, function(element) {element.style.display = "none";}
					);
				}
			}
		}
	} // if "main"
	
    
    if (document.body.id === "courses") {
        var ulTags = document.getElementsByTagName("UL");
        for (var i = 0; i < ulTags.length; i++)
        {
            if (ulTags[i].className && ulTags[i].className === "course-tree")
            {
                var courseTree = ulTags[i];
                
                function collapseSubTrees(tree)
                {
                    var childNodes = tree.childNodes;
                    for (var j = 0; j < childNodes.length; j++)
                    {
                        var child = childNodes[j];
                        collapseSubTrees(child);
                        if (child && child.tagName && child.tagName.toLowerCase() === "ul")
                        {
                            child.parentNode.getElementsByTagName("A")[0].style.background = "url('images/tree_collapsed.gif') right center no-repeat";
                            child.parentNode.getElementsByTagName("A")[0].style.paddingRight = "12px";
                            child._originalHeight = child.offsetHeight;
                            child.style.height = "0";
                            child._collapsed = true;
                        }
                    }
                }
                collapseSubTrees(courseTree);
               
                function collapseTree(subTree)
                {
                    clearTimeout(subTree.animationId);
                    subTree._originalHeight = subTree.offsetHeight;
                    subTree.animationId = animate(subTree, 
                    {"height" : 
                        {
                            "start" : subTree.offsetHeight + "px", 
                            "end" : "0px", 
                            "rate" : "-10"
                        },
                        "opacity" :
                        {
                            "start" : "1.0", "end" : "0.0", "rate" : "-0.1"
                        }
                    }, 25);
                    subTree._collapsed = true;
                    subTree.parentNode.getElementsByTagName("A")[0].style.backgroundImage = "url('images/tree_collapsed.gif')";
                }
                
                function expandTree(subTree)
                {
                    clearTimeout(subTree.animationId);
                    subTree.animationId = animate(subTree, 
                    {"height" : 
                        {
                            "start" : subTree.offsetHeight + "px", 
                            "end" : subTree._originalHeight, 
                            "rate" : "10"
                        },
                        "opacity" :
                        {
                            "start" : "0.0", "end" : "1.0", "rate" : "0.1"
                        }
                    }, 25, 
                    function(subTree) 
                    {
                        subTree.style.height = "auto";
                    });
                    subTree._collapsed = false;
                    subTree.parentNode.getElementsByTagName("A")[0].style.backgroundImage = "url('images/tree_expanded.gif')";
                }
               
                courseTree.onclick = function(e)
                {
                    if (!e) {
                        var e = window.event;
                    }
                    if (e.target) {
                        element = e.target;
                    }
                    else if (e.srcElement) {
                        element = e.srcElement;
                    }
                    else {
                        return;
                    }
                    //Go up the dom tree find the damn LI.
                    while (element !== courseTree)
                    {
                        if (element.tagName.toLowerCase() === "li")
                        {
                            var subTree = element.getElementsByTagName("UL")[0];
                            if (subTree)
                            {
                                if (subTree._collapsed)
                                {
                                    if (element.parentNode._expandedChild)
                                    {
                                        collapseTree(element.parentNode._expandedChild);
                                    }
                                    expandTree(subTree);
                                    element.parentNode._expandedChild = subTree;
                                }
                                else
                                {
                                    collapseTree(subTree);
                                    element.parentNode._expandedChild = null;
                                }
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        }
                        element = element.parentNode;
                    }
                }
            }
        }
    } // if "courses"
    
    var logo1 = document.getElementById("logo-img");
    var logo2 = document.createElement("DIV");
    logo2.style.position = "absolute";
    logo2.style.top = "16px";
    logo2.style.left = "23px";
    logo2.style.width = "234px";
    
    logo2.style.height = "45px";
    logo2.style.overflow = "hidden";
	
	logoImage = document.getElementById("logo-img");
	if (logoImage && logoImage.src) {
		logoPath = logoImage.src.replace('logo.png', 'logo2.png');
		logo2.innerHTML = "<a href='.'><img src='"+logoPath+"' width='234' height'45' /></a>";
	}
	/*
	if (window.onload_functions)
	{
		logo2.innerHTML = "<a href='.'><img src='../images/logo2.png' width='234' height'45' /></a>";
	}
	else
	{
		logo2.innerHTML = "<a href='.'><img src='images/logo2.png' width='234' height'45' /></a>";
    }*/
    function crossClip(leftClipedElement, rightClipedElement, position)
    {
        leftClipedElement.style.clip = "rect(auto,auto,auto," + position + "px)";
        rightClipedElement.style.clip = "rect(auto," + position + "px,auto,auto)";
    }
    
    crossClip(logo1, logo2, 83);
    document.getElementById("header").appendChild(logo2);
    
    function logoSlide()
    {	
        var rightClip = 83;
        var intervalId = setInterval(
            function () {
                rightClip += animationData["logo"].rateHide;
                crossClip(logo1, logo2, rightClip);
                if (rightClip >= 234) {
                    clearInterval(intervalId);
                    logo2.style.clip = "rect(auto,auto,auto,auto)";
                    setTimeout(slideOut, 100);
                }
            }, animationData["logo"].intervalHide);
    };
    
    function slideOut() {
        setTimeout(
            function() {
                var leftClip = 83;
                var intervalId = setInterval(
                    function () {
                        leftClip += animationData["logo"].rateShow;
                        crossClip(logo2, logo1, leftClip);
                        if (leftClip >= (204 - animationData["logo"].rateShow)) {
                            clearInterval(intervalId);
                            crossClip(logo2, logo1, 204);
                            setTimeout(function() {crossClip(logo2, logo1, 214);}, animationData["logo"].dotDelay);
                            setTimeout(function() {crossClip(logo2, logo1, 225);}, 2 * animationData["logo"].dotDelay);
                            setTimeout(function() {crossClip(logo1, logo2, 83);}, 3 * animationData["logo"].dotDelay);
                            
                            setTimeout(logoSlide, 3 * animationData["logo"].dotDelay + animationData["logo"].showDelay);
                        }
                    }, animationData["logo"].intervalShow);
            }, animationData["logo"].hideDelay);
    }
    
    setTimeout(logoSlide, animationData["logo"].initialDelay);
    
    if (document.getElementById("ads"))
    {
        var ads = getElementsByClassName("ad_content", "div", document.getElementById("ads"));
        var adIndex = 0;
        var hiddenAdIndex = 3;
        var adCount = ads.length;
        var tempDiv = document.createElement("div");
        tempDiv.style.position = "absolute";
        tempDiv.style.margin = "0";
        tempDiv.style.display = "none";
        tempDiv.id = "temp_ad";
        tempDiv.className = "ad_content";
        document.getElementById("ads").appendChild(tempDiv);    
        
        function rotateAds()
        {
            setTimeout( function ()
            {    
                tempDiv = document.getElementById("temp_ad");
                tempDiv.innerHTML = ads[adIndex].innerHTML;
                var leftOffset = 0;
                if (ads[adIndex].className.search(/indented_ad/) !== -1)
                {
                    leftOffset = 50;
                }
                var obj =ads[adIndex];
                curleft = obj.offsetLeft + obj.offsetParent.offsetLeft;
                curtop = obj.offsetTop + obj.offsetParent.offsetTop;
                tempDiv.style.left = curleft + leftOffset + "px";
                tempDiv.style.top = curtop + "px";
                tempDiv.style.display = "block";
                setOpacity(tempDiv, 0.99);
                
                ads[adIndex].innerHTML= ads[hiddenAdIndex].innerHTML;
                ads[hiddenAdIndex].innerHTML = tempDiv.innerHTML;
                animate(tempDiv, 
                {
                    "opacity" :
                    {
                        "start" : "0.99", "end" : "0.0", "rate" : "-0.1"
                    }
                }, 100, function() 
                    {
                        tempDiv.style.display = "none";
                        adIndex = (adIndex  + 1) % 3;        
                        hiddenAdIndex = ((hiddenAdIndex + 1) % (adCount - 3)) + 3;
                        rotateAds();
                    }
                );
            }, 3000);
        }
        
        rotateAds();
    } // #ads
}

if (window.jQuery)
{
	jQuery(function($) {
		
		$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
		
		$('ul.gallery').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				//image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
		
		if ($("#course_blocks .block").length) {
			var blockStack = [];
			if ($("#course_blocks .block").length > 6)
			{
				$("#course_blocks .block").each(function(i) {
					if (i >= 6)
					{
						var parent = $(this).parent();
						blockStack.push($(this).remove());
						parent.remove();
					}
				});
			}
			var current_block = 0;
			var blockReplacementInterval = null;
			function stopBlockReplacment() 
			{
				if (blockReplacementInterval)
				{
					clearInterval(blockReplacementInterval);
					blockReplacementInterval = null;
				}		
			}
			function startBlockReplacment()
			{
				stopBlockReplacment();
				blockReplacementInterval = setInterval(function () {
					var block = blockStack.shift();
					var container = $("#course_blocks .block_container").eq(current_block);
					var previousBlock = container.find(".block").eq(0).css('zIndex', 10);
					block.hide().css('zIndex', 20);
					block.appendTo(container);
					block.fadeIn('slow', function() { blockStack.push(previousBlock.remove()); });
					if (++current_block >= 6)
					{
						current_block = 0;
					}
				}, 3000);
			}
			startBlockReplacment();
			
			$("#course_blocks .block_container").hover(function () {
				stopBlockReplacment();
			}, function () {
				startBlockReplacment();
			});
		}
	});
}

if (window.onload_functions !== undefined)
{
	onload_functions.push('init()');
}
else
{
	window.onload = init;
}


