﻿    var TPageNumber = 1;
    var arrImgs = new Array();
    var tries = 0;
    var maxTries = 15;
    
    var topDataOpen = false;

    $(document).ready(function() {


        $(".openTopSliderContent").click(function(e) {
            e.preventDefault();
            if (topDataOpen) {
                $("#topSlider #topData #FadeIn").fadeIn(350, function() { $("#topSlider #topData").slideUp(350); });

                topDataOpen = false;
            }
            else {
                $("#topSlider #topData").slideDown(350, function() { $("#topSlider #topData #FadeIn").fadeOut(350); });
                topDataOpen = true;
            }
        });

        if ($("#OurWork .ServicesItemHolder .ServicesItem").length > 0) {
            // we need to check that all the images have loaded
            AreImagesLoaded();
        }

        // select the correct link and open 
        var path = location.pathname.substring(1);

        var FileName = path.split("/");
        path = FileName[FileName.length - 1];

        if (path != "") {
            var i = 0;

            while (path.indexOf(".aspx") == -1) {
                path = FileName[i];
                i++;
            }

            var longerpath = location.href.substring(1);

            FileName = longerpath.split("/");
            longerpath = "";
            i = 0;
            while (longerpath.indexOf(path) == -1) {
                longerpath = FileName[i];
                i++;
            }

            if (i <= FileName.length) {
                longerpath = longerpath + "/" + FileName[i] + "/";
            }
            else {
                longerpath = "";
            }

            $("#TopLinks a[href$='" + path + "']").attr("class", "Selected");

            $("#col1 a[href*='" + longerpath + "']").attr("class", "Selected");

        }
        else {
            $("#TopLinks a[href$='default.aspx']").attr("class", "Selected");
        }

        $("#col3 p,#col3Clients p").prepend("<img src=\"img/66.gif\">");
        $("#col3 p,#col3Clients p").append("<img src=\"img/99.gif\">");

        if ($("#HomePageBanner").length > 0) {
            $.preloadImages("img/businesslogo.png", "img/TheDesignLogo.png");

            setTimeout(
                function() {
                    $("#Slide2").animate(
                            { marginLeft: "0px" }, 500, HomePageBannerSlider())
                }, 2000);
        };

        if ($("div.TestimonialPage").length > 0) {
            //alert($("div.TestimonialPage:first").text());
            $("div.TestimonialPage:first").css("display", "block");

            setTimeout(ShowTestimonials, 3000);


        }

        if ($("#HomePagePortfolio").length > 0) {
            var NumberOfFadeSlides = $("#HomePagePortfolio .FaderSlide").length;

            if (NumberOfFadeSlides > 1) {

                var CurrentSlide = 1;

                setInterval(function() {
                    var NextSlide = CurrentSlide + 1;
                    if (NextSlide > NumberOfFadeSlides) {
                        NextSlide = 1;
                    }
                    $("#HomePagePortfolio .FaderSlide[id='FaderSlide" + CurrentSlide + "']").fadeOut("slow");
                    setTimeout(function() {
                        $("#HomePagePortfolio .FaderSlide[id='FaderSlide" + NextSlide + "']").fadeIn("slow");
                    }, 500);

                    CurrentSlide = NextSlide;

                }, 5000);
            }
        }

        $(".ServicesItem").hover(function() {
            $(this).children("div").fadeIn("fast");
        }, function() {
            $(this).children("div").fadeOut("slow");
        });

        $(".ServicesItem").click(function() {
            //window.location = "OurWork.aspx?sID=" + $(this).attr("sID");
            window.location = $(this).attr("to");
        });



    });
    
    function ShowTestimonials()
    {
    
        if ($("div.TestimonialPage").length>1)
        {
            var OriginalPN = TPageNumber;
        
            TPageNumber++;
            if (TPageNumber>$("div.TestimonialPage").length)
            {
                TPageNumber=1;
            }        
        
            $("div.TestimonialPage[pn='"+OriginalPN+"']").fadeOut("fast",
                function(){$("div.TestimonialPage[pn='"+TPageNumber+"']").fadeIn("slow")}
                );
        
            setTimeout(ShowTestimonials,10000);
        }
   
    }
    
    var ThisSlideIndex = 1;
    var ThatSlideIndex = 2;
    
    function HomePageBannerSlider()
    {
    
        setTimeout(function(){
    
        var ThisSlide = $("#HomePageBanner #Slider #Slide" + ThisSlideIndex);
        var ThatSlide = $("#HomePageBanner #Slider #Slide" + ThatSlideIndex);
        
        var ThisZIndex = ThisSlide.css("z-index");
        var ThatZIndex = ThatSlide.css("z-index");
        
        ThisSlide.css("margin-left","935px");
        
        ThisSlide.css("z-index",ThatZIndex);
        ThatSlide.css("z-index",ThisZIndex);
        
        var timeGap = 500*ThatSlideIndex;
        
        
        setTimeout(function(){
            
            ThisSlide.animate(
                            {marginLeft:"0px"},500,
                        HomePageBannerSlider());
        },timeGap);
        
        if (ThisSlideIndex==1)
        {
            ThisSlideIndex=2;
            ThatSlideIndex=1;
        }
        else
        {
            ThisSlideIndex=1;
            ThatSlideIndex=2;
        }        
        
        },5000);
        
    }
    

function contains(a, obj) 
{  
    var i = a.length;  
    while (i--) {    
        if (a[i] == obj) {    
            return true;    
        }  
    }  
    
    return false;
}

    function AreImagesLoaded()
    {
        // they have not yet loaded
        var Loaded=false;
        // get the list of images
        var arr = $('.ServicesItem');
        
        if ($("#OurWorkPop").length==0)
        {
            $("#OurWork").prepend("<div id=\"OurWorkPop\"><!-- --></div>");            
        }        
        
        // check that we have not already loaded them all
        if (arr.length != arrImgs.length)
        {
            // loop the images
            $.each(arr, function()
            {
                var elmt = $(this);
                // get the image name
                var imgName = '';
                //imgName = String($(this).css('background-image').substring(4));
                imgName = String(elmt.attr('bg'));
                //imgName = imgName.substring(0,imgName.length-1);
                // give the image a src
                if (imgName.substring(0,1)=='\"')
                {
                    imgName = imgName.substring(1);
                    imgName = imgName.substring(0,imgName.length-1);
                }                
                // is this image name in the arraylist?
                if (!contains(arrImgs,imgName))
                {                                     
                    // create the image
                    var thisImg = new Image;
                    //var thisImg = document.createElement('img');
                    //alert(imgName);
                    // check the onload, if found then add to the array    
                    thisImg.onload = function() {                          
                        arrImgs[arrImgs.length] = imgName; 
                        elmt.css("background-image","url('" + imgName + "')");
                        }
                    
                    thisImg.src =  imgName;
                    
                    
                }
            // end of the loop   
                      
            });
            
            tries++;
            
            if (tries>=maxTries)
            {
                Loaded=true;
            }

        }
        else
        {
            Loaded=true;
        }

        if (!Loaded)
        {
            setTimeout('AreImagesLoaded()',250);
        }
        else
        {
            $("#OurWorkPop").animate({opacity:0},350,function(){
                $("#OurWorkPop").remove();
                displayImages();
            });
            //alert(tries);
        }
        // end the function
    }