var ie6 = false, ie7 = false, products_highlight = false, loading = false;
var timeout = false;

function updateMenu() {
    var m = $('#menu').offset().left;
    if ($('#menu .dock-line').is(':visible') && m > 0) {
        $('#menu .dock-line').css({
            'width': m * 2,
            'left': - (m * 2) + 7
        });
        return true;
    }
    return false;
}

function initPage(to) {
    if (to != 'home') {
        window.clearTimeout(bgslide);
        var act = $('#menu li.act').length ? $('#menu li.act:first') : false;
        var m = $('#menu').offset().left;
        if (!$('#menu .dock-line').is(':visible')) {
            $('#menu .dock-line').css({
                'width': 0,
                'left': 7,
                'display': 'block',
                'opacity': 1,
                'top': act ? $(act).position().top + 4 : '50%'
            })
            .animate({
                'width': m * 2,
                'left': - (m * 2) + 7
            });
        } else {
            $('#menu .dock-line').animate({
                'top': act ? $(act).position().top + 4 : '50%'
            });
        }
    } else {
        $('#menu .dock-line').animate({
            'opacity': 0
        }, 600, 'swing', function() {
            $(this).hide();
        });
    }
}

function slide(to, tog) {
    if (typeof tog != 'undefined' && $(tog).length) {
        $(tog).parents('ul:first').find('li.act').removeClass('act');
        $(tog).parent('li').addClass('act');
    }
    
    initPage(to);
    if ($('body').scrollLeft() > 0) {
        $('#outer').css('left', - $('body').scrollLeft());
        $('body').scrollLeft(0);
    }
    if (typeof to != 'undefined') {
        var sc = 0;
        if (to == 'home') { 
            bgslide = window.setTimeout(function() {
                nextImage($('#home .background-wrapper:first'));
            }, 5000);
        }
        else if (to == 'products') { sc = 20; }
        else if (to == 'order') { sc = 40; }
        else if (to == 'contact') { sc = 60; }
        else if (to == 'imprint') { sc = 80; }
        
        sc = ie6 !== true ? '-' + (sc * 10) + '%' : '-' + sc + '%';
        if (parseInt($('#outer').css('top')) < 0) {
            $('#outer').animate({
                'top': '0%'
            }, 960, 'easeInOutQuart', function() {
                $(this).animate({
                    'left': sc
                }, 960, 'easeInOutQuart');
            });
        } else {
            $('#outer').stop().animate({
                'left': sc
            }, 1200, 'easeInOutQuart');
        }
        
    }
    
    return false;
}

function product(to, tog, fn) {
    $('.top-product').removeClass('top-product');
    $('#p_' + to).addClass('top-product');
    
    if (typeof fn != 'function') {
        fn = function() {}
    }
    if (to == 'hair-paint' && !$('#details').hasClass('filled')) {
        fn = function() {
            getNuances($('ul.colors li a:eq(13)'));
        }
    }
    
    $('#l_products').parents('ul:first').find('li.act').removeClass('act');
    $('#l_products').parent('li').addClass('act');
    initPage('products');
    
    $('#outer').stop().animate({
        'left': ie6 !== true ? '-' + (20 * 10) + '%' : '-' + 20 + '%',
        'top': '-200%'
    }, 960, 'easeInOutQuart', fn);
    
    return false;
}

function exposeLoad(color) {
    color = typeof color != 'undefined' && color !== false ?
        color : 
        '#333';
    $('.expose').css('display', 'block').animate({
        'opacity': 0.95,
        'backgroundColor': color
    }, 720, 'easeOutQuart');
}

function exposeClose() {
    $('.expose').animate({
        'opacity': 0
    }, 400, 'linear', function() {
        $(this).hide();
    });
}

function resetProducts(products) {
    $(products).removeClass('exposed')
    .animate({
        'opacity': 1
    });
    exposeClose();
}

