﻿//top navigation..........................................................
bindTopNav = function () {
    //links
    var menuItems = [{ Text: "HOME", Url: "/"},                
                { Text: "CONTACT US", Url: "contact-us.htm" },
                { Text: "LINKS", Url: "links.htm"}];

    //adding to html
    $.each(menuItems, function (index, obj) {
        var cssCls = '';
        if (obj.CssClass) cssCls = 'class="' + obj.CssClass + '"';

        var content = '<li ' + cssCls + '><a href="' + obj.Url + '">' + obj.Text + '</a></li>'
        $('ul#nav').append(content);

    });

    //set click event for current selection
    $('ul#nav li a').each(function () {

        var currentUrl = window.location.href.toLowerCase();
        var linkUrl = $(this).attr('href').toLowerCase().replace('..', '');

        if (currentUrl.endsWith(linkUrl))
            $(this).parent().addClass('active');
    });

}

//left navigation..........................................................
bindLeftNav = function () {
    //links
    var menuItems = [{ Text: "Suspension", Url: "#" },
                { Text: "Disc Brake Lathes", Url: "#", CssClass: "active" },
                { Text: "Fiberlight", Url: "#" },
                { Text: "Endoscopes", Url: "#" },
                { Text: "Motormeter", Url: "#" },
                { Text: "Dynamic Break Tester", Url: "#" },
                { Text: "Weight Bags", Url: "#"}];

    //adding to html
    $.each(menuItems, function (index, obj) {
        var item;
        if (obj.CssClass)
            item = '<li class="' + obj.CssClass + '">' + obj.Text + '</li>'
        else
            item = '<li><a href="' + obj.Url + '">' + obj.Text + '</a></li>'

        $('div#left_nav ul').append(item);

    });
}

//Three main product group..........................................................
bindProductGroup = function () {
    //products group
    var easyload = {
        TitleYellow: "Easy",
        TitleWhite: "Load",
        Image: "assets/images/blk_img.jpg",
        Description: "The revolution in single-handed loading - safely, quickly and with the minimum of effort.",
        LinkText: "Easy Load",
        LinkUrl: "products/easyload/index.htm"
    };
    var loadAlert = {
        TitleYellow: "Load",
        TitleWhite: "Alert",
        Image: "assets/images/loadalert_img.png",
        Description: "Effective overload warning at a realistic cost.",
        LinkText: "Load Alert",
        LinkUrl: "products/loadalert/index.htm"
    };
    var madSuspension = {
        TitleYellow: "Mad",
        TitleWhite: "Suspension",
        Image: "assets/images/madeg_img.png",
        Description: "Cost effective suspension upgrades for heavily laden vans, motorhomes, caravans and trailers.",
        LinkText: "Mad Suspension <br />Products",
        LinkUrl: "http://www.mad-suspension.co.uk/"
    };

    //adding to html
    var productGroup = [easyload, loadAlert, madSuspension];
    $.each(productGroup, function (index, obj) {
        var content = '<div class="blk">';
        content += '<h2><a href="{0}"><span class="yellowtxt">{1}</span> {2}</a></h2>';
        content += '<a href="{0}"><img src="{3}"  alt="" /></a>';
        content += '<p>';
        content += '<a href="{0}">{4}</a>';
        content += '</p>';
        content += '<p><a href="{0}">{5}</a> &raquo;</p>';
        content += '</div>';

        var formattedContent = content.format(obj.LinkUrl, obj.TitleYellow, obj.TitleWhite, obj.Image, obj.Description, obj.LinkText);
        $('div#blk_section').append(formattedContent);
    });

    $('div#blk_section').append('<div class="clear"></div>');

}

//Footer text ..........................................................
bindFooter = function () {
    //left footer content
    var content = 'Comptek Limited. Unit 7 RO24, Twizel Close, Stonebridge, Milton Keynes. MK13 0DX.<br />'
    content += 'Tel: 01908 220308 | Fax: 01908 316481<br />'
    content += '<a href="mailto:sales@comptek.co.uk">sales@comptek.co.uk</a>'

    $('div#footer_inner p.floatleft').append(content);

    //right footer links
    var menuItems = [{ Text: "Home", Url: "/" },
                { Text: "Contact us", Url: "contact-us.htm" },
                { Text: "Links", Url: "links.htm"}];

    //adding footer links to html
    $.each(menuItems, function (index, obj) {
        var item = '<a href="' + obj.Url + '">' + obj.Text + '</a>'

        if (index < menuItems.length - 1)
            item += ' | ';

        $('div#footer_inner p.floatright').append(item);
    });
    $('div#footer_inner p.floatright').append('<br/><br/><br/>');
    $('div#footer_inner p.floatright').append('Website by: <a href="http://www.proacumen.com">ProAcumen</a>');
}

//dom ready event ..........................................................
onDomReady = function () {
    bindTopNav();
    bindProductGroup();
    //bindLeftNav();
    bindFooter();
}
$(document).ready(onDomReady);

//prototype function........................................................
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined'
        ? args[number]
        : '{' + number + '}'
        ;
    });
};

String.prototype.endsWith = function (str) { return (this.match(str + "$") == str) }
