var accordionIndex = 0;


function selectBoxes() {
    //Start elSelect
    /*
    $$('select').each(function(item) { 
    guidd = 'a'+guid();
    var nwrap = new Element('div', {id : guidd});
    nwrap.wraps(item);
		
    new elSelect({'container': guidd, hover: {
    'mouseenter': {'background-color': '#CCCCCC'},
    'mouseleave': {'background-color': '#EEEEEE'}
    }
    });
    }); */


    if ($$('choosered')) {
        $$('choosered').addEvents({ 'change': function () {
            if (this.get('value') != '0') {
                window.location = this.get('value');
            }
        }
        });
    }
    if ($$('choosegreen')) {
        $$('choosegreen').addEvents({ 'change': function () {
            if (this.get('value') != '0') {
                window.location = this.get('value');
            }
        }
        });
    }


}

window.addEvent('domready', function () {
    selectBoxes();
    if ($$('span.selectline-line')) {
        $$('span.selectline-line').each(function (el) {
            var myHTMLRequest = new Request.HTML({
                onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
                    el.set('html', responseHTML);
                    selectBoxes();
                }
            }).get('green-line-inputs.html');
        });

    }

    if ($$('span.selectline-red')) {
        $$('span.selectline-red').each(function (el) {
            var myHTMLRequest = new Request.HTML({
                onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
                    el.set('html', responseHTML);
                    selectBoxes();
                }
            }).get('red-line-inputs.html');
        });

    }
    //Accordion
    if ($$('#resources div')) {
        accordionIndex = (window.location.hash) ? window.location.hash.replace(/#/, '') : 0;
        var myAccordion = new Fx.Accordion(
			$$('#resources div'),
			$$('#resources-content .item'),
			{
			    display: accordionIndex,
			    alwaysHide: true,
			    initialDisplayFx: false,
			    onComplete: function () {
			        loc = window.location.toString().split('#');
			        window.location = loc[0] + '#' + this.previous;
			    }

			}
		);
    }







    /*
    Home Page Events 
    */


    if ((gs = $('home-green')) && (rs = $('home-red'))) {
        gs.hide();
    }

    if ($$('green')) {
        $$('green').addEvent('change', function () {
            if (this.get('value') == 'green') {
                showGreen();
            } else {
                showRed();
            }
        });

        $$('red').addEvent('change', function () {
            if (this.get('value') == 'red') {
                showRed();
            } else {
                showGreen();
            }
        });
        showRed();
    }


    //Quick Search Change Menu
    if ($$('quicksearchselect')) {
        $$('quicksearchselect').addEvents({ 'change': function () {
            if (this.get('value') != '0') {
                window.location = "http://www.luas.ie/" + this.get('value');
            }
        }
        });
    }



    var pqs = $$('a[href=quick-search-menu.html]');
    if (pqs) {
        pqs.each(function (item) {
            var its = item.getParent().set('html', '');
            $$('quicksearch').inject(its);
        });
    }
});

//window.addEvent('load', function () {
//    new rsZoom({ container: 'hoverMap', 'height': 150, 'width': 150, 'relative': true });
//});



function showGreen() {
    $('home-red-home').hide();
    $('home-green-home').show();
}
function showRed() {
    $('home-green-home').hide();
    $('home-red-home').show();
}

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

