(function($){
    $(function(){
        //Main menu                
        $("ul#menu li").hover(function(){
            //Change the z-index to show next box over the others
            $(this).css("z-index", parseInt($(this).css("z-index"))+5);
            
            //Show and Animate the hidden list
            $('ul:first',this).animate({
                opacity: 'visible',
                height:  'toggle'
                }, 300);                
            }, function(){
                //Return to normal z-index
                $(this).css("z-index", parseInt($(this).css("z-index"))-5);
                
                //Hide with animation the list
                $('ul:first',this).animate({
                    opacity: 'hide',
                    height:  'toggle'
                }, 300);
            }
        );
        
        //Rotator
        $("div#rotator div.cycleFrame").cycle({fx: 'fade', speed: 500, timeout: 5500});
        
        //Colorbox
        $('a.colorbox').colorbox({rel: 'gallGroup', title: false});
        
        //Cufon
        Cufon.replace(".cufon");
        Cufon.replace(".downloadFiles h3");
        
        //Image Swap for the gallery
        produsctImageSwitching();
        
        //Handle Frontpage firm products image swap
        frontpageProductsImageSwap();
        
        //Show Producttype's products tooltip
        showProductsTooltip();
    });    
    
    //Handle Frontpage firm products image swap
    function frontpageProductsImageSwap(){
        var effect = new Array('scrollDown', 'scrollUp', 'scrollLeft', 'scrollRight');
        
        //If it's browser IE below version 9 add background for the transparency
        if( $.browser.msie && ($.browser.version < 9) ){            
            $("div#content div.threeCol a.direction").each(function(){
                $("div.icons img", $(this)).addClass("ielte8");
            });
            
            effect.shift();
            effect.shift();
        }
        
        $("div#content div.threeCol a.direction").hover(function(){                
            $("div.icons", $(this)).cycle({ 
                fx: effect[Math.floor(Math.random()*effect.length)],
                delay: -1500,
                timeout: 2000,
                speed: 500 
            }); 
        }, function(){
            //$("div.icons", $(this)).cycle("destroy").removeAttr('style');
            $("div.icons", $(this)).cycle("pause");
            $("div.icons img", $(this)).removeAttr('style');
            
        });
    }    
    
    function produsctImageSwitching(){
        $('div.catalogue-Gallery ul.listImages li a').click(function(){
            var destA = $(this).parent().parent().parent('div.catalogue-Gallery').find('div.bigPic > a');
            var destImg = $('img', destA);
            var srcImg = $(this).find('img');
           
           destImg.attr({
                'alt': srcImg.attr('alt'),
                'title': srcImg.attr('title'),
                'src': $(this).attr('href')
           });
           
           destA.attr({
                'title': srcImg.attr('title'),
                'href': $(this).attr('href')
           })
           
           
           return false; 
        });

        $('div.catalogue-Gallery div.bigPic > a').click(function(event){
            event.preventDefault();
            
            //Remove current item from the group
            $('a[href="'+$(this).attr('href')+'"]').removeAttr('rel');
        
            // Build up the list of related colorbox objects
            $('div.catalogue-Gallery a.cbox').colorbox({
                scalePhotos: true,
                maxWidth: '640px',
                maxHeight: '480px'
            });
        
            // Open the specific link's colorbox
            $.colorbox({
                href: $(this).attr('href'),
                scalePhotos: true,
                maxWidth: '640px',
                maxHeight: '480px',
                rel: 'cboxGroup'
            });
            
            //Attach the current item to the group again
            $('a[href="'+$(this).attr('href')+'"]').attr('rel', 'cboxGroup');
        });

    }
    
    //Show Producttype's products tooltip
    function showProductsTooltip(){
        $("div#producttypeCategoryListing ul li a.showTitle")
            .tooltip({
                tipClass: 'producttipeTooltip',
		          effect: 'fade',
		          fadeOutSpeed: 10,
                  delay: 0,
		          position: "bottom center",
		          offset: [3, 20]
            });
    }
})(jQuery.noConflict());


//Function required for the Flash+Ajax handle
function flashAjax( url ){
    (function($){
        /* Uncomment on debugging
        try{
            console.log("Ajax function fired with url: "+url);    
        }catch( e ){
            alert( "Ajax function fired with url: "+url );
        }
        */
        
        //Non-ajax response
        try{
            url = url.replace('?ajax=1', '');
            url = url.replace('&ajax=1', '');   
        }catch(e){}
        window.location = url;
        
        /* Uncomment bellow for ajax response
        //Show loading image

        $('div.glide-flash-map div.content').html('<img src="'+bgmap.loading+'" class="loading" />');
        
        //Try to clean the &amp; from the query string
        try{
            for(i = 0; i < 10; i++){
                url = url.replace('&amp;', '&');   
            }
        }catch(e){}
        
        //Make the ajax request
        if( url != 'undefined' ){
            $.get(url,
                function( response ){
                     $('div.glide-flash-map div.content').html( response );
                     //Handle ajaxable links
                     $('a[href*="ajax=1"]').click(function(){
                        flashAjax( $(this).attr('href') );
                        return false;
                    });
                    //Cufon support
                    Cufon.replace(".cufon");
                }
            );
        }else{
            //If there is not an ajax request clear the div
            setTimeout(function(){
                $('div.glide-flash-map div.content').html("");
            }, 500);
        }
        */
    })(jQuery.noConflict());
}
