﻿function initSite() {
    /* Is the link part of a PPC campaign? */
    if (getParameterByName('utm_source') != null) {
        jQuery.jCookie('utm_found', '1');
    }

    if (jQuery.jCookie('utm_found') == '1') {
        $(".telephone-std").addClass("telephone-alt");
        $(".telephone-std").removeClass("telephone-std");
    }

    /* Initialise Uniform for custom form elements */
    $("select, input:text, input:checkbox, input:radio, input:file").uniform();

    /* If a carousel exists (home page) initialise it */
    function mycarousel_initCallback(carousel) {
        // Disable autoscrolling if the user clicks the prev or next button.
        carousel.buttonNext.bind('click', function() {
            carousel.startAuto(0);
        });

        carousel.buttonPrev.bind('click', function() {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function() {
            carousel.stopAuto();
        }, function() {
            carousel.startAuto();
        });
    };

    jQuery(document).ready(function() {
        jQuery('#mycarousel').jcarousel({
            auto: 30,
            wrap: 'last',
            initCallback: mycarousel_initCallback
        });
    });
    
    jQuery(document).ready(function() {
            jQuery('.slideStandard').jcarousel({
            
            scroll: 1,
            initCallback: mycarousel_initCallback,
            buttonNextHTML: null,
            buttonPrevHTML: null,
            wrap: 'last'
            
        });
    });

    

    /* Scroll homepage news and testimonials */
    $("ul#newsCarousel").cycle({
        fx: "scrollUp",
        speed: 250,
        timeout: 15000,
        random: false
    });

    $("ul.blTestimonials").cycle({
        fx: "scrollDown",
        speed: 250,
        timeout: 15000,
        random: true
    });

    /* Make home columns the same height */
    $(".promoPanels .col").equalHeights();


    /* Slide up about us content */
    $(".dropitem-container .inner").hide();

    $(".dropitem-container").bind("mouseenter", function() {
        $("div.inner", this).slideDown();
    }).bind("mouseleave", function() {
        $("div.inner", this).slideUp();
    });








    /* Set our About Us testimonials carousels up */
    function mycarousel_initCallback(carousel) {
        jQuery('a.tlink').bind('click', function() {
            goToByScroll(".testesRow");
            carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('href').replace('#test', '')));
            return false;
        });
        jQuery('.navControls .next').bind('click', function() {
            carousel.next();
            return false;
        });

        jQuery('.navControls .prev').bind('click', function() {
            carousel.prev();
            return false;
        });
    };

    // Ride the carousel...
    jQuery(document).ready(function() {
        jQuery(".testes").jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });




        function teamcarousel_initCallback(carousel) {
            jQuery('.controls .nobullet a').bind('click', function() {
                carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('href').replace('#person', '')));
                return false;
            });
            jQuery('.navControls .next').bind('click', function() {
                carousel.next();
                return false;
            });

            jQuery('.navControls .prev').bind('click', function() {
                carousel.prev();
                return false;
            });
        };



        jQuery(".teamCarousel").jcarousel({
            scroll: 1,
            initCallback: teamcarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    });



    /* Code for the projects page  */   
    // Custom sorting plugin
    (function($) {
        $.fn.sorted = function(customOptions) {
            var options = {
                reversed: false,
                by: function(a) {
                    return a.text();
                }
            };
            $.extend(options, customOptions);

            $data = $(this);
            arr = $data.get();
            arr.sort(function(a, b) {

                var valA = options.by($(a));
                var valB = options.by($(b));
                if (options.reversed) {
                    return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
                } else {
                    return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
                }
            });
            return $(arr);
        };
    })(jQuery);

    // DOMContentLoaded
    $(function() {

        // bind radiobuttons in the form
        var $filterType = $('#filter input[name="type"]');
        var $filterSort = $('#filter input[name="sort"]');

        // get the first collection
        var $applications = $('.applications');

        // clone applications to get a second collection
        var $data = $applications.clone();

        // attempt to call Quicksand on every form change
        $filterType.add($filterSort).change(function(e) {

            if ($($filterType + ':checked').val() == 'all') {
                var $filteredData = $data.find('li');
            } else {
                var $filteredData = $data.find('li[data-type~=' + $($filterType + ":checked").val() + ']');
            }

            // if sorted by size
            if ($('#filter input[name="sort"]:checked').val() == "size") {

                var $sortedData = $filteredData.sorted({
                    reversed: true,
                    by: function(v) {
                        return parseFloat($(v).find('span[data-type=size]').text());
                    }
                });
            } else {

                // if sorted by name
                var $sortedData = $filteredData.sorted({
                    by: function(v) {
                        return $(v).find('strong').text().toLowerCase();
                    }
                });
            }

            // finally, call quicksand
            $applications.quicksand($sortedData, {
                duration: 1000,
                easing: "swing"
            });

        });

    });


    // Add some events to our radio buttons
    $(".pageWorkIndex #filter label").bind("click", function() {
        $(".pageWorkIndex #filter label").removeClass('selected');
        $(this).addClass('selected');
    });


    // Make our sliding panels slidey
    jQuery(document).ready(function() {
        $(".slidingPanels").slidingPanels({
            onEnterState: demoEnterState,
            onLeaveState: demoLeaveState
        });
    });

    
    /* We need to hide some parts of our contact us form by default */
    jQuery(document).ready(function() {
        $(".contactRow .yourproject").hide();
    });

    $(function() {
        $('ul.iwouldlike select').change(function(e) {

            if(this.selectedIndex==1)
                $(".contactRow .yourproject").slideDown();
            else
                $(".contactRow .yourproject").slideUp();
        });

    });
    
    
    











} 
