// DOM ready

$(document).ready(function() {


    $('#image-holder').fadeIn(1000);
    $("#content").fadeIn(1000, function() { $(this).fixClearType() });

    //Cleartype fix
    jQuery.fn.fixClearType = function() {
        return this.each(function() {
            if (!!(typeof this.style.filter && this.style.removeAttribute))
                this.style.removeAttribute("filter");
        })
    }


    // Now an object array. Image path, title and body properties to each one
    // The body and the title can take html.  So you can add links and so on.  Make sure things are properly html encoded.
    var images = [
         	{ image: 'images/services/small/planning.jpg', title: 'Planning', body: 'A key aspect to the service that Staniforth Architects offers. We have an excellent conversion ratio on projects ranging from large industrial schemes, to change of use, sensitive conservation area development, open countryside, section 106 negotiation and high density. We are able to offer appeal assistance and pre-application advice and consultation. We have developed a trusting relationship with a number of regional Local Planning Authorities along with planning panels and officers.' }
			, { image: 'images/services/small/masterplanning.jpg', title: 'Master Planning', body: 'With a particular passion for master planning and urban design we offer a fresh and experienced unique service. Having experience of urban extensions, new town design, industrial mixed use, phasing and city centre / deprived area redevelopment. We have access to a number of recognised consultants and companies.' }
			, { image: 'images/services/small/landscape.jpg', title: 'Landscape Design', body: 'Staniforth Tate Limited, Landscape Design has secured and implemented a large number of commercial and residential projects. Advice given from design / feasibility stage through to site implementation. Commercial contracts undertaken. We are able to offer advice on TPO\'s through to forestry management. Swift consultation and supply of commercial and residential landscape schemes for planning submission / condition compliance.' }
			, { image: 'images/services/small/sustainability.jpg', title: 'Sustainability', body: 'We have a strong code to promote and enhance sustainable design and maintain a sustainable working environment. With a number of our staff trained in various aspects of sustainability and renewable energies, along with our ability to offer pre and post construction consultation and building testing services we are able to offer an ever expanding service.' }
			, { image: 'images/services/small/epc.jpg', title: 'SAP / SEBM / EPC', body: 'Approved staff assessors are able to offer SAP, SBEM and EPC certification for any project. EPC\'s are required for all newly constructed dwellings and are a key part of the European Performance of Buildings Directive.' }
			, { image: 'images/services/small/csh.jpg', title: 'Code for Sustainable Homes', body: 'On the 27 February 2008 the Government confirmed a mandatory rating against the Code will be implemented for new homes from 1 May 2008. The Code measures the sustainability of a new home against categories of sustainable design, rating the \'whole home\' as a complete package.  The Code sets minimum standards for energy and water use at each level and, within England, replaces the EcoHomes scheme, developed by the Building Research Establishment (BRE). We have approved assessors able to offer full assessment and consultation services.' }
			, { image: 'images/services/small/cdm.jpg', title: 'Planning Supervisor', body: 'Many building sites, including small works, are covered by the Construction (Design and Management) Regulations 2007 (CDM), which are enforced by the Health and Safety Executive (HSE). The purpose of CDM is to ensure that health and safety issues are co-ordinated and managed throughout all stages of a construction project in order to reduce accidents and ill health. Where CDM applies, you will have legal duties to discharge which are enforceable in a court of law. A Planning Supervisor (CDM Co-ordinator) is the key adviser to the client and has responsibility for co-ordinating the health and safety aspects of design. Click here for <a href="pdf/clients-guide-2007.pdf">a client\'s guide to the CDM Regulations</a>.' }
			, { image: 'images/services/small/law.jpg', title: 'Legal Consultation', body: 'With an in-house lawyer we are able to offer assistance with most construction and development law issues. From land purchase, including site acquisition / covenant issues to construction contract dispute and administration.'}];


    var largeImages = new Array();

    $("ul.sub-nav *").remove();

    //Creates all the large images and hides them so they can be picked up by the shadowbox.
    //Also creates Sub Nav
    $(images).each(function(index) {
        var imgLink = $('<a rel="shadowbox[MustangThumbs];options={counterType:\'skip\',continuous:true,animSequence:\'sync\',fadeDuration:0.1,resizeDuration:0.1}" href="' + this.image.replace(/\/small[\/]/gi, "/largest/") + '"><img src="' + this.image.replace(/\/small[\/]/gi, "/large/") + '" /></a>');
        $("#image-holder").append(imgLink);
        var newSubItem = $('<li><a href="#">' + this.title + '</a></li>');
        newSubItem[0].bo = this;
        this.index = index;
        this.largeImageLink = imgLink[0];
        $("ul.sub-nav").append(newSubItem);
    });

    $("#image-holder a").css({ display: "none" });
    $($("#image-holder a").get(0)).css({ display: "block" });

    $("ul.sub-nav li").click(function() {
        $("#image-holder a:visible").fadeOut();
        $(this.bo.largeImageLink).fadeIn();
        //$($("#image-holder a").get(this.bo.index)).fadeIn();
        $("#hTitle").html(this.bo.title);
        $("#pBody").html(this.bo.body);
        return false;
    });

    //Loads first image to main pic.
    if (images.length > 0) {
        $($("#image-holder a").get(0)).fadeIn();
        $("#hTitle").html(images[0].title);
        $("#pBody").html(images[0].body);
    }


    // images length
    var max = $(images).length;
    // at least 1 image exist
    if (max > 0) {
        // create the UL element
        var ul = $('<ul id="portfolio"></ul>');
        // append to div#wrapper
        $(ul).appendTo($('#thumbnails'));
        // load the first image
        LoadImage(0, max);
    }

    // function of loading image
    // params: (int) index of image in array, (int) length of images array
    function LoadImage(index, max) {
        // if current index is lower then max element (max-1)
        if (index < max) {
            // create the LI, add loading class
            var list = $('<li id="portfolio_' + index + '"></li>').attr('class', 'loading').css({ cursor: "pointer" });

            list.click(function() {
                $("#image-holder a:visible").fadeOut();
                $($("#image-holder a").get(index)).fadeIn();
                $("#hTitle").html(images[index].title);
                $("#pBody").html(images[index].body);
            });


            // append to UL
            $('ul#portfolio').append(list);
            // current LI
            var curr = $("ul#portfolio li#portfolio_" + index);
            // new image object
            var img = new Image();
            // image onload
            $(img).load(function() {
                $(this).css('display', 'none'); // since .hide() failed in safari
                $(curr).removeClass('loading').append(this);
                $(this).fadeIn('slow', function() {
                    // once the current loaded, trigger the next image
                    LoadImage(index + 1, max);
                });
            }).error(function() {
                // on error remove current
                $(curr).remove();
                // trigger the next image
                LoadImage(index + 1, max);
            }).attr('src', images[index].image);
        }
    }


});


