addDOMLoadEvent(findActiveLinks);
addDOMLoadEvent(setupMenuDesign);
addDOMLoadEvent(checkFontSize);
addDOMLoadEvent(setupFontSize);
addDOMLoadEvent(setupCorners);
addDOMLoadEvent(setupInvestmentProgramme);
addDOMLoadEvent(addckeditor);
addDOMLoadEvent(setupimageeffect);

function setupimageeffect()
{
    setInterval(imageeffect, 4000);
}

function imageeffect()
{
    var y = $$('.imageswrapper .images img.fadeout')
    if(y.length>0) {
        for(var n=0;n<y.length;n++)
        {
            if(n==0) {
                alt = $(y[n]).getAttribute("alt");
                src = $(y[n]).getAttribute("src");
                $(y[n]).up('p').insert({bottom: '<img class="fadein" src="'+src+'" alt="'+alt+'" style="position: absolute; left: '+((y.length-1)*240)+'px">'});
            }

            $(y[n]).remove();
        }
    }

    var x = $$('.imageswrapper .images img');
    //for(var i=0; i < x.length; i++)
    for(var i=x.length-1; i >=0; i--)
    {
        // need to clone first image and move to end

        // for testing only going to use first image
        if(i<x.length-1) {
            alt = $(x[i+1]).getAttribute("alt");
            src = $(x[i+1]).getAttribute("src");
            $(x[i]).removeClassName("fadein");
            $(x[i]).addClassName("fadeout");
            $(x[i]).up('p').insert({top: '<img class="fadein" src="'+src+'" alt="'+alt+'" style="position: absolute; left: '+(i*240)+'px">'});
            if(i<5) $(x[i]).fade({duration: 3.0});
        } else {
            $(x[i]).addClassName("fadeout");
        }
    }
}