var rsZoom = new Class({
    options: {
        //Basic
        container: false,
        defaultClass: 'rsZoom',
        width: false,
        height: false,
        relative: false
    },
    container: false,
    containerSizes: false,
    overlay: false,
    thumbnail: false,
    sizes: false,
    inner: false,
    rel: false,
    /*
    pass the options,
    create html and inject into container
    */
    initialize: function (options) {
        this.setOptions(options);
        this.container = $$(this.options.container);
        if (this.container) {
            this.pullItems();
        }
    },

    setStage: function () {
        //Make sure overlay is on top
        this.overlay.inject(this.container, 'top');

        var siz = this.thumbnail.getSize();
        this.sizes = siz;
        this.container.setStyles({ 'height': siz.y, 'width': siz.x, 'position': 'relative' });


        //Set styles of the overlay
        if (!this.options.width) {
            //If theres no width for the overlay set the maximum it can be
            this.options.width = siz.x;
        }
        if (!this.options.height) {
            this.options.height = siz.y;
        }
        this.overlay.set('opacity', 0).setStyles({ 'width': this.options.width, 'height': this.options.height, 'position': 'absolute', 'border': 'solid 2px #CCCCCC' });

        if (this.options.relative) {
            this.rel.setStyles({ 'width': this.options.width, 'height': this.options.height });
        }

    },

    pullItems: function () {
        this.container.addClass(this.options.defaultClass);

        var ovtemp = this.container.getElement('.overlay');
        /*
        * If the overlay is move inner relative to the position set up the stage
        */
        if (this.options.relative) {
            ovtemp.setStyles({ 'width': $$('linelarge').getStyle('width'), 'height': $$('linelarge').getStyle('height') });
            var ovAb = new Element('div');
            var ovRel = new Element('div', { 'styles': { 'position': 'relative', 'overflow': 'hidden'} });
            ovtemp.inject(ovRel);
            ovRel.inject(ovAb);
            this.rel = ovRel;
            this.inner = ovtemp.setStyles({ 'position': 'absolute' });
            this.overlay = ovAb;
        } else {
            this.overlay = ovtemp;
        }
        this.thumbnail = this.container.getElement('.thumbnail');

        //Set Stage
        this.setStage();

        this.container.addEvents({
            'mousemove': function (event) {
                this.move(event);
            } .bind(this),
            'mouseenter': function () {
                /*
                var db = $(document.body);
                var myFx = new Fx.Scroll(db, {
                offset: {
                x: 0,
                y: 0
                }
                }).toTop();
                document.addEvent("mousewheel",function(e){ e.preventDefault(); });
                */
                this.overlay.tween('opacity', 1);
            } .bind(this),
            'mouseleave': function () {
                this.overlay.tween('opacity', 0);
                document.removeEvents("mousewheel");
            } .bind(this)
        });
    },

    move: function (event) {
        var p = event.page;
        var q = this.thumbnail.getCoordinates();
        var lp = (p.x - q.left.toInt() - (this.options.width / 2));
        var maxlp = (this.sizes.x - this.options.width);
        var maxf = false;

        if (lp < 0) {
            lp = 0;
        }
        if (lp > maxlp) {
            lp = maxlp;
            maxf = true;
        }

        var tp = ((this.sizes.y.toInt() / 2) - (this.options.height / 2));
        tp = (p.y - q.top.toInt() - (this.options.height / 2));
        var maxtp = (this.sizes.y - this.options.height);
        if (tp < 0) {
            tp = 0;
        }
        if (tp > maxtp) {
            tp = maxtp;
            maxf = true;
        }



        this.overlay.setStyles({ 'top': tp, 'left': lp });


        //Inner Relative Move
        if (this.options.relative) {
            //Find the right position for it being left
            //Find where the mouse is
            lp = p.x - q.left.toInt();
            var ip = (lp / this.sizes.x);

            //Set move the inner image accordingly
            var innerw = this.inner.getSize().x.toInt();
            this.inner.setStyle('left', (ip * innerw * (-1) + (this.options.width * ip)));



            tlp = p.y - q.top.toInt();
            var itp = (tlp / this.sizes.y);

            //Set move the inner image accordingly
            innerw = this.inner.getSize().y.toInt();
            var posTop = (itp * innerw * (-1) + (this.options.height * itp));
            /*
            if(posTop < 0 && tp == 0) {
            posTop = 0;
            }
            */
            this.inner.setStyle('top', posTop);

        }
    },

    message: function (message) {
        if (console) {
            console.log(message);
        }
    }

});
rsZoom.implement(new Events);
rsZoom.implement(new Options);