function hairBG(url) {
    url = url.replace(/["']{1}/gi,"");
    $('#hair .activeslide').addClass('prevslide').removeClass('activeslide');
    $('#hair .nextslide').hide().attr('src', url).removeClass('nextslide').addClass('activeslide').fadeIn(500);
    $('#hair .prevslide').removeClass('prevslide').addClass('nextslide');
}

function getNuances(a) {
    loading = true;
    $('.top-product').addClass('loading');
    $.get($(a).attr('href'), {},
        function(data){
            $('#details').addClass('filled').animate({
                'opacity': 0
            }, 240, 'linear', function() {
                var dif = $('#details').offset().left - $(a).offset().left - $(a).outerWidth() + 13;
                
                $('#details').html(data);
                
                if ($(a).position().top - 
                    $(a).parents('ul:first').position().top - 
                    $('#details ul:first').position().top + 
                    $('#details').outerHeight() >= $(a).parents('ul:first').outerHeight()) 
                {
                    $('#details #hp-content').insertBefore($('#details #hp-list'));
                } else {
                    $('#details #hp-list').insertBefore($('#details #hp-content'));
                }
                
                $('#details').animate({
                    'opacity': 1,
                    'top': $(a).position().top - $(a).parents('ul:first').position().top - $('#details ul:first').position().top
                }, 500, 'swing');
                
                if (!ie6 && !ie7) {
                    $('#details .dock-line:first')
                    .css({
                        'top': $('#details ul:first').position().top + 31,
                        'display': 'block',
                        'opacity': 0
                    }).animate({
                        'opacity': 1,
                        'width': dif,
                        'left': - dif + 13
                    }, 640, 'easeInQuart');
                }
                hairBG($('#details ul li a:first').css('backgroundImage').replace(/^url|[\(\)]/g, ''));
                
                $('#details ul li a').hover(
                    function() {
                        window.clearTimeout(timeout);
                        $('#details .hovered').removeClass('hovered');
                        $('#details ul li a.active').attr('class', 'active-hidden');
                        $(this).addClass('hovered');
                    }, function() {
                        timeout = window.setTimeout(function() {
                            $('#details .hovered').removeClass('hovered');
                            $('#details ul li a.active-hidden').attr('class', 'active');
                        }, 500);
                    }
                ).click(function() {
                    $('#details a.active, #details a.active-hidden')
                        .removeClass('active').removeClass('active-hidden');
                    $(this).addClass('active');
                    
                    $('#details h3:first').text($(this).attr('title'));
                    $('#details p:first').html($(this).html());
                    hairBG($(this).css('backgroundImage').replace(/^url|[\(\)]/g, ''));
                    return false;
                });
                
                $('.top-product').removeClass('loading');
                loading = false;
            });
            
        }
    );
    
    return false;
}

jQuery().ready(function() {
    $('#menu .dock-line').hide();
    
    $('.expose').css({
        'opacity': 0
    });
    
    var startpage = $('body').attr('id').split('_');
    startpage = startpage[1];
    
    $('a[rel=external]').attr('target', '_blank');
    $('a').click(function() {
        $(this).blur();
    });
    
    if (!($.browser.msie && $.browser.version < 7)) {
        if ($.browser.msie && $.browser.version == 7) {
            ie7 = true;
        }
        
        $('#menu').hide().css({
            'left': 0,
            'opacity': 0
        }).show();
        
        if (startpage == 'home') {
            $('#claim').css('opacity', 0);
            window.setTimeout(function() {
                $('#menu').animate({
                    'left': '50%',
                    'opacity': 1
                }, 1400, 'easeInOutQuart', function() {
                    initPage(startpage);
                });
            
                $('#claim').animate({
                    'opacity': 1
                }, 1200, 'easeInSine');
            }, 320);
        } else {
            $('#menu').animate({
                'left': '50%',
                'opacity': 1
            }, 1000, 'easeInOutQuart', function() {
                initPage(startpage);
            });
        }
    } else {
        ie6 = true;
        initPage(startpage);
    }
    
    $.fn.supersized.options = {  
        startwidth: 1024,  
        startheight: 768,
        vertical_center: 0,
        align_bottom: 0,
        slideshow: [
            webroot + 'img/content/backgrounds/n/model-1.jpg',
            webroot + 'img/content/backgrounds/n/model-2.jpg',
            webroot + 'img/content/backgrounds/n/model-3.jpg',
            webroot + 'img/content/backgrounds/n/model-4.jpg',
            webroot + 'img/content/backgrounds/n/model-5.jpg'
        ],
        anim_bg: 0
    };
    $('.background-wrapper').find('img').attr('alt', '').end().supersized();
    $('.background-wrapper').before('<div style="position:absolute;'
        + 'width:100%;'
        + 'height:100%;'
        + 'left:0;'
        + 'top:0;'
        + 'z-index:1;"></div>');
    
    var loc = window.location.hash;
    if (loc.length <= 1) {
        loc = 'home';
    } else {
        loc = loc.substr(1);
    }
    if ($('#menu a#l_'+loc).length && $('#menu li.act').length < 1) {
        $('#menu a#l_'+loc).parent().addClass('act');
    }
    
    $('#menu ul li a').each(function() {
        var l = $(this).attr('href').substr($(this).attr('href').lastIndexOf('/')+1);
        $(this).click(function() {
            slide(l, $(this));
            return false;
        });
    });
    $(window).bind("resize load", function(){
        updateMenu();
    });
    
    var products = $('#products-index li a');
    $(products).each(function() {
        $(this).data('oTitle', $(this).attr('title'));
        $(this).removeAttr('title');
        
        /*
        var p = $(this);
        $(window).bind('load', function() {
            var cl = $(p).parent().attr('class');
            if (cl != '') {
                $('.' + cl).not($(p).parent()).css({
                    'position': 'absolute',
                    'left': $(p).position().left,
                    'top': $(p).position().top
                });
            }
        });
        */
        
        var l = $(this).attr('href').substr($(this).attr('href').lastIndexOf('/')+1);
        $(this).click(function() {
            resetProducts(products);
            return product(l, $(this));
        });
    });
    $(products).hoverIntent(
        function() {
            var a = $(this);
            window.clearTimeout(products_highlight);
            $(products).not($(this)).removeClass('exposed')
            .animate({
                'opacity': 0.1
            });
            $(this).addClass('exposed').animate({
                'opacity': 1
            });
            $('#product-heading, #product-text').stop().animate({
                'opacity': 0
            }, 240, 'swing', function() {
                $('#product-heading').empty().append($(a)
                .find('img.heading:first').clone()).animate({
                    'opacity': 1
                }, 320);
                
                $('#product-text').html($(a).data('oTitle'))
                .css('color', $(a).hasClass('light') ? '#000' : '#FFF').animate({
                    'opacity': 1
                }, 320);
            });
            exposeLoad($(this).hasClass('light') ? '#CCC' : false);
        },
        function() {
            products_highlight = window.setTimeout(function() {
                resetProducts(products);
                $('#product-heading, #product-text').empty();
            }, 1200);
        }
    );
    $('a.product-back').click(function() {
        slide('products');
        return false;
    });
    
    $('ul.colors li a').click(function() {
        return loading ? false : getNuances($(this));
    });
    
    $('a[rel=product]').click(function() {
        var l = $(this).attr('href').substr($(this).attr('href').lastIndexOf('/')+1);
        return product(l, $(this));
    });
    
    $('form .input input, form textarea').focus(function() {
        if (this.value == this.defaultValue && 
            $(this).parent().prev('label').text() == this.defaultValue) {
            $(this).val('');
        }
    }).blur(function() {
        if (!this.value.length || $(this).val() == $(this).parent().prev('label').text()) {
            $(this).removeClass('focused')
            if ($(this).parent().prev('label').text() == this.defaultValue) {
                $(this).val(this.defaultValue);
            } else {
                $(this).val($(this).parent().prev('label').text());
            }
        } else {
            $(this).addClass('focused');
        }
    }).each(function() {
        if ($(this).parent().prev('label').text() != this.defaultValue) {
            $(this).addClass('focused');
        }
    });
});
