(function($) {
    var W = window, D = W.document, DE = D.documentElement, $D = $(D);

    var homePanels = {
        /**
         * manages home panels
         */

        init : function() {
            var that = this;

            box.ui('carousel').create({
                element : '#newsCarousel',
                horizontal : true,
                display : 1,
                duration : 400,
                autoplaying : false,
                paginate : true,
                buttons : false
            });

            box.ui('carousel').create({
                element : '#contentCarousel',
                horizontal : true,
                display : 1,
                duration : 400,
                autoplaying : false,
                paginate : true,
                buttons : false
            });

            this.wrapper = $('#content');
            this.items = $('.panel');
            this.containerWidth = 0;
            this.items.each(function(index) {
                var item = $(this);

                that.containerWidth += item.width();
                var id = item.attr('id');

                /* stores moving children once for later animation */
                if(id === 'mainSolutions') {
                    item.data('contentWrapper', item.find('.contentWrapper'));
                }
                if(id === 'mainContent' || id === 'mainNews') {
                    item.data('slide', item.find('.slide'));
                }
                item.data('more', item.find('.more'));

                item.data('more').css({
                    'opacity' : 0
                });

                that.itemBinding($(this));
            });

            this.panelWidth = this.containerWidth / this.items.length;
            this.maxWidth = 565;
            this.minWidth = (this.containerWidth - this.maxWidth) / (this.items.length - 1);

            this.wrapper.mouseover(function() {
                that.wrapper.addClass('active');
            });
            /* panels widths reset on mouseout */
            this.wrapper.mouseout(function(e) {
                /* checks if current hovered element is a wrapper's child */
                /* mouseout can be triggered when hovering one */
                if($(e.relatedTarget).parents('#content').length === 0) {
                    /* if not, resets panels widths */
                    for(var i = 0, j = that.items.length; i < j; i++) {
                        that.resetWidth(that.items[i]);
                    }
                    that.wrapper.removeClass('active');
                }
            });
            /* close button */
            this.wrapper.find('.firstUniverse #discover').click(function(e) {
                e.preventDefault();
                $(".flashHider").hide();
                that.removePanels();
            });
			
			this.wrapper.find('.secondUniverse #discover').click(function(e) {

                e.preventDefault();
                $(".flashHider").hide();
                
                var flashvars = {
                		xmlConfig : '/uk/sites/all/themes/georgia-pacific/swf/slideshow/healthcare/xml/config.xml'	
                };
				 var params = {
					allowScriptAccess : 'always',
					allowFullScreen : 'false',
					wmode : 'transparent'
				};

				var attributes = {
					id : 'myVideoWrapper'
				};
				swfobject.embedSWF($(this).attr('href'), /*video url*/
				'myVideoWrapper', /*element to replace*/
				'960', /*width*/
				'490', /*height*/
				'8', /*min flash version*/
				null, flashvars, params, attributes,function(){that.removePanels();});
            });
			
			
			this.wrapper.find('#discoverFirstUniverse').click(function(e) {

                e.preventDefault();
				$('.firstUniverse').show();
                $('.secondUniverse').hide();
				$('.introContentBlock2').hide();
				$('#mainSolutions h3').hide();

            });
			
			this.wrapper.find('#discoverSecondUniverse').click(function(e) {

                e.preventDefault();
				$('.firstUniverse').hide();
                $('.secondUniverse').show();
                $('.introContentBlock2').hide();
				$('#mainSolutions h3').hide();

            });
	    
	    
			arrayUrl = (window.location.search).split('=');
			if(arrayUrl[0] == '?autoOpen'){
				if(arrayUrl[1]=='horeca'){
					this.wrapper.find('.firstUniverse #discover').click();
				}else if(arrayUrl[1]=='healthcare'){
					this.wrapper.find('.secondUniverse #discover').click();
				}
			}
        },
        /* closes & removes panels */
        removePanels : function() {
            var that = this;
            this.items.stop().unbind();
            this.wrapper.unbind();
            /* no use of $.animate() facade because we don't wan't the $.stop() here */
            $('#mainSolutions').animate({
                'width' : that.containerWidth
            }, function() {
                $('#content').animate({
                    'opacity' : 0
                }, function() {
                    $(this).remove();
                    box.ui('carousel').destroy('newsCarousel');
                });
            });

            $('#mainContent').animate({
                'width' : 0
            });
            $('#mainNews').animate({
                'width' : 0
            });
        },
        /* panels bindings */
        itemBinding : function(element) {
            var that = this;
            element.bind('mouseover', function() {
                /* opens panel on hover */
                /* unbind to prevent multiple actions */
                $(this).unbind();
                that.mouseIn($(this));
            });
        },
        /* $.animate() facade  */
        animate : function(element, styles, callback) {
            var that = this;
            if(element !== $(element)) {
                element = $(element);
            }
            if( typeof (callback) !== 'function') {
                /* no callback or wrong type */
                element.stop().animate(styles, {
                    duration : 600,
                    queue : false,
                    easing : 'easeOutQuad'
                });
            } else {
                element.stop().animate(styles, {
                    duration : 600,
                    queue : false,
                    easing : 'easeOutQuad',
                    complete : function() {
                        callback.call();
                    }
                });
            }
        },
        /* panel opening animation */
        setMaxWidth : function(element, width) {
            var that = this;
            this.animate(element, {
                'width' : width
            }, function() {
                that.animate($(element).data('more'), {
                    'opacity' : 1
                });
            });

            this.setMaxStyles(element);
        },
        /* panel minimizing animation (while an other element is opened) */
        setMinWidth : function(element, width) {
            var that = this;
            /* unbind to prevent multiple actions */
            $(element).unbind();
            this.animate(element, {
                'width' : width
            }, function() {
                that.animate($(element).data('more'), {
                    'opacity' : 0
                });
                that.itemBinding($(element));
				
				$('.firstUniverse').hide();
				$('.secondUniverse').hide();
				$('.introContentBlock2').show();
				$('#mainSolutions h3').show();
            });

            this.setMinStyles(element);
        },
        /* back to default panel animation (no panel open) */
        resetWidth : function(element) {
            var that = this;
            /* unbind to prevent multiple actions */
            $(element).unbind();
            that.animate($(element).data('more'), {
                'opacity' : 0
            });
            that.animate(element, {
                'width' : this.panelWidth
            }, function() {
                that.itemBinding($(element));
            });
            that.setInitStyles(element);
        },
        /* panel mouseover actions */
        mouseIn : function(element) {
            var that = this;
            for(var i = 0, j = that.items.length; i < j; i++) {
                var currentElement = that.items[i];
                if(currentElement === element[0]) {
                    that.setMaxWidth(currentElement, that.maxWidth);
                } else {
                    that.setMinWidth(currentElement, that.minWidth);
                }
            }
        },
        /* panels & content styles at default state */
        setInitStyles : function(element) {
            element = $(element);
            var id = element.attr('id');

            if(id === 'mainSolutions') {
                this.animate(element.data('contentWrapper'), {
                    'padding-top' : 268,
                    'padding-left' : 20
                });
            }

            if(id === 'mainContent') {
                this.animate(element.data('slide'), {
                    'padding-top' : 268,
                    'padding-right' : 20,
                    'padding-left' : 20,
                    'height' : 221
                });
            }
            
            if (id === 'mainNews') {
                this.animate(element.data('slide'), {
                    'padding-top' : 268,
                    'padding-right' : 20,
                    'padding-left' : 265,
                    'height' : 221
                });
            }
            this.animate(element.data('more'), {
                'opacity' : 0
            });
			
        },
        /* panels & content styles at maximised state (hovered) */
        setMaxStyles : function(element) {
            element = $(element);
            var id = element.attr('id');

            if(id === 'mainSolutions') {
                this.animate(element.data('contentWrapper'), {
                    'padding-top' : 80,
                    'padding-left' : 20
                });
            }

            if(id === 'mainContent') {
                this.animate(element.data('slide'), {
                    'padding-top' : 80,
                    'padding-right' : 265,
                    'padding-left' : 20,
                    'height' : 409
                });
            }
            
            if(id === 'mainNews') {
                this.animate(element.data('slide'), {
                    'padding-top' : 80,
                    'padding-right' : 265,
                    'padding-left' : 20,
                    'height' : 409
                });
            }
			
			
        },
        /* panels & content styles at minimised state (another panel hovered) */
        setMinStyles : function(element) {
            element = $(element);
            var id = element.attr('id');

            if(id === 'mainSolutions') {
                this.animate(element.data('contentWrapper'), {
                    'padding-top' : 80,
                    'padding-left' : 0
                });
            }

            if(id === 'mainContent') {
                this.animate(element.data('slide'), {
                    'padding-top' : 80,
                    'padding-right' : 0,
                    'padding-left' : 20,
                    'height' : 409
                });
            }
            
            if(id === 'mainNews') {
                this.animate(element.data('slide'), {
                    'padding-top' : 80,
                    'padding-right' : 0,
                    'padding-left' : 367,
                    'height' : 409
                });
            }
            
            this.animate(element.data('more'), {
                'opacity' : 0
            });
        }
    };

    $D.ready(function() {
        /* transform #videoWrapper in a flash player with swf object
         the video url is taken from the html markup */
    	var flashvars = {
        		xmlConfig : '/uk/sites/all/themes/georgia-pacific/swf/slideshow/horeca/xml/config.xml'	
        };
        var params = {
            allowScriptAccess : 'always',
            allowFullScreen : 'false',
            wmode : 'transparent'
        };
        var attributes = {
            id : 'myVideoWrapper'
        };

        swfobject.embedSWF($('#videoWrapper a').attr('href'), /*video url*/
        'videoWrapper', /*element to replace*/
        '960', /*width*/
        '490', /*height*/
        '8', /*min flash version*/
        null, flashvars, params, attributes);

        homePanels.init();
        
       

    });
})(jQuery);

