var adquatio = {
    baseUrl : '',
    debug : false,
    menuWidth : 800,
    menuShowed : false,
    imageChaing : false,
    isActualitesOrAgence : false,
    pecettaInterval : 0,
    pecettaStatus : 'tick',
    presentationIndex : 0,
    presentationInterval : 0,
    presentationArray : null,
    someConcoursImageIsTweening : false,
    isSpecialImage : false,
    main  : function(baseUrl, debug, currentUrl)
    {
        this.baseUrl = baseUrl;
        this.debug = debug;
        
        this.d("main func with arguments: ");
        this.d(arguments);

        if(currentUrl.replace("/index.php/", "") == baseUrl){
            this.firstTween();
        }else{
            $j('#menu').css('margin-left', this.menuWidth*-1);
            $j('#architectes').hide();
        }
            
        this.setAdquatioButton();
        this.initSWFAddress();
        this.hideSubMenus();
        this.setSubMenusEvents();
        this.setResizeEvents();
        this.setMenuEvents();
        
        var scope = this;
        $j('#blackBox').click(function(e){
            scope.showBlackBox(0);
            scope.hideMenu();
        });
    
        if($j.browser.webkit == true)
            $j('ul#menu li.firstLevel ul li').css('margin', '6px 0 8px 0');
        
    },
    d : function(message)
    {
        if(this.debug)
            console.log(message);
    },
    firstTween : function()
    {
        $j('#menu').css('margin-left', this.menuWidth*-1);
        $j('h1').hide();
        $j('#architectes').hide();
        var scope = this;
        setTimeout(function(){
                $j('h1').fadeIn(3000, function(e){
                    $j('#architectes').fadeIn(4000, function(e){
                        //scope.startPulseLogo(0);
                    });
                });
            },
            2500
        );
    },
    startPulseLogo : function(value)
    {   
        var scope = this;
        /*
        this.d("start pulse logo");
        setTimeout(function(){
            $j('h1 a').css('background', 'none');
            setTimeout(function(){
                $j('h1 a').css('background', 'url('+scope.baseUrl+'/assets/adquatio.png)');
                if(scope.menuShowed == false)
                    scope.startPulseLogo();
            },100);
        },3000);
        */

        $j('h1 a').animate({opacity:value}, 300, function(e){
            var nv = (value==1?0:1);
            $j('h1 a').animate({opacity:nv}, 300, function(e){
            
                setTimeout(function(){
                        if(scope.menuShowed == true){
                            if(value == 0){
                                $j('h1').animate({opacity:1}, 300);
                            }
                        }else{
                            scope.startPulseLogo((nv==1?0:1));
                        }
                    },
                    2500
                );
            
            });
            /*
            */
        });
        
    },
    initSWFAddress : function()
    {
        var scope = this;
        
        $j.address.change(function(event) {
            if(event.value == "/")
                return;
            $j('#architectes').hide();
            $j('#architectes').remove();
            scope.hideMenu();
            scope.showBlackBox(0);
            //clearInterval(this.pecettaInterval);
            scope.stopPecettaThumb();
            var url = scope.baseUrl+event.value;
            $j.ajax({
                url : url,
                success : function(response){
                    scope.d("content successfully loaded!");
                    $j("#content").fadeOut(600, function(e){
                        $j("#content").html(response);  
                        $j('a.toAddress').address();
                        $j("#content").fadeIn(600, function(e){
                            scope.stopPecettaThumb()
                        });
                        scope.stopPecettaThumb();
                    });
                },
                error : function(){
                    scope.d("error loading content!");
                },                
                dataType : 'html'
            });
        });  
        /*
        $j('ul#menu a.firstLevel').click(function() {  
            $j.address.value($(this).attr('href'));  
            return false;
        });
        */
        
        /*
        var scope = this;
        SWFAddress.addEventListener(SWFAddressEvent.CHANGE, function(e){
            scope.handleSWFAddressEvent(e);
        });
        */
        /*
        SWFAddress.addEventListener(SWFAddressEvent.INIT, function(e){
            scope.handleSWFAddressEvent(e);
        });
        */
    },
    handleSWFAddressEvent : function(e)
    {
        //this.d("handle swfaddress event ["+e.type+"]");
        /*
        switch(e.type){
            case "init":
                break;
            case "change":
                break;
        }
        */
    },
    setAdquatioButton : function()
    {
        this.d("set ad quatio button events");
        var scope = this;
        $j('h1 a').hover(function(e){
            if(!scope.menuShowed){
                scope.showMenu() 
                $j('#menu').mouseenter();
            }
        });
        $j('h1 a').click(function(e){
            if(!scope.menuShowed){
                scope.showMenu() 
                $j('#menu').mouseenter();
            }
            return false;
        });
        /*
        $j('#menu').mouseleave(function(e){
            if(scope.menuShowed)
                scope.hideMenu()
        });
        */
        /*
        $j('h1 a').toggle(
            function(e){
                if(!scope.menuShowed)
                    scope.showMenu()
                else
                    scope.hideMenu();
            },
            function(e){
                if(scope.menuShowed)
                    scope.hideMenu()
                else
                    scope.showMenu();
            }
        );*/
        
    },
    hideMenu : function()
    {
        this.d("hide the menu");
        $j('#menu').animate({marginLeft:this.menuWidth*-1}, 800);
        this.menuShowed = false;
        //this.alphaImageProject(1);
    },
    showMenu : function()
    {
        this.d("show the menu");
        $j('#menu').animate({marginLeft:0}, 800);
        this.menuShowed = true;
        //this.alphaImageProject(.5);
    },
    hideSubMenus : function()
    {
        this.d("hide sub menus");
        $j('.submenu').each(function(i, e){
            $j(e).css('display', 'none');
        });
    },
    setSubMenusEvents : function()
    {
        $j('#menu > li').each(function(i, e){
            var m = new submenu($j(e).find('ul'));
            $j(e).hover(
                function () {
                    m.showPopup();
                },
                function () {
                    m.hidePopup();                    
                }
            );
        });
        /*
        $j('#menu li').hover(
            function () {
                
            },
            function () {
                
            }
        );
        */
        /*
        $j('#menu li').hover(
            function () {
                //$j('ul', this).stop();
                //$j('ul', this).slideToggle();
                $j('ul', this).slideDown(350);
            },
            function () {
                //$j('ul', this).stop();
                //$j('ul', this).slideToggle();
                $j('ul', this).slideUp(350);
            }
        );
        */
    },
    setMenuEvents : function()
    {
        var scope = this;
        $j('#menu').mouseenter(function(e){
            scope.d("mouse entered in main menu "+scope.isActualitesOrAgence);
            if(scope.isActualitesOrAgence){
                $j('#blackBox').show();
                scope.showBlackBox(.65);
            }
        });
        $j('#menu').mouseleave(function(e){
            scope.d("mouse leaved in main menu "+scope.isActualitesOrAgence);
            if(scope.isActualitesOrAgence){
                scope.showBlackBox(0);
                scope.hideMenu();
            }
        });
    },
    setIsActualitesOrAgence : function(b)
    {
        this.d("set is actualites or agence to "+b);
        this.isActualitesOrAgence = b;
    },
    showBlackBox : function(value)
    {
        this.d("set black box to "+value);
        $j('#blackBox').animate({opacity:value}, 400, function(){
            if(value == 0)
                $j('#blackBox').hide();
        });
    },
    setResizeEvents : function()
    {
        this.d("set window resize events ");
        var scope = this;
        $j(window).resize(
            function(e){
                //console.log("on resize");
                //console.log(scope.isSpecialImage);
                
                var ww = $j(window).width();
                var wh = $j(window).height();
                
                
                $j('#blackBox').css('width', ww);
                $j('#blackBox').css('height', wh);
                
                $j("div#content").css("width", ww);
                
                
                if(scope.isSpecialImage == false)
                    $j("div#content").css("height", wh);
                
                if($j('#projectImage').length != 0){
                
                    if(scope.isSpecialImage == false){
                
                        if($j('#projectImage').hasClass('horizontal')){
                        // immagine al 100% dell'altezza
                        $j('#projectImage').css('height', wh);
                        $j('#projectImage').css('width', 'auto');
                        $j('#projectImageLink').css('width', $j('#projectImage').width());
                        $j('#projectImageLink').css('height', wh);
                        
                        if(ww-$j('#projectImage').width() > 385){
                            $j('#textInfo').css('left', $j('#projectImage').width());
                            $j('#textInfo').css('right', '');
                        }else{
                            // project test position
                            $j('#textInfo').css('left', '');
                            $j('#textInfo').css('right', 0);
                        }
                    }else{
                        $j('#projectImage').css('height', wh);
                        $j('#projectImage').css('width', 'auto');
                        $j('#projectImageLink').css('width', $j('#projectImage').width());
                        $j('#projectImageLink').css('height', wh);
                        
                        // project test position
                        $j('#textInfo').css('left', $j('#projectImage').width()+126);
                        $j('#textInfo').css('right', '');   
                    }
                    
                    }
                }
            }
        );
    
        $j(window).resize();

    },
    hideProjectText : function()
    {
        this.d("hide project text");
        $j('#info').show();
        //$j('#textInfo').animate({marginRight:-385});
        $j('#textInfo').fadeOut();
    },
    showProjectText : function()
    {
        $j('#info').hide();
        //$j('#textInfo').animate({marginRight:0});
        $j('#textInfo').fadeIn();
        if(this.menuShowed)
            this.hideMenu();
    },
    changeProjectImage : function(direction)
    {
        
        if(this.imageChanging)
            return false;
        
        imageIndex += direction;
        if(imageIndex < 0)
            imageIndex = arrImages.length-1;
        
        if(imageIndex+1 > arrImages.length)
            imageIndex = 0;
        
        this.hideMenu();
        this.d("changing image index to index "+imageIndex + " image url is "+arrImages[imageIndex]);
        
        var scope = this;
        var image = new Image();
        image.onload = function(){
            $j('#projectImage').fadeOut(400, function(){
                $j('#projectImage').attr('src', arrImages[imageIndex]);
                $j('#projectImage').parent().attr('href', arrImages[imageIndex]);
                $j('#projectImageLink').css('width', image.width);
                
                scope.isSpecialImage = false;
                
                for(var j = 0 ; j < dontResizeImages.length ; j++){
                    //alert(arrImages[imageIndex] + " == "+ dontResizeImages[j]);
                    if(arrImages[imageIndex] == dontResizeImages[j]){
                        scope.isSpecialImage = true;
                        break;
                    }
                }
                
                if(scope.isSpecialImage){
                
                    $j('#projectImage').removeClass("vertical");
                    $j('#projectImage').removeClass("horizontal");
                    
                    $j('#projectImageLink').removeClass('verticalImageHolder');
                    $j('#projectImageLink').removeClass('horizontalImageHolder');
                    
                    $j('#projectImageLink').css('margin-top', '132px');
                    $j('#projectImageLink').css('height', image.height+'px');
                    
                    $j('#projectImage').css('height', image.height+'px');
                    $j('#projectImage').css('top', '132px');
                    $j('#content').css('height', '');
                    $j('#content').css('overflow', 'hidden');
                    
                }else{

                    $j('#projectImageLink').css('margin-top', '0');
                    $j('#projectImageLink').css('height', '100%');
                
                    $j('#projectImage').css('height', '100%');
                    $j('#projectImage').css('top', '');
                    $j('#content').css('height', '100%');
                    $j('#content').css('overflow', '');
                
                    if(image.width > image.height){
                        $j('#projectImage').addClass("horizontal");
                        $j('#projectImage').removeClass("vertical");
                        $j('#projectImageLink').removeClass('verticalImageHolder');
                        $j('#projectImageLink').addClass('horizontalImageHolder');
                    }else{
                        $j('#projectImage').addClass("vertical");
                        $j('#projectImage').removeClass("horizontal");
                        $j('#projectImageLink').addClass('verticalImageHolder');
                        $j('#projectImageLink').removeClass('horizontalImageHolder');
                    }
                
                }

                $j(window).resize();
                    
                $j('#projectImage').fadeIn(400);
            });
        };
        image.src = arrImages[imageIndex];
        
        
        this.updateImagePositionText();
        this.hideProjectText();
    },
    updateImagePositionText : function()
    {
        if(imageIndex+1 < 10)
            $j('p#imageBrowser span').html("0"+(imageIndex+1));
        else
            $j('p#imageBrowser span').html(imageIndex+1);
    },
    alphaImageProject : function(value)
    {
        if($j('#projectImage') != null){
            this.d("alpha image project to "+value);
            if(value == 1){
                $j('#projectImageLink').css('background-color', 'transparent');
            }else{
                $j('#projectImageLink').css('background-color', 'black');
                $j('#projectImageLink').css('width', $j('#projectImage').width());
            }
           $j('#projectImage').animate({opacity: value});
           //$j('#projectImage').css('opacity', value);
        }
    },
    startPecetta : function(firstImageUrl)
    {
        this.d("start follow mouse");
        $j('#pecetta').html('<img src="'+firstImageUrl+'" alt="" />');
        $j('#pecetta').show();
        $j(document).mousemove(function(e){
            $j('#pecetta').css('top', e.pageY+15);
            $j('#pecetta').css('left', e.pageX+15);
        });

    },
    stopPecetta : function()
    {
        $j('#pecetta').hide();
        $j(window).unbind('mousemove');
    },
    /*news*/
    showNewsBrowser : function(index, max)
    {
        console.log("show news browser");
        console.log(arguments);
    },
    changeNewsImage : function(newImage, newIndex)
    {
        if(this.imageChanging)
            return false;
        var image = new Image();
        this.imageChanging = true;
        var scope = this;
        
        $j('p.imagesNumbers a').each(function(i, e){
            $j(e).removeClass('current');
        });
        
        $j('p.imagesNumbers a#linkImage'+newIndex).addClass('current');
        
        image.onload = function(){
            $j('#newsImage').fadeOut(400, function(){
                $j('#newsImage').attr('src', newImage);
                $j('#newsImage').fadeIn(400, function(){
                    scope.imageChanging = false;
                });
            });
        };
        image.src = newImage;
    },
    highlightThumb : function(thumbID)
    {
        $j('#news a img').each(function(i, e){
            if($j(e).parent().attr('id') != thumbID){
                $j(e).animate({opacity:.25});
            }
        });
        $j('#'+thumbID+' img').animate({opacity:1});
    },
    highlightThumbs : function(type)
    {
        if(this.someConcoursImageIsTweening)
            return;
        
        this.d('highlightThumbs '+type);
        this.someConcoursImageIsTweening = true;
        notType = type == 'concours' ? 'chantiers' : 'concours';
        
        if(type == "chantiers"){
            $j('#spotChantiers').addClass('chantiersHighlighted');
            $j('#spotConcours').removeClass('concoursHighlighted');
            //css('color', '#');
        }else{
            //$j('#spotConcours').css('color', '#1F4AFF');
            $j('#spotChantiers').removeClass('chantiersHighlighted');
            $j('#spotConcours').addClass('concoursHighlighted');
        }
        
        var scope = this;
        $j('#news a.'+type+' img').each(function(i, e){
            $j(e).stop();
            $j(e).animate({opacity:1},400,function(e){
                scope.someConcoursImageIsTweening = false;
            });
        });
        $j('#news a.'+notType+' img').each(function(i, e){
            $j(e).stop();
            $j(e).animate({opacity:.25});
        });
    },
    deHighlightAllThumbs : function(){
        this.d("de highlight all thumbs");
        //return;
        $j('#news a img').each(function(i, e){
            $j(e).stop();
            $j(e).animate({opacity:1});
        });
        $j('#spotChantiers').removeClass('chantiersHighlighted');
        $j('#spotConcours').removeClass('concoursHighlighted');
        this.someConcoursImageIsTweening = false;
    },
    startPecettaThumb : function(label, typeLabel, type)
    {
        pecettaStatus = 'tick';
        this.d("start pecetta thumb arguments are:");
        this.d(arguments);
        $j('#pecetta').html();
        var frames = "";
        frames += '<span id="pecettaLabel">'+label+'</span>';
        frames += '<span id="pecettaType" style="display:none;" class="'+type+'">'+typeLabel+'</span>';
        $j('#pecetta').html(frames);
        $j('#pecetta').show();
        $j(document).mousemove(function(e){
            $j('#pecetta').css('top', e.pageY+15);
            $j('#pecetta').css('left', e.pageX+15);
        });

        clearInterval(this.pecettaInterval);
        var scope = this;
        this.pecettaInterval = setInterval(function(e){
            if(scope.pecettaStatus == 'tick'){
                scope.pecettaStatus = 'tack';
                $j('#pecettaLabel').hide();
                $j('#pecettaType').show();
            }else{
                scope.pecettaStatus = 'tick';
                $j('#pecettaLabel').show();
                $j('#pecettaType').hide();                
            }
        },
        1000)
    },
    stopPecettaThumb : function()
    {
        clearInterval(this.pecettaInterval);
        $j('#pecetta').hide();
        $j(window).unbind('mousemove');
    },
    initPresentation : function(arrImages)
    {
        this.d("start presentation diaporama "+arrImages);
        this.presentationArray = arrImages;
        this.presentationIndex = 0;
        clearInterval(this.presentationInterval);
        this.startChangePresentationImage();
    },
    startChangePresentationImage : function()
    {
        this.d("start change presentation image");
        var scope = this;
        setTimeout(
            function(){
                scope.changePresentationImage();
            },
            5000
        );
    },
    changePresentationImage : function()
    {
        this.d("change presentation image");
        if(this.presentationIndex+1 == this.presentationArray.length){
            this.presentationIndex = 0;
        }else{
            this.presentationIndex++;
        }
        var imgLoad = new Image();
        imgLoad.src = this.presentationArray[this.presentationIndex];
        var scope = this;
        imgLoad.onload = function(){
            $j('#presentationImage').fadeOut(400, function(){
                $j('#presentationImage').attr('src', scope.presentationArray[scope.presentationIndex]);
                $j('#presentationImage').fadeIn(400, function(e){
                    scope.startChangePresentationImage();
                });
            });
        };
    }
};


function submenu(el){
    
    this.timeout = null;
    //this.id = 'ul#'+id;
    this.el = el;
    
    this.showPopup = function(){
        clearTimeout(this.timeout);
        if(el.css('display') == 'none'){
            this.timeout = setTimeout(function(){ el.slideToggle() }, 400);
        }
    };
    
    this.hidePopup = function(){
        if(el.css('display') == 'none'){
            clearTimeout(this.timeout);
        }else{
            this.timeout = setTimeout(function(){ el.slideToggle() }, 300);
          }
    }
    
}