function addckeditor() {
    var x = $$('.fhEditor textarea');
    for(var i=0; i < x.length; i++)
    {
        CKEDITOR.addStylesSet( 'my_styles',
        [
            // Block Styles
            { name : 'Page Header', element : 'h2', styles : {  } },
            { name : 'Page Sub-Header' , element : 'h3', styles : {  } },
        ]);

        CKEDITOR.replace($(x[i]).identify(), {
            forcePasteAsPlainText: true,
            stylesSet: 'my_styles',
            toolbar:
                [
                    ['Source','-'],
                    ['Cut','Copy','Paste','SpellChecker'],
                    ['Undo','Redo','-','RemoveFormat'],
                    ['Bold','Italic','Strike'],
                    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                    ['Link','Unlink'],
                    '/',
                    ['Image','MediaEmbed','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
                    ['Styles'],
                    ['Maximize']
                ]
        });
        
        if(i==0) CKFinder.setupCKEditor( null, '/ckfinder/' );
    }
    
}

function setupInvestmentProgramme()
{
    var x = $$('form.investment_prog #houseno');
    for (var i=0; i<x.length;i++)
    {
        Event.observe($(x[i]), 'click', function(e)     {
                if(this.getValue()=='No.') this.value = '';
        }, false);
    }

    x = $$('form.investment_prog #street');
    for (i=0; i<x.length;i++)
    {
        Event.observe($(x[i]), 'click', function(e)     {
                if(this.getValue()=='Street name') this.value = '';
        }, false);
    }
}

function setupLeaflets()
{
    var x = $$('.leaflets li');
    for (var i=0; i<x.length;i++)
    {
        if($(x[i]).down("a")&&$(x[i]).down("img")) {
            Event.observe($(x[i]).down("a"), 'mouseover', function(e)     {
                image = this.up("li").down("img").readAttribute('src');
                alt = this.up("li").down("img").readAttribute('alt');
                $('leaflet_thumb').insert({top: '<img class="right" src="'+image+'" alt="'+alt+'">'});
            }, false);
            Event.observe($(x[i]).down("a"), 'mouseout', function(e)     {
                $('leaflet_thumb').down("img").remove();
            }, false);
        }
    }
}

function setupCorners()
{
    ieVersion = getIEVersionNumber();
    if (ieVersion>0&&ieVersion<9) {
        var x = $$('div.quicktools');
        if (x.length>0) {
            for (var i=0; i<x.length;i++)
            {
                $(x[i]).addClassName("corners");
            }
            Nifty("div.quicktools", "big transparent");
        }

       x = $$('p.highlight_strip');
        if (x.length>0) {
            for (var i=0; i<x.length;i++)
            {
                $(x[i]).addClassName("corners");
            }
            Nifty("p.highlight_strip", "transparent");
        }
    }
}

function setupFontSize() {
    if ($('textsize')) {
        $('textsize').insert({top: 'Text Size <a title="Reset fontsize" id="normalfont"><img src="assets/images/normalfont.gif" alt="Reset fontsize"></a><a title="Use medium fontsize" id="mediumfont"><img src="assets/images/mediumfont.gif" alt="Use medium fontsize"></a><a title="Use large fontsize" id="largefont"><img src="assets/images/largefont.gif" alt="Use large fontsize"></a>'});
        Event.observe($('normalfont'), 'click', function(e)     {
            setFontSize('normalfont');
        }, false);
        Event.observe($('mediumfont'), 'click', function(e)     {
            setFontSize('mediumfont');
        }, false);
        Event.observe($('largefont'), 'click', function(e)     {
            setFontSize('largefont');
        }, false);
    }
}

function setFontSize(size)
{
    if(size=="normalfont") {
        $('body').removeClassName('mediumfont');
        $('body').removeClassName('largefont');
    }
    if(size=="mediumfont") {
        $('body').addClassName('mediumfont');
        $('body').removeClassName('largefont');
    }
    if(size=="largefont") {
        $('body').addClassName('largefont');
        $('body').removeClassName('mediumfont');
    }
    createCookie("fontSize", size, 365);
}

function checkFontSize()
{
    var fontSize = readCookie("fontSize");
    if (fontSize != null) setFontSize(fontSize);
}

function setupMenuDesign()
{
	var x = $$('.nav ul ul');
	for(var i=0; i < x.length; i++)
        {
            $(x[i]).style.display = "none";
	}

	x = $$('.nav ul a.active');
	for(i=0; i < x.length; i++)
        {
            if ($(x[i]).next("ul"))
                $(x[i]).next("ul").style.display = "block";
            if ($(x[i]).up("ul"))
                $(x[i]).up("ul").style.display = "block";
	}
}

function findActiveLinks()
{
	var x = $$('a');
	for(var i=0; i < x.length; i++)
        {
	    var currentPath = window.location.pathname;
	    var currentPage = currentPath.substring(currentPath.lastIndexOf('/') + 1);
	    var linkPath = $(x[i]).href;
	    var linkPage = linkPath.substring(linkPath.lastIndexOf('/') + 1);

            if (getURLVariable('task'))
            {
                currentPage = currentPage + '?task=' + getURLVariable('task');
            }
            if (currentPage == linkPage || (linkPage == "index.php" && currentPage == ''))
	    {
                $(x[i]).addClassName('active');
            }
        }

	var x = $$('.nav ul ul a.active');
	for(var i=0; i < x.length; i++)
        {
            if(!$(x[i]).up("li").hasClassName('toplevel')) {
                if($(x[i]).up("li").up("li").hasClassName('toplevel')) {
                    $(x[i]).up("li").up("li").down("a").addClassName('active');
                } else {
                    $(x[i]).up("li").up("li").down("a").addClassName('active');
                    if($(x[i]).up("li").up("li").up("li").hasClassName('toplevel')) {
                        $(x[i]).up("li").up("li").up("li").down("a").addClassName('active');
                    } else {
                        $(x[i]).up("li").up("li").up("li").down("a").addClassName('active');
                        $(x[i]).up("li").up("li").up("li").up("li").down("a").addClassName('active');
                    }
                }
            }
	}
}
