$(function() {
    setTimeout("Core.resizeContainer();", 1000);
});

var Spotlight = {
    arrImages : [],
    arrTitles : [],
    currImage : 0,
    marginTop : 0,
    spotlightHeight : 350,
    fadeProgress : false,
    
    initialise : function() {
        $.preload(Spotlight.arrImages);
        $('#img_spotlight').attr({'src' : Spotlight.arrImages[0]});
        $('#dv_spotlight_title').html(Spotlight.arrTitles[0]);
        $('#img_spotlight').fadeIn(2000);
        setInterval('Spotlight.moveImage()', 50);
    },
    
    moveImage : function() {
        var imgHeight = $('#img_spotlight').height();
        Spotlight.marginTop--;
        $('#img_spotlight').css({'margin-top' : Spotlight.marginTop + 'px'});
        
        var imgMargin = Spotlight.spotlightHeight - imgHeight - Spotlight.marginTop;
        
        if(imgMargin > -100 && Spotlight.fadeProgress == false) {
            Spotlight.nextImage();
        }
    },
    
    nextImage : function() {
        Spotlight.fadeProgress = true;
        $('#img_spotlight').fadeOut(2000, function() {
            Spotlight.marginTop = 0;
            Spotlight.currImage++;
            if(Spotlight.currImage == Spotlight.arrImages.length) {
                Spotlight.currImage = 0;
            }
            
            $('#img_spotlight').attr({'src' : Spotlight.arrImages[Spotlight.currImage]});
            
            $('#dv_spotlight_title').slideUp(500, function() {
                $('#dv_spotlight_title').html(Spotlight.arrTitles[Spotlight.currImage]);
                $('#dv_spotlight_title').slideDown();
            });
            
            
            
            $('#img_spotlight').fadeIn(2000, function() {
                Spotlight.fadeProgress = false;
            });
        });
    }
}

var Core = {
    
    currPageId  : 'home',
    currGroupId : 0,
    crappyBrowser : false,
    
    loadPage : function(pageId) {
        
        $('#spn_menu_item_' + Core.currPageId + '_active').hide();
        $('#spn_menu_item_' + Core.currPageId + '_inactive').show();
        $('#spn_menu_item_' + pageId + '_active').show();
        $('#spn_menu_item_' + pageId + '_inactive').hide();
        
        $('#dv_menu_item_' + Core.currPageId).css({'background-color': '#f57b1e'});
        $('#dv_menu_item_' + pageId).css({'background-color': 'white'});
        
        Core.currPageId = pageId;
        
        $('#dv_spotlight_image').animate({'height' : '200px'}, 1500);
        Spotlight.spotlightHeight = 200;
        $('#dv_header').animate({'height' : '377px'}, 1500);
        $('.dv_menu_item').removeClass('active');
        $('#dv_menu_item_' + pageId).addClass('active');
           
        window.location = '#' + pageId;
        
        var pageH = $('#dv_page').height();
        $('#dv_page').css({'height' : pageH + 'px'});
        $('#dv_content').fadeOut(function() {
            $.get('pages/' + pageId + '.php',
                function(html) {
                    $('#dv_content').html(html);
                    setTimeout("Core.resizeContainer();", 200);
                }
            );
        });
        
    },
    
    resizeContainer : function() {
        var contH = $('#dv_content').height();
        contH += 100;
        $('#dv_page').animate({'height' : contH + 'px'},
            function() {
                $('#dv_content').fadeIn();
                _typeface_js.renderDocument();

                if(Core.currPageId == 'products') {
                    $('#dv_page').css({'height' : 'auto'});
                }

            });
    },
    
    showSplashIntro : function() {
        $('#splash_logo').animate({'opacity' : 1,'margin-top': '-200px'}, 1500);
        setTimeout("$('#splash_title').animate({'opacity' : 1,'margin-top': '100px'}, 1500);", 400);
        setTimeout("$('#splash_fade').fadeOut(1500);", 3000);
    },
    
    loadGroup : function(id) {
        if(Core.currGroupId == 0) {
            if(Core.crappyBrowser) {
                $('#dv_product_subgroup_container_' + id).show();
            }else{
                $('#dv_product_subgroup_container_' + id).slideDown();
            }
        }else{
            if(Core.crappyBrowser) {
               $('#dv_product_subgroup_container_' + Core.currGroupId).hide();
            }else{
                $('#dv_product_subgroup_container_' + Core.currGroupId).slideUp();
            }
        }
        
        if(Core.crappyBrowser) {
            $('#dv_product_subgroup_container_' + id).show();
            var contTop = $('#dv_product_group_item_' + id).offset();
            setTimeout("Core.scrollDown(" + (contTop.top - 20) + ");", 100);
        }else{
            var contTop = $('#dv_product_group_item_' + id).offset();
            setTimeout("$('#dv_product_subgroup_container_" + id + "').slideDown();", 500);
            setTimeout("Core.scrollDown(" + (contTop.top - 20) + ");", 1000);
        }
        
        
        $('.dv_product_group_item').css({'color' : '#ad9389'});
        $('#dv_product_group_item_' + id).css({'color' : 'white'});
        
        Core.currGroupId = id;
    },
    
    scrollDown : function(newTop) {
        
        var docH = $(document).height();
        var winH = $(window).height();
        
        if((docH - winH) < newTop) {
            newTop = (docH - winH);
        }
        
        if($.browser.safari) var animationSelector='body:not(:animated)';
         else var animationSelector='html:not(:animated)';

         // animate to target and set the hash to the window.location after the animation
         $(animationSelector).animate({ scrollTop: newTop }, 500);
    },
    
    sendMail : function() {
        $.post('inc/php/send_mail.php',{
            company : $('#company').val(),
            fullname : $('#fullname').val(),
            email : $('#email').val(),
            subject : $('#subject').val(),
            product : $('#product').val(),
            message : $('#message').val()
        },
        function(data) {
            alert('Your message has been received, we will contact you as soon as possible.');
        });
    }
}
