﻿/// General scripts for day to day running on site.
var Peach = {

    /// <summary> 
    /// Utilizes jQuery feature detection to return the Internet Explorer browser number.
    /// </summary> 
    GetIEVersion: function () {
        var $browser = 0;
        // The browser is IE 6 - 8.
        if (!jQuery.support.leadingWhitespace) {

            // IE 6 & 7.
            if (!jQuery.support.boxModel) {

                !jQuery.support.opacity && !window.XMLHttpRequest ? $browser = 6 : $browser = 7;
            }
            else {
                $browser = 8;
            }
        }
        return $browser;
    },


    /// <summary> 
    /// Creates the slide in and out effect for the banners
    /// </summary> 
    BannerRotation: function () {
        if ($("#carousel").length > 0) {
            var $carousel = $("#carousel"),
			    $list = $carousel.find("ul"),
                $listItems = $list.find("li"),
                myArray = [],
                regWidth = $carousel.width(),
                combinedWidth = 0,
                callback = null,
			    autoScroll = true,
			    direction = 1, // 1 or -1
			    delaySpeed = 8500,
			    slideSpeed = 1000;

            $listItems.filter(":first").clone().appendTo($list);
            $listItems.filter(":last").clone().prependTo($list);

            $list.css({ "margin-left": -(regWidth) });

            $listItems = $list.find("li");

            callback = function () {
                var $ul = jQuery("#carousel ul"),
					currentMargin = ~ ~($ul.css("margin-left").replace("px", "")),
					left = Math.round(currentMargin + (regWidth * direction));

                $ul.animate({
                    "margin-left": left
                }, slideSpeed, function () {
                    if (left <= ((combinedWidth - regWidth) * -1)) {
                        $ul.css({ "margin-left": (regWidth * -1) });
                    }
                    else if (left >= 0) {
                        $ul.css({ "margin-left": ((combinedWidth - regWidth - regWidth) * -1) });
                    }

                    setTimeout(callback, delaySpeed);
                });
            };

            len = $listItems.length;

            combinedWidth = regWidth * len;

            $list.css("width", combinedWidth);

            if (autoScroll === true) {
                setTimeout(callback, delaySpeed);
            }
        }
    }

};

/// <summary> 
/// Launch the scripts when all elements are loaded in the DOM.
/// </summary>
jQuery(window).ready(function () {


    Peach.BannerRotation();

});

/* crazy egg code */

(function(){
    jQuery(function(){
        var crazy = [],
			pageHref = document.location.href,
			crazyLen = 0,
			i = 0,
            documentProtocol = document.location.protocol;
		
		crazy.push({
			"url": "http://www.stewartmilnehomes.com/ardgowan-rise-ardmore.aspx?did=102&developmentid=0&hid=0&&housetypeid=1109&pricerange=0&bedrooms=0&hometypeid=0&townid=0&regionid=0",
			"src": documentProtocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0009/0388.js?"+Math.floor(new Date().getTime()/3600000)
		});
		
		crazy.push({
			"url": "http://www.stewartmilnehomes.com/contact-us.aspx",
			"src": documentProtocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0009/0388.js?"+Math.floor(new Date().getTime()/3600000)
		});
		
		crazy.push({
			"url": "http://www.stewartmilnehomes.com/homes-search-results.aspx?Page=13&RegionID=11&TownID=0&HomeTypeID=0&Bedrooms=0&PriceRange=0&DevelopmentID=0&dID=0",
			"src": documentProtocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0009/0388.js?"+Math.floor(new Date().getTime()/3600000)
		});
		
		crazy.push({
			"url": "http://timbersystems.stewartmilne.com",
			"src": documentProtocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0009/0388.js?"+Math.floor(new Date().getTime()/3600000)
		});	

        crazy.push({
			"url": "http://timbersystems.stewartmilne.com/timber-systems.aspx",
			"src": documentProtocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0009/0388.js?"+Math.floor(new Date().getTime()/3600000)
		});

		crazyLen = crazy.length;
		
		for(i=0; i<crazyLen; i+=1){
            (function(){
                var crazyUrl = crazy[i].url,
                    crazySrc = crazy[i].src,
                    compareUrl,
                    noQS,
                    noPage,
                    inHref;
                if(typeof crazyUrl !== "undefined" && typeof crazySrc !== "undefined") {      
                    crazyUrl = crazyUrl.toLowerCase();
                    pageHref = pageHref.toLowerCase();
                    compareUrl = crazyUrl === pageHref;
                    noQS = crazyUrl.indexOf("?") === -1;
                    noPage = pageHref.indexOf(".aspx") === -1;
                    inHref = pageHref.indexOf(crazyUrl) > -1;
                
                   if(compareUrl === true || (noPage && noQS && inHref)){
                        (function(){
					        var a=document.createElement("script");
					        var b=document.getElementsByTagName('script')[0];
					        a.src=crazySrc;
					        a.async=true;
					        a.type="text/javascript";
					        b.parentNode.insertBefore(a,b);
				        })();
			        }
                }
            })();
		}
    });
})();
