﻿
window.addEvent('domready', function() {
    SetupBlogSliders();
    SetupBlogEffects();
    $('QuickContactForm').setStyle('top', '-499px');
    $('openbuttonpicture').setStyle('display', 'none');
});

function slideCenterPannel(Istate) {
    if (Istate == 0) {
        //close action
        //Hide content and button, then move
        //$('postWindow').setStyle('display', 'none');
        //$('closebutton').setStyle('display', 'none');

        //Animate movement
        var Effect0 = new Fx.Morph('postWindow', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect1 = new Fx.Morph('center-col-holder', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect2 = new Fx.Morph('openbuttonpicture', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect3 = new Fx.Morph('center-col-holder', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect4 = new Fx.Morph('closebutton', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect5 = new Fx.Morph('shadow-sidebar', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect6 = new Fx.Morph('sidebar', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect8 = new Fx.Morph($('post'), { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect9 = new Fx.Tween($('MainPosts'), { duration: 500, transition: Fx.Transitions.Sine.easeInOut });

        Effect0.start({ 'opacity': 0 });
        Effect4.start({ 'opacity': 0 });

        Effect0.addEvent('complete', function() {

            $('closebutton').setStyle('display', 'none');
            $('postWindow').setStyle('display', 'none');
            

            Effect1.start({ 'width': [215, 30] });

            Effect3.addEvent('complete', function() {
                $('openbuttonpicture').setStyle('display', '');
                $('openbuttonpicture').setStyle('opacity', 0);
                Effect2.start({ 'opacity': 1 });
            });

            Effect3.start({ 'padding-left': 0 });

            Effect5.start({ 'left': [30] });

            $$('.PostListImages').each(function(e, i, a) {
                //e.set('morph', { duration: '500', transition: 'sine:inout' });
                e.get('morph').cancel();
                e.morph({ "width": "755px" });
            });

            $$('.blogImageSlide').each(function(e, i, a) {
                e.get('morph').cancel();
                e.morph({ "left": "0px" });
            });

            if ($('post')) {
                //alert('');
                Effect8.start({ 'padding-left': 30 });
            }
            if ($('MainPosts')) {
                //alert('');
                Effect9.start('padding-left', '30px');
            }
        });
    }
    else {
        //Open action
        //Animate movement
        var Effect0 = new Fx.Morph('postWindow', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect1 = new Fx.Morph('center-col-holder', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect2 = new Fx.Morph('openbuttonpicture', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect3 = new Fx.Morph('center-col-holder', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect4 = new Fx.Morph('closebutton', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect5 = new Fx.Morph('shadow-sidebar', { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        //var Effect7 = new Fx.Morph($('blogTopImages'), { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect8 = new Fx.Morph($('post'), { duration: 500, transition: Fx.Transitions.Sine.easeInOut });
        var Effect9 = new Fx.Tween($('MainPosts'), { duration: 500, transition: Fx.Transitions.Sine.easeInOut });

        Effect2.start({ 'opacity': 0 })

        Effect2.addEvent('complete', function() {

            //Add events
            Effect1.addEvent('complete', function() {
                //Remove openbutton
                $('openbuttonpicture').setStyle('display', 'none');

                //Add close button and content
                $('postWindow').setStyle('display', '');
                $('closebutton').setStyle('display', '');

                $('postWindow').setStyle('opacity', 0);
                $('closebutton').setStyle('opacity', 0);

                Effect0.start({ 'opacity': 1 });
                Effect4.start({ 'opacity': 1 });
                if ($('post')) {
                    Effect8.start({ 'padding-left': [215] });
                }
            });

            //Change size back to normal
            Effect1.start({ 'width': [30, 215] });

            Effect5.start({ 'left': [215] });

            if ($('MainPosts')) {
                Effect9.start('padding-left', '215px');
            }

            $$('.PostListImages').each(function(e, i, a) {
                e.get('morph').cancel();
                e.morph({ "width": "570px" });
            });

            $$('.blogImageSlide').each(function(e, i, a) {
                e.get('morph').cancel();
                e.morph({ "left": "-100px" });
            });
        });




        //Hide open button

        //set the button as visable
        //$('openbuttonpicture').setStyle('display', 'none');
    }
}

function SetupBlogEffects() {

    //top blog slider, inject elements into the right place
    if ($('HiddenBlogImages') != null && $('slider') != null) {
        $('HiddenBlogImages').getChildren().each(function(item, index, array) {
            item.inject($('slider'));
        });

        //Update Navigation buttons for images 
        //updateBlogImageLinks();
    }

    //PostContainers only exists on blog pages 
    if ($('PostContainers') != null) {
        // Made expanded selection
        var URL = location.href;
        URL = URL.toLowerCase();


        URL = URL.replace("http://localhost:2259", "");
        URL = URL.replace("http://www.wearealight.com", "");
        URL = URL.replace("http://www.wearealight.co.uk", "");

        //rsslinkpink
        $$('.rssLinkPic').each(function(e, i, a) {
            e.addEvent("mouseenter", function() {
                e.setProperty('src', '/resources/images/RSSpink.png');
            });
            e.addEvent("mouseleave", function() {
                e.setProperty('src', '/resources/images/RSSlink.png');
            });
        });

        //Highlight a menu item if there is a match
        var Counter = 1;
        $$('.slidebarNews').each(function(e, i, a) {
            if (e.getFirst().getFirst('.headingLink').getProperty('href').toLowerCase() == URL) {
                e.setStyle("background-color", "#F2F2F2");
                $('PostContainers').morph({
                    'left': $('PostContainers').getStyle('left').toInt() - (215 * parseInt(i / 4))
                });
            }
        });

        //Set the height of the size bar
        $('center-col-holder').setStyle('height', $('bodyContent').getStyle('height'));

        var BlogSliderItems = $$(".slidebarNews");

        //alert(BlogSliderItems.length);
        BlogSliderItems.each(function(item, index, array) {
            var Tween = new Fx.Tween(item, { duration: '300', transition: Fx.Transitions.Sine.easeInOut });

            item.addEvent('mouseenter', function() {
                //Tween.cancel();
                //Tween.start('background-color', '#ddd');
                //alert(item.getChildren(".BlogIconImage").length);
                if (item.getChildren(".BlogIconImage").length > 0) {
                    var img = item.getFirst(".BlogIconImage");
                    if (img != null) {
                        var Tween3 = new Fx.Tween(img, { duration: '300', transition: Fx.Transitions.Sine.easeInOut });
                        img.setStyle('z-order', '10');
                        img.setStyle('opacity', 0);
                        img.setStyle('display', 'inline');
                        Tween3.start("opacity", 1);
                    }
                }
            });

            item.addEvent('mouseleave', function() {
                //Tween.cancel();
                //Tween.start('background-color', '#fff');

                if (item.getChildren(".BlogIconImage").length > 0) {
                    var img = item.getFirst(".BlogIconImage");
                    if (img != null) {
                        var Tween2 = new Fx.Tween(img, { duration: '100', transition: Fx.Transitions.Sine.easeInOut });

                        Tween2.addEvent('complete', function() {
                            img.setStyle('display', 'none');
                        })
                        Tween2.start("opacity", 0);
                    }
                }
            });

            //item.morph("background-color", "")
            //	        var children = item.getChildren(".BlogIconText");
            //	        if (children.length == 1) 
            //	        {
            //	            //Winner
            //	            var obj = children.getFirst();
            //	            obj.addEvent('mouseover', function(){
            //	                   alert(obj.text);
            //	            });
            //	        }

        });
    }

    //Check for blog sidemenu
    if ($('PostContainers')) {
        //Add events for all next buttons
        $$('.blogMenuNext').each(function(e, i, a) {
            e.addEvent('click', function() {
                var LeftVal = $('PostContainers').getStyle('left').toInt();
                if (isNaN(LeftVal)) {
                    LeftVal = 0;
                }
                $('PostContainers').morph({
                    'left': [LeftVal, LeftVal + 215]
                });
            });
        });

        //Add events for all next buttons
        $$('.blogMenuPrev').each(function(e, i, a) {
            e.addEvent('click', function() {

                var LeftVal = $('PostContainers').getStyle('left').toInt();
                if (isNaN(LeftVal)) {
                    LeftVal = 0;
                }
                $('PostContainers').morph({
                    'left': [LeftVal, LeftVal - 215]
                });
            });
        });
    }

    SetupCommentsForm();
}


function updateBlogImageLinks(post) {
    if (post.getFirst('.PostImageContainer')) {
        var BlogImageLinks = post.getFirst('.PostImageContainer').getFirst('.PostListImages').getChildren();

        //Create element ul for buttons
        var menuUL = new Element('ul', {
            'id': 'blogImageMenu'
        });

        post.getFirst('.PostImageContainer').getFirst('.PostListImages').getChildren().each(function(item, index, array) {
            if (index != 0) item.setStyle('opacity', 0);
        });

        BlogImageLinks.each(function(item, index, array) {
            //Add li for each image
            var classToAdd = 'BlogImageMenuLink';
            if (index == 0) classToAdd = 'BlogImageMenuLinkSelected';

            var menuLI = new Element('li',
        {
            'class': classToAdd,
            'html': index + 1
        });

            menuLI.addEvent('mouseenter', function(el) {
                //menuLI.addClass('BlogImageMenuLinkSelected');
            });
            menuLI.inject(menuUL, "top");

            menuLI.addEvent('click', function(el) {
                menuLI.getParent().getChildren().each(function(item) {
                    item.removeClass('BlogImageMenuLinkSelected');
                    item.addClass('BlogImageMenuLink');
                });

                BlogImagePosition = parseInt(menuLI.innerHTML) - 1;
                var displayIndex = parseInt(menuLI.innerHTML) - 1;
                var itemToDisplay = post.getFirst('.PostImageContainer').getFirst('.PostListImages').getChildren()[displayIndex];
                //alert(itemToDisplay.innerHTML);
                post.getFirst('.PostImageContainer').getFirst('.PostListImages').getChildren().each(function(item, index, array) {
                    if (index != displayIndex) {
                        //Fade Out
                        if (item.getStyle('opacity') != 0) {
                            var T = new Fx.Tween(item, { duration: '300', transition: Fx.Transitions.Sine.easeInOut });
                            T.start('opacity', '0');
                        }
                    }
                    else {
                        //Fade in
                        if (item.getStyle('opacity') != 1) {
                            var T = new Fx.Tween(item, { duration: '300', transition: Fx.Transitions.Sine.easeInOut });
                            T.start('opacity', '1');
                            menuLI.addClass('BlogImageMenuLinkSelected');
                        }
                    }
                });

            });
            menuLI.inject(menuUL, "top");
        });

        menuUL.inject(post.getFirst('.BlogPostImagesControlsPositioner').getFirst());
    }
}


function SetupCommentsForm() {
    //check form exists 
    if ($('btnSaveAjax')) {
        //Set Default Values

        //Name
        $("ctl00_cphBody_CommentView1_txtName").addEvent("keyup", function() {
            if ($("ctl00_cphBody_CommentView1_txtName").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtName").setProperty("value", "Name *")
            }
        })

        $("ctl00_cphBody_CommentView1_txtName").addEvent("blur", function() {
            if ($("ctl00_cphBody_CommentView1_txtName").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtName").setProperty("value", "Name *")
            }
        })

        $("ctl00_cphBody_CommentView1_txtName").addEvent("focus", function() {
            if ($("ctl00_cphBody_CommentView1_txtName").getProperty("value") == "Name *") {
                $("ctl00_cphBody_CommentView1_txtName").setProperty("value", "")
            }
        })

        //Email
        $("ctl00_cphBody_CommentView1_txtEmail").addEvent("keyup", function() {
            if ($("ctl00_cphBody_CommentView1_txtEmail").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtEmail").setProperty("value", "Email *")
            }
        })

        $("ctl00_cphBody_CommentView1_txtEmail").addEvent("blur", function() {
            if ($("ctl00_cphBody_CommentView1_txtEmail").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtEmail").setProperty("value", "Email *")
            }
        })

        $("ctl00_cphBody_CommentView1_txtEmail").addEvent("focus", function() {
            if ($("ctl00_cphBody_CommentView1_txtEmail").getProperty("value") == "Email *") {
                $("ctl00_cphBody_CommentView1_txtEmail").setProperty("value", "")
            }
        })

        //Website
        $("ctl00_cphBody_CommentView1_txtWebsite").addEvent("keyup", function() {
            if ($("ctl00_cphBody_CommentView1_txtWebsite").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtWebsite").setProperty("value", "Website")
            }
        })

        $("ctl00_cphBody_CommentView1_txtWebsite").addEvent("blur", function() {
            if ($("ctl00_cphBody_CommentView1_txtWebsite").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtWebsite").setProperty("value", "Website")
            }
        })

        $("ctl00_cphBody_CommentView1_txtWebsite").addEvent("focus", function() {
            if ($("ctl00_cphBody_CommentView1_txtWebsite").getProperty("value") == "Website") {
                $("ctl00_cphBody_CommentView1_txtWebsite").setProperty("value", "")
            }
        })

        //Content 
        $("ctl00_cphBody_CommentView1_txtContent").addEvent("keyup", function() {
            if ($("ctl00_cphBody_CommentView1_txtContent").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtContent").setProperty("value", "Comment *")
            }
        })

        $("ctl00_cphBody_CommentView1_txtContent").addEvent("blur", function() {
            if ($("ctl00_cphBody_CommentView1_txtContent").getProperty("value") == "") {
                $("ctl00_cphBody_CommentView1_txtContent").setProperty("value", "Comment *")
            }
        })

        $("ctl00_cphBody_CommentView1_txtContent").addEvent("focus", function() {
            if ($("ctl00_cphBody_CommentView1_txtContent").getProperty("value") == "Comment *") {
                $("ctl00_cphBody_CommentView1_txtContent").setProperty("value", "")
            }
        })
        //setup form 


        //Add Validation event
        $('btnSaveAjax').addEvent('click', function() {
            //Validate Form
            var Validated = true;
            var Response = "";
            //If Ok, Submit, else inform User

            if ($("ctl00_cphBody_CommentView1_txtContent").getProperty("value") == "" || $("ctl00_cphBody_CommentView1_txtContent").getProperty("value") == "Comment *") {
                Validated = false;
                Response = "Please Enter an Comment\n" + Response;
                $("ctl00_cphBody_CommentView1_txtContent").focus();
            }

            if (!IsEmail($("ctl00_cphBody_CommentView1_txtEmail").getProperty("value"))) {
                Validated = false;
                Response = "Please Enter an Email Address\n" + Response;
                $("ctl00_cphBody_CommentView1_txtEmail").focus();
            }

            if ($("ctl00_cphBody_CommentView1_txtName").getProperty("value") == "" || $("ctl00_cphBody_CommentView1_txtName").getProperty("value") == "Name *") {
                Validated = false;
                Response = "Please Enter a Name\n" + Response;
                $("ctl00_cphBody_CommentView1_txtName").focus();
            }

            if ($("ctl00_cphBody_CommentView1_txtWebsite").getProperty("value") == "Website") {
                $("ctl00_cphBody_CommentView1_txtWebsite").setProperty("value", "");
            }

            if (Response.length > 0) {
                alert(Response);
            }

            if (Validated) {
                AddComment(false);
            }
        });

        //Triger Events To fill in default values
        $('ctl00_cphBody_CommentView1_txtName').fireEvent('blur')
        $('ctl00_cphBody_CommentView1_txtEmail').fireEvent('blur')
        $('ctl00_cphBody_CommentView1_txtContent').fireEvent('blur')
        $('ctl00_cphBody_CommentView1_txtWebsite').fireEvent('blur')
    }
}

function SetupBlogSliders() {
    //alert($$('.post').length);
    if ($$('.post')) {
        var posts = $$('.post');
        posts.each(function(e, i, a) {
            //Add Each Image in
            //e.getFirst('.BlogPostImages').set("html", "dfgdfgdfgdfg");
            updateBlogImageLinks(e);
        });
    }
}
