﻿function torro(siteURL) {
    var gallery = $('gallery-all');
    var gallery_description = $('gallery-description');
    var thumbs = $$('.thumbs');
    var checkThumbs = thumbs.length;
    var animate = false;
    var currentSlide
    var smallThumbs;

    if (thumbs.length != 0) {
        var currentURL = document.URL;
        //alert(currentURL + siteURL);
        if (currentURL == siteURL) {
            //alert(currentSlide.getElement('a'));
            thumbs.getElement('a').addEvent('click', function(e) {
                var thumbs = $$('#gallery-all div.thumbs');
                if (this.getParent('div.thumbs') != currentSlide && this.getParent('div.thumbs') != thumbs[0]) {
                    e.stop();
                    nextItem();
                    this.setStyle('cursor', 'pointer');
                } else if (this.getParent('div.thumbs') == thumbs[0]) {

                    e.stop();
                    prevItem();
                    this.setStyle('cursor', 'pointer');
                }
            });
        } else if (currentURL != siteURL) {
            thumbs.addEvent('click', function(e) {
                var thumbs = $$('#gallery-all div.thumbs');
                if (this != currentSlide && this != thumbs[0]) {
                    nextItem();
                    this.setStyle('cursor', 'pointer');
                } else if (this == thumbs[0]) {
                    this.setStyle('cursor', 'pointer');
                    prevItem();
                }
            });
        }
    }

    function updateInfo(idx) {
        var thumbs = $$('#gallery-all div.thumbs');
        var firstInfo = thumbs[idx].getElement(".hidden_info");
        if (firstInfo) {
            $('gallery-description').set('opacity', 0);
            $('gallery-description').set('html', firstInfo.get('html'));
            $('gallery-description').fade('in');
        }
    }


    if (checkThumbs > 0) {
        var currentThumb;
        var firstInfo;
        firstInfo = thumbs[0].getElement(".hidden_info");


        if (thumbs.length > 1 && gallery_description) {
            updateInfo(1);
        }



        function nextItem(repeat, speed) {

            if (!speed) {
                speed = 500;
            }

            if (!repeat) {
                repeat = 0;
            }
            //alert(repeat);
            if (animate == false) {

                var thumbs = $$('#gallery-all div.thumbs');


                var grid = $$('.grid');

                var width = thumbs[1].getSize();

                var width2 = thumbs[0].getSize();

                var posMargin = width.x;

                var negMargin = posMargin * -1;

                if (repeat == 0) {

                }
                var nextFx = new Fx.Tween(thumbs[0], {
                    duration: speed,
                    onComplete: function(el) {
                        animate = false;
                        if (firstInfo) {
                            updateInfo(2);
                        }


                        el.fade('hide');
                        el.setStyle('margin-left', 0);
                        el.inject($('gallery-all'), 'bottom');
                        el.fade('show');


                        if (repeat > 0) {
                            nextItem(repeat - 1, speed);
                        } else {
                            thumbs.each(function(el) {
                                if ((thumbs.length > 2 && el != thumbs[2]) || (thumbs.length == 2 && el != thumbs[0])) {
                                    //Pixastic.process(el.getElement('.gal_image'), "desaturate", {average : false});
                                    el.getElement('.gal_image_a').addClass('grey');

                                } else {

                                    currentSlide = el;
                                    //alert(currentSlide);
                                    //Pixastic.revert(el.getElement('.gal_image'));
                                    el.getElement('.gal_image_a').removeClass('grey');
                                    if (smallThumbs.length != 0) {
                                        if (thumbs.length > 2) {
                                            //alert(smallThumbs.length);
                                            var selectThumb = ("smallthumb-") + (thumbs[2].get('id').split('-')[1]);
                                        } else {
                                            var selectThumb = ("smallthumb-") + (thumbs[0].get('id').split('-')[1]);

                                        }
                                        //alert(selectThumb);
                                        selectThumb = $(selectThumb);
                                        thumbRollOver(selectThumb)
                                    }
                                }
                            });
                        }


                    },
                    onStart: function() {
                        animate = true;
                        var nextFx2 = new Fx.Tween(thumbs[1], { duration: speed });
                        nextFx2.start('margin-left', negMargin);
                        if (firstInfo) {
                            gallery_description.fade('out');
                        }
                    }
                });

                nextFx.start('margin-left', negMargin - width2.x);


            } // end if animate

        } // end next item


        function prevItem() {
            if (animate == false) {
                var thumbs = $$('#gallery-all div.thumbs');
                var lastThumb = thumbs.length - 1;
                //var grid = $$('.grid');
                //alert(lastThumb);

                var width = thumbs[lastThumb].getSize();
                var posMargin = width.x;
                var negMargin = posMargin * -1;




                var prevFx = new Fx.Tween(thumbs[lastThumb], {
                    onStart: function(el) {
                        animate = true;

                        el.setStyle('display', 'none');
                        el.inject($('gallery-all'), 'top');
                        el.setStyle('display', 'block');

                        if (firstInfo) {
                            gallery_description.fade('out');
                        }
                    },
                    onComplete: function() {
                        animate = false;
                        if (firstInfo) {
                            updateInfo(1);
                        }
                        currentSlide = thumbs[0];
                        //Pixastic.process(thumbs[1].getElement('.gal_image'), "desaturate", {average : false});
                        thumbs[1].getElement('.gal_image_a').addClass('grey');
                        thumbs[0].getElement('.gal_image_a').removeClass('grey');
                        //Pixastic.revert(thumbs[0].getElement('.gal_image'));
                        if (smallThumbs.length != 0) {
                            var selectThumb = ("smallthumb-") + (thumbs[0].get('id').split('-')[1])
                            selectThumb = $(selectThumb);
                            thumbRollOver(selectThumb);
                        }

                    }
                });

                var prevFx2 = new Fx.Tween(thumbs[0], {
                    onStart: function(el) {

                    }
                });

                prevFx.element.setStyle('margin-left', negMargin - thumbs[0].getSize().x);
                prevFx.start('margin-left', negMargin);
                prevFx2.start('margin-left', "0px");
                /*
                var gridOut = new Fx.Tween(grid[0]);
                gridOut.start('opacity', 1, 0);
			
			var gridIn = new Fx.Tween(grid[1]);
                gridIn.start('opacity', 0, 1);
                */
            }
        }
        // end previous item
        function thumbRollOver(activeThumb) {
            currentThumb = activeThumb;
            //alert(activeThumb);
            var satFx = new Fx.Tween(activeThumb.getElement('a'), {
                onStart: function(el) {
                    var theA = currentThumb.getElement('a');
                    //smallThumbs.setStyle('opacity','0.7');
                    for (var i = 0; i < smallThumbs.length; i++) {
                        if (smallThumbs[i] != currentThumb) {
                            //var oldSelected = $$('.selected-thumb');

                            smallThumbs[i].getElement('a').setStyle('background-position', '0 -65px')

                            smallThumbs[i].removeClass('selected-thumb');
                            smallThumbs[i].getElement('a').setStyle('opacity', '0.7')
                            smallThumbs[i].setStyle('border-bottom', 'none');
                        } else {
                            smallThumbs[i].addClass('selected-thumb');
                        }
                    }
                    theA.setStyle('opacity', '1');
                    //var parent=el.getParent('div.smallthumb');
                    theA.getParent('div.smallthumb').setStyle('border-bottom', '1px solid #cd1041');

                }
            });
            satFx.start('background-position', '0 0');
        }

        if (checkThumbs > 0) {

            if (thumbs.length > 1) {

                thumbs.each(function(el) {
                    if (el != thumbs[1]) {
                        //Pixastic.process(el.getElement('.gal_image'), "desaturate", {average : false});
                        el.getElement('.gal_image_a').addClass('grey');
                    }
                });

                var imgCount = thumbs.length;
                var myFx = new Fx.Tween(thumbs[0]);
                // var grid = $$('.grid');
                // var gridOut = new Fx.Tween(grid[1]);
                // gridOut.start('opacity', 1, 0);	

                //begin thumbnails
                smallThumbs = $$('.smallthumb');
                if (smallThumbs.length != 0) {
                    selectedThumb = $('smallthumb-2')
                    thumbRollOver(selectedThumb);
                }

                $each(smallThumbs, function(el, idx) {
                    el.addEvent('click', function(e) {

                        var me = this;
                        var myID = me.get('id');
                        myID = myID.split("-");
                        myID = myID[1];
                        moveThumb = "thumb-" + myID;

                        e.stop();
                        relatedThumb = $(moveThumb);
                        //alert(relatedThumb);
                        var thumbs = $$('#gallery-all div.thumbs');

                        for (var i = 0; i < thumbs.length; i++) {
                            var arrayIndex = i;

                            if (thumbs[i].get('id').split("-").pop() == myID) {

                                var slideIndex = arrayIndex;

                            }

                        }

                        //alert(slideIndex);

                        if (slideIndex < 1) {
                            //trigger prev
                            prevItem();

                        } else if (slideIndex == 1) {
                            //do nothing the clicked thumbnail is the current slide
                        } else {
                            //trigger next for thumbnail
                            if (slideIndex - 2 > 0) {
                                var speed = Math.round(500 / (slideIndex - 2));
                            }
                            nextItem(slideIndex - 2, speed);

                        } //end else 

                    });
                });
                //end thumbnails

                //begin next

                $('next').addEvent('click', function(e) {
                    e.stop();
                    nextItem();
                });
                //end next	

                // begin prev
                $('prev').addEvent('click', function(e) {
                    e.stop();
                    prevItem();
                });
                //end prev

            }
            else {
                //var grid = $$('.grid');
                //thumbs[0].setStyle('margin-left', '546px');
                //var gridOut = new Fx.Tween(grid[0]);
                //gridOut.start('opacity', 1, 0);	

            }
        }
    }

    if (smallThumbs) {
        smallThumbs.addEvent('click', function(e) {

            var clickedThumb = this;
            e.stop();
            thumbRollOver(clickedThumb);

        });
        var selectThumb = ("smallthumb-") + (thumbs[0].get('id').split('-')[1])
        selectThumb = $(selectThumb);

        smallThumbs.addEvents({
            'mouseover': function(e) {
                e = new Event(e);
                //e.stopPropagation();
                e.stop();
                //var selectThumb=("smallthumb-")+(thumbs[0].get('id').split('-')[1])
                //selectThumb=$(selectThumb);	
                //console.log('ON: '+this);
                var overThumb = this;
                if (overThumb != currentThumb) {
                    overThumb.setStyle('opacity', '1.0');
                    overThumb.setStyle('border-bottom', '1px solid #cd1041');
                }

            },
            'mouseleave': function(e) {
                //var selectThumb=("smallthumb-")+(thumbs[0].get('id').split('-')[1])
                //selectThumb=$(selectThumb);	
                e = new Event(e);
                //e.stopPropagation();
                e.stop();
                //console.log('OFF: '+this);

                var outThumb = this;
                //alert(e.target);
                if (outThumb != currentThumb) {
                    outThumb.setStyle('opacity', '0.7')
                    outThumb.setStyle('border-bottom', 'none');
                }
            }

        });
    }


    //end gallery

    //begin menu function


    var menuAnimate = false;

    var currentURL = document.URL;

    var openMenu = null;

    var topMenus = $$('.topmenu');

    var closeMenus = $$('.close');

    var subMenus = $$('ul.submenu');

    //alert(keystaffMenu);




    function openSubMenu(mySub, source, speed) {
        var me = null;
        if (source) {
            me = source;

        }

        if (source == null) {
            var myTitle = mySub.get('title');
            for (var i = 0; i < topMenus.length; i++) {
                if (topMenus[i].get('id') == myTitle) {
                    me = topMenus[i]
                    me.setStyle('color', '#cd1041');
                    //me.setStyle('margin-left', '-1px');
                    me.setStyle('border-left', '1px solid #cd1041');
                    me.setStyle('padding-left', '19px');
                }

            }
        }

        if (speed == null) {
            speed = 500;
        }

        if (mySub && menuAnimate == false) {

            if (!openMenu) {
                var menuFx = new Fx.Tween(mySub, {
                    onStart: function(el) {
                        menuAnimate = true;
                        mySub.setStyle('display', 'block');
                    },
                    duration: speed,
                    onComplete: function(el) {
                        openMenu = el;
                        if (me) {
                            me.setStyle('color', '#cd1041');
                            me.setStyle('border-left', '1px solid #cd1041');
                            //me.setStyle('margin-left', '-1px');
                            me.setStyle('padding-left', '19px');
                        }
                        menuAnimate = false;
                    }
                });
                menuFx.start('width', "165px");
            }
            else {
                //var subMenu = $$('li.submenu');

                if (openMenu != mySub && menuAnimate == false) {
                    var closeFx = new Fx.Tween(openMenu, {
                        onStart: function() {
                            menuAnimate = true;
                        },
                        onComplete: function(el) {
                            el.setStyle('display', 'none');
                            if (openMenu == el) {
                                openMenu = null;
                            };

                            var menuFx = new Fx.Tween(mySub, {
                                onStart: function(el) {

                                    el.setStyle('display', 'block');
                                    openMenu = el;
                                    topMenus.setStyle('color', '#77787B');
                                    topMenus.setStyle('border-left', 'none');
                                    topMenus.setStyle('padding-left', '20px');
                                    topMenus.setStyle('margin-left', '0px');
                                    if (me) {
                                        me.setStyle('color', '#cd1041');
                                        me.setStyle('border-left', '1px solid #cd1041');
                                        //me.setStyle('margin-left', '-1px');

                                    }
                                },
                                onComplete: function() {
                                    menuAnimate = false;
                                }
                            });
                            menuFx.start('width', "165px");


                        }
                    });

                    closeFx.start('width', "0px");
                } else {
                    if (openMenu == mySub) {

                        var closeFx = new Fx.Tween(openMenu, {
                            onStart: function(el) {
                                menuAnimate = true;
                            },
                            onComplete: function(el) {
                                menuAnimate = false;
                                topMenus.setStyle('color', '#77787B');
                                topMenus.setStyle('border-left', 'none');
                                topMenus.setStyle('margin-left', '0');
                                topMenus.setStyle('padding-left', '20px');
                                el.setStyle('display', 'none');
                                openMenu = null;
                            }
                        });
                        closeFx.start('width', "0px");



                    } //end if openMenu == mySub
                } //end else
            } // end if openMenu
        }
    }

    //end get current project
    $each(topMenus, function(el, idx) {

        el.addEvent('click', function(e) {

            var me = e.target;

            var myID = me.get('id');

            if (myID != "keystaff") {
                e.stop();
            }
            var mySub;

            for (var i = 0; i < subMenus.length; i++) {
                if (subMenus[i].get('title') == myID) {
                    mySub = subMenus[i];
                    openSubMenu(mySub, me);

                }
            }

        });

    });

    var selectSublink = $$('a[rel=sublink]');
    $each(selectSublink, function(el, idx) {
        if (el.get('href') == currentURL) {
            // open parent menu.
            el.setStyle('color', '#cd1041');
            el.setStyle('border-left', '1px solid #cd1041');
            //el.setStyle('margin-left', '-1px');
            el.setStyle('padding-left', '19px');
            var submenu = el.getParent('.submenu');
            openSubMenu(submenu, null, 0);

        }
    });

    //begin fullscreen display
    var full_thumbs = $$('.thumb_link');
    var currentFullThumb;
    currentFullThumb = full_thumbs[0];
    if (full_thumbs) {

        full_thumbs.each(function(itm) {
            itm.addEvent('click', function(e) {
                e.stop();

                changeFullThumb(itm);

            })
        });
    }

    function changeFullThumb(itm) {
        var file = itm.get('href');
        $('large_image').set('src', file);

        currentFullThumb = itm;
        var clickedThumb = itm.getParent('div.smallthumb');

        thumbRollOver(clickedThumb);
    }

    largeImage = $$('.largeimage');
    var fullscreen = $$('img.full-screen');
    if (fullscreen.length) {
        var standardView = $('standard-view');
        var fullScreenView = $('fullscreen-view');
        var closeFullScreen = $$('.close-full');
        var header = $('header');
        fullScreenView.fade('hide');
        var full_thumbs = $$('.thumb_link');
        var currentThumbIndex = 0;
        largeImage.addEvent('click', function(e) {

            for (var i = 0; i < full_thumbs.length; i++) {
                //thumbIndex = i;
                //alert(currentFullThumb);
                if (full_thumbs[i] == currentFullThumb) {
                    currentThumbIndex = i;
                    //alert(i);
                }

            }
            var nextThumb = currentThumbIndex + 1;
            if (nextThumb == full_thumbs.length) {
                nextThumb = 0;
            }
            currentFullThumb = full_thumbs[nextThumb];
            changeFullThumb(currentFullThumb);

            //.fireEvent('click');
            //thumbRollOver(full_thumbs[nextThumb].getParent('div.smallthumb'));

        })



        function resizeFullScreen(e) {
            $('images').setStyle('height', window.getSize().y - 108);
        }

        fullscreen.addEvent('click', function(e) {
            e.stop();
            scroll(0, 0);
            window.addEvent('resize', resizeFullScreen);
            resizeFullScreen(null);
            fullScreenView.setStyle('display', 'block');
            fullScreenView.setStyle('height', window.getScrollSize().y);
            fullScreenView.fade('in');
            //alert(full_thumbs[0]);
            thumbRollOver(full_thumbs[0].getParent('div.smallthumb'))

            $$('body')[0].setStyle('overflow', 'hidden');
        });
        closeFullScreen.addEvent('click', function(e) {
            e.stop();
            window.removeEvent('resize', resizeFullScreen);
            $$('body')[0].setStyle('overflow', '');
            fullScreenView.fade('out');
            fullScreenView.setStyle('display', 'none');
        });

    }

    $$('.news a').each(function(element) {

        // Constructor, takes the element and options as arguments
        new ReMooz(element, {
            centered: true, // Zoom the center of the screen
            origin: element.getElement('img') // Take the image inside as origin for the zooming element
        });

    });


}
