$(document).ready(function() {
    
    $("#menu-tab").css({'-moz-border-radius-topright': '5px', '-moz-border-radius-bottomright': '5px', '-webkit-border-top-right-radius': '5px', '-webkit-border-bottom-right-radius': '5px'});
    
    $("#menu-tab-link").toggle(function() {
        $('#menu-wrap').show();
        move_menu_tab("out");
        toggle_overlay('set');
    },
    function() {
        $('#menu-wrap').hide();
        move_menu_tab("in");
        toggle_overlay('del');
    });
        
    $(window).load(function() {
       set_elements_heights();
       
       if($('#product-page').length != 0) {
           product_highlights_areas();
       }
       
    });
    
    $('.back-button-link').click(function(event) {
        event.preventDefault();
        history.back();
    });
    
    if($('#contact-form').length != 0) {
        validate_message_form();
    }
    
    if($('#distributor-countries').length != 0) {
        $('.distributor-link').click(function(event) {
            
            event.preventDefault();
            if($('#distributor-dialog').length != 0) {
                remove_distributor();    
            }
            
            var country = $(this).attr('rel');
            show_distributor(country);
            
        });  
    }
    
    if ($.browser.msie && $.browser.version <= 6 ) {
        
        $('.default-list li').css("background-position", "0 8px");
           
    }
    
});

function show_distributor(country) {
    
    var dist = $('#'+country).html();
    var dialog = $('<div id="distributor-dialog">'+dist+'</div>');
    $('#distributors').after(dialog);
    var wrap = $('#site-content-wrap');
    
    if ($.browser.msie && $.browser.version <= 6 ) {
        
        wrap.css("height", 450);
        set_elements_heights();
        
    } else {
        var footer = $('#footer');
    
        var dialog_height = 0;
        var box_height = 0;
    
        dialog_height = dialog.height();
    
        box_height = wrap.height();
        box_height = box_height + dialog_height;
    
        wrap.css("height", (box_height - 120));
        footer.css("top", (dialog_height - 120));
    }
        
}

function remove_distributor() {
    
    var dialog = $('#distributor-dialog');
    var wrap = $('#site-content-wrap');
    
    if ($.browser.msie && $.browser.version <= 6 ) {
        
        dialog.remove();
        
    } else {
        
        var footer = $('#footer');
        var dialog_height = 0;
        var box_height = 0;
    
        dialog_height = dialog.height();
        box_height = wrap.height();
        box_height = box_height - dialog_height;
        dialog.remove();
    
        wrap.css("height", (box_height + 120));
        footer.css("top", (dialog_height + 120));
        
    }
    
}


function product_highlights_areas() {
    
    var site_content_wrap = $('#site-content');
    var dialogs = {}
    dialogs['paper-sustainable'] = {header: "Highly sustainable", body: "4CC is ECF (Elementary Chlorine Free) and labelled with the Nordic Ecolabel (the Swan), the EU Ecolabel and FSC."};
    dialogs['paper-weight'] = {header: "A wide range", body: '4CC comes in weights <br /> from 90 – 300 g/m&sup2;. Read more on the <a href="range.html">"Range"</a> page.'};
    dialogs['paper-colour'] = {header: "Toner friendly", body: "Brings colour to life while spending less of it, making your toners last longer."};
    dialogs['paper-quality'] = {header: "High quality prints", body: "Gives you sharp images, text and vivid colours."};
    
    if($('body.product').length != 0) {
        site_content_wrap.prepend('<div id="paper-sustainable" class="paper-highlight"></div>');
        site_content_wrap.prepend('<div id="paper-quality" class="paper-highlight"></div>');
    }
    
    if($('body.usability').length != 0) {
        site_content_wrap.prepend('<div id="paper-weight" class="paper-highlight"></div>');
        site_content_wrap.prepend('<div id="paper-colour" class="paper-highlight"></div>');    
    }
    
    var collection = $('.paper-highlight');
    collection.css({'-moz-border-radius': '4px', '-webkit-border-radius': '4px'});
    
    window.setTimeout(function(){
        collection.fadeTo('slow', 0.5);
    }, 300);
    
    $('.paper-highlight').mouseenter(function () {
        display_dialog($(this).attr('id'), dialogs);
    });
        
}


function display_dialog(target_id, dialogs) {
    
    var offset = $('#'+target_id).offset();
    $('#site-content').prepend('<div id="information-dialog"><h1 class="dialog-header">'+dialogs[target_id].header+'</h1><p>'+dialogs[target_id].body+'</p></div>');
    
    var dialog = $('#information-dialog');
    dialog.fadeTo(0, 0);
    
    var dialog_width = dialog.outerWidth();
    var dialog_height = dialog.outerHeight();
    
    var left_pos = (offset.left + 32) - Math.round(dialog_width / 2);
    var top_pos = (offset.top + 32) - Math.round(dialog_height / 2);
    
    dialog.offset({'top': top_pos, 'left': left_pos}).css({'-moz-border-radius': '7px', '-webkit-border-radius': '7px', 'cursor': 'pointer'}).fadeTo('fast', 1);
    
    $('div#information-dialog').mouseleave(function () {
        dialog.fadeTo('fast', 0).remove();
    });
        
}


function set_elements_heights() {
    
    var wrap = $('#site-content-wrap');
    var box_height = wrap.innerHeight();
    wrap.css("height", box_height);
    $('#site-content').height('100%');
    $('#menu-wrap').height('100%');
    
}


function move_menu_tab(direction) {
    
    var tab_offset = $('#menu-tab').offset();
    var menu_wrap_width = $('#menu-wrap').width();
    
    if(direction == "out") {
        $('#menu-tab').offset({top: tab_offset.top, left: tab_offset.left + menu_wrap_width});
    } else {
        $('#menu-tab').offset({top: tab_offset.top, left: tab_offset.left - menu_wrap_width});
    }
   
}

function toggle_overlay(action) {
    
    if(action == "set") {
        $('#site-content').prepend('<div id="overlay"></div>');
    
        $("#overlay").click(function() {
            $('#menu-tab-link').click();
        });
    
        var overlay = $('#overlay');
        overlay.width('100%');
        overlay.height('100%').fadeTo(0, 0.3);

    } else {
        $('#overlay').remove();
    }
    
}

function validate_message_form() {
    
    $("#contact-form").validate({
            rules: {
                person: {
                    required: true,
                    minlength: 3
                },
                company: {
                    required: true,
                    minlength: 3
                },
                email: {
                    required: true,
                    email: true
                },
                message: {
                    required: true
                }
            },
            errorPlacement: function(error, element) { 
                error.insertAfter(element.prev());
            }
        });
    
}

