var QHC = QHC || {};   // project namespace;

(function($) {
    // home page switcher timer;
    QHC.timer = (function() {
        QHC.videoOn = false;

        var _intID = null;
        var _duration = 12000;

        return {
            start: function(callback, scope) {
                if(QHC.videoOn) return false;
                _intID = setTimeout(function() {
                    callback.call(scope);
                    QHC.timer.start(callback, scope);
                }, _duration);
            },
            stop: function() {
                clearTimeout(_intID);
            }
        }
        
    })();

    // switch home page background images;
    QHC.switcher = (function() {

        var _element = null;
        var _parent = null;
        var _indx = 0;
        var _imgs = [];
		/* VLT 14/04/2011 */
		/* Added an array of links which will be used for each image*/
		/* Array of links: 1 image => 1 link (can be duplicated)*/
		var _href = [];
		
		/* then the link is replaced in the template by the correct one (href section)*/
        /*var _template = "<div class='figure'><a href='/products_and_services/switching.htm'><img src='{src}' height='310' width='940' alt='Let us look after you and your family'></a></div>";*/
		var _template = "<div class='figure'><a href='{href}'><img src='{src}' height='310' width='940' alt='Let us look after you and your family'></a></div>";

        return {
            init: function(el /*Object, jQuery collection*/, pel /*Object, jQuery collection*/, imgs /*Array*/, href /*Array*/) {
                _parent = pel;
                _imgs = imgs;
				_href = href;
                this.setElement(el);
                this.preload(imgs);
            },
            addImage: function(img) {
                _imgs.push(img.src);
            },
            preload: function(imgs) {
                var that = this;
                for (var i = 0; i<imgs.length; i++) {
                    var img = new Image();
                    img.src = imgs[i];
                };    
            },
            fire: function() {
				/* VLT 14/04/2011 */
				/* use the new array for multiple dynamic links*/
				/* if there are less links than images, the use the 1st link */
				var href_index = this.getIndex();
				if (href_index > _href.length -1) {
					href_index = 0;
				}
				/* then added the code to be replace by the correct link in the template */
				/*var o = {src: _imgs[this.getIndex()]}; */
                var o = {src: _imgs[this.getIndex()], href: _href[href_index]};
                var s = QHC.utils.substitute(_template,o);
                this.animate($(s).appendTo(_parent).hide());
            },
            animate: function(el) {
                var that = this;    
                _element.fadeOut(500, function() {
                    el.fadeIn(1000);
                    that.setElement.call(that, el);
                });
            },
            setElement: function(el){
                if(_element){
                    _element.remove();
                };
                _element = el;
            },
            setIndex: function(i) {
                _indx = i > _imgs.length-1 ? 0 : i;
            },
            getIndex: function() {
                return _indx;
            }
        };
        
    })();

    QHC.actions = (function() {

        var _element = null;
        var _parent = null;
        var _left = 622;

        return {
            init: function(el /*Object, jQuery collection*/, pel /*Object, jQuery collection*/, imgs /*Array*/) {
                _parent = pel;
                this.setElement(el);
            },
            setElement: function(el){
                if(_element){
                    _element.remove();
                };
                _element = el;
            },
            fire: function() {
                var el = _element;
                var l = _left;
                /*
                setTimeout(function() {
                    el.find('.gutter').css({
                            'background': 'url(images/save-today.gif) top left no-repeat'
                        })
                        .end()
                        .css({
                            'left': '972px',
                            'opacity': 0
                        })
                        .animate({'left': l, 'opacity': 1}, 150,  function() {
                                el.css({'opacity': ''}).find('.gutter').css({
                                    'background': 'url(images/save-today.png) top left no-repeat'
                            })
                        }); 
                }, 1000);
                */
            }
        };
        
    })();

    $(document).ready(function($) {
		/* Caroussel images */
        var i = [
            'images/Great-News-homepage-banner2.jpg', // Make sure this image matches your default image on index page
            'images/Great-News-homepage-banner2.jpg', 
            'images/summer2.jpg'
        ];

		/* VLT 14/04/2011 */
		/* Array of links: 1 image => 1 link (links can be duplicated)*/
		var j = [

<!-- H+A Update - 5/6/2011 -->
            'greatnews.htm', // link for 1st image above, Make sure this link matches your default link on index page
<!-- H+A Update - 5/6/2011 -->

            'greatnews.htm',  /* link for 2nd image above */
            'products_and_services/switching.htm' /* link for 3rd image above */
        ];
		
        var fire = function() {
            var i = QHC.switcher.getIndex();
            QHC.switcher.setIndex(i+1);
            QHC.switcher.fire();
            QHC.actions.fire();
            
            i = QHC.switcher.getIndex();
            $('.switcherOverride a').each(function() {
                if(this.id.indexOf('_'+i) != -1) {
                    $(this).addClass('active').siblings().removeClass('active');           
                };
            });

        };

		/* VLT 14/04/2011 */
		/* Send the array of links to the init function as well*/
        QHC.switcher.init($('#figure_0'), $('#switcher'), i, j); 
        QHC.actions.init($('#actionBlock_0')); 
        QHC.timer.start(fire, QHC.switcher); 

        // animation override handler;
        $('.switcherOverride').click(function(e) {
            e.preventDefault();
            var i = e.target.id.split('_')[1];
            $(e.target).addClass('active').siblings().removeClass('active');
            QHC.timer.stop();
            QHC.switcher.setIndex(parseInt(i));
            QHC.switcher.fire();
            QHC.actions.fire();
            QHC.timer.start(fire, QHC.switcher);
        });

        // freeze 'switch box' on hover;
        $('.homeActionBlock')
            .mouseenter(function() {
                QHC.timer.stop();
            })
            .mouseleave(function() {
            QHC.timer.start(fire, QHC.switcher);
        });

        // bridge to MooTools Light box;
        $('.playVideo').live('click', function() {
            QHC.videoOn = true;
            QHC.timer.stop();
            open_lightbox(this.href);
            return false;
        });

        $('.MultiBoxClose').live('click',function(){
            QHC.videoOn = false;
            QHC.timer.start(fire, QHC.switcher);
        })
    });    
})(jQuery);

