(function(){var e=jQuery,f="jQuery.pause",d=1,b=e.fn.animate,a={};function c(){return new Date().getTime()}e.fn.animate=function(k,h,j,i){var g=e.speed(h,j,i);g.complete=g.old;return this.each(function(){if(!this[f]){this[f]=d++}var l=e.extend({},g);b.apply(e(this),[k,e.extend({},l)]);a[this[f]]={run:true,prop:k,opt:l,start:c(),done:0}})};e.fn.pause=function(){return this.each(function(){if(!this[f]){this[f]=d++}var g=a[this[f]];if(g&&g.run){g.done+=c()-g.start;if(g.done>g.opt.duration){delete a[this[f]]}else{e(this).stop();g.run=false}}})};e.fn.resume=function(){return this.each(function(){if(!this[f]){this[f]=d++}var g=a[this[f]];if(g&&!g.run){g.opt.duration-=g.done;g.done=0;g.run=true;g.start=c();b.apply(e(this),[g.prop,e.extend({},g.opt)])}})}})();

/*
 * QueryLoader v2 - A simple script to create a preloader for images
 *
 * For instructions read the original post:
 * http://www.gayadesign.com/diy/queryloader2-preload-your-images-with-ease/
 *
 * Copyright (c) 2011 - Gaya Kessler
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Version:  2.1
 * Last update: 11-1-2011
 *
 */
(function($) {
    var qLimages = new Array;
    var qLdone = 0;

    var qLimageContainer = "";
    var qLoverlay = "";
    var qLbar = "";
    var qLpercentage = "";

    var qLoptions = {
        onComplete: function () {},
        backgroundColor: "#000",
        barColor: "#fff",
        barHeight: 1,
        percentage: false,
        deepSearch: true,
        completeAnimation: "fade",
        onLoadComplete: function () {
            if (qLoptions.completeAnimation == "grow") {
                $(qLbar).stop().css("width", "100%").animate({
                    top: "0%",
                    height: "100%"
                }, 500, function () {
                    $(qLoverlay).fadeOut(500, function () {
                        $(this).remove();
                        qLoptions.onComplete();
                    })
                });
            } else {
                $(qLoverlay).fadeOut(1200, function () {
                    $(qLoverlay).remove();
                    qLoptions.onComplete();
                });
            }
        }
    }

    var afterEach = function () {
        createPreloadContainer();
        createOverlayLoader();
    }

    var createPreloadContainer = function() {
        qLimageContainer = $("<div></div>").appendTo("body").css({
            display: "none",
            width: 0,
            height: 0,
            overflow: "hidden"
        });
        for (var i = 0; qLimages.length > i; i++) {
            var image = $("<img />").attr("src", qLimages[i]).bind("load", function () {
                completeImageLoading();
            });
        }
    }

    var completeImageLoading = function () {
        qLdone++;

        var percentage = (qLdone / qLimages.length) * 100;
        $(qLbar).stop().animate({
            width: percentage + "%"
        }, 1500);

        if (qLoptions.percentage == true) {
            $(qLpercentage).text(Math.ceil(percentage) + "%");
        }

        if (qLdone == qLimages.length) {
            destroyQueryLoader();
        }
    }

    var destroyQueryLoader = function () {
        $(qLimageContainer).remove();
        qLoptions.onLoadComplete();
    }

    var createOverlayLoader = function () {
        qLoverlay = $("<div id='qLoverlay'></div>").css({
            width: "100%",
            height: "100%",
            backgroundColor: "#fbfbfb",
			backgroundImage: "url(photos/intro/intro-white.jpg)",
            backgroundPosition: "center",
			backgroundRepeat: "no-repeat",
			backgroundSize: "100%",
            position: "fixed",
            zIndex: 666999,
            top: 0,
            left: 0
        }).appendTo("body");
        qLbar = $("<div id='qLbar'></div>").css({
            height: qLoptions.barHeight + "px",
            marginTop: "-" + (qLoptions.barHeight / 2) + "px",
            backgroundColor: qLoptions.barColor,
            width: "0%",
            position: "fixed",
            bottom: "42px"
        }).appendTo(qLoverlay);
        if (qLoptions.percentage == true) {
            qLpercentage = $("<div id='qLpercentage'></div>").text("0%").css({
                height: "40px",
                width: "100px",
                position: "fixed",
                fontSize: "15px",
				fontFamily: "Georgia",
				fontStyle: "italic",
                bottom: "40px",
                left: "73px",
                marginTop: "-" + (59 + qLoptions.barHeight) + "px",
                textAlign: "center",
                marginLeft: "-50px",
                marginTop: "-30px",
            	zIndex: 999999,
                color: qLoptions.barColor
            }).appendTo(qLoverlay);
        }
    }

    var findImageInElement = function (element) {
        var url = "";

        if ($(element).css("background-image") != "none") {
            var url = $(element).css("background-image");
        } else if (typeof($(element).attr("src")) != "undefined" && element.nodeName.toLowerCase() == "img") {
            var url = $(element).attr("src");
        }

        url = url.replace(/url\(\"/g, "");
        url = url.replace(/url\(/g, "");
        url = url.replace(/\"\)/g, "");
        url = url.replace(/\)/g, "");

        var urls = url.split(", ");

        for (var i = 0; i < urls.length; i++) {
            if (urls[i].length > 0) {
                var extra = "";
                if ($.browser.msie && $.browser.version < 9) {
                    extra = "?" + Math.floor(Math.random() * 3000);
                }
                qLimages.push(urls[i] + extra);
            }
        }
    }

    $.fn.queryLoader2 = function(options) {
        if(options) {
            $.extend(qLoptions, options );
        }

        this.each(function() {
            findImageInElement(this);
            if (qLoptions.deepSearch == true) {
                $(this).find("*:not(script)").each(function() {
                    findImageInElement(this);
                });
            }
        });

        afterEach();

        return this;
    };

})(jQuery);


var _count = 0;
var _timer;
var _pauseTimer = 0;
var _innerTimer;
var _play = false;
var _pageVisible = false;

function innerTimeoutAction() {
    
    _innerTimer = setTimeout(function() {
        _pauseTimer = _pauseTimer+1;
            
        innerTimeoutAction();
            
    }, 1000);
}

function timedCount(resume) {
    var stepTime = 10000;
    
    if (resume) {
        innerTimeoutAction();
        stepTime = 10000-(_pauseTimer*1000); 
        
    } else {
        _pauseTimer = 0;
        
    } 
    
    clearTimeout(_innerTimer);
    innerTimeoutAction();
    
    _timer = setTimeout("timedCount()", stepTime);
    
    if (_play == true && _count <= 60) {
        $(".textBox.active").fadeOut("fast", function() {
            $(".textBox.active").removeClass("active"); 
            
            $(".textBox:eq("+_count/10+")").fadeIn("fast", function() {
                $(this).addClass("active");
            });
        });
        
        if (!resume) {
            _count = _count+10;
        }    
        
        if (_count == 30) {
            setTimeout(function() {
                $("#car").addClass("done");    
            }, 3000);    
        }
        
        if (_count == 50) {
            
            setTimeout(function() {
                $("#wrap").animate({bottom: -185}, "slow", function() {
                    $("#playAgain").fadeIn();
                    
                    clearTimeout(_innerTimer);
                });
                
                clearTimeout(_timer);
                $("#car").removeClass("moving");
        
            }, 10000);
            
        }
        
        
    }
    
    _play = true;   
    
}

function playAnimation() {
    $("#layer0").animate({left: -1850}, 60000, 'linear');
    $("#layer1").animate({left: -3127}, 60000, 'linear');
    $("#layer2").animate({left:-9527}, 60000, 'linear');
    $("#layer3").animate({ left:-16573}, 60000, 'linear');
    
    $("#layer4").animate({ left:-14941}, 60000, 'linear');
    
}

/**
 * jQuery.support.cssProperty
 * To verify that a CSS property is supported (or any of its browser-specific implementations)
 *
 * @param string p - css property name
 * [@param] bool rp - optional, if set to true, the css property name will be returned, instead of a boolean support indicator
 *
 * @Author: Axel Jack Fuchs (Cologne, Germany)
 * @Date: 08-29-2010 18:43
 *
 * Example: $.support.cssProperty('boxShadow');
 * Returns: true
 *
 * Example: $.support.cssProperty('boxShadow', true);
 * Returns: 'MozBoxShadow' (On Firefox4 beta4)
 * Returns: 'WebkitBoxShadow' (On Safari 5)
 */
$.support.cssProperty = (function() {
  function cssProperty(p, rp) {
    var b = document.body || document.documentElement,
    s = b.style;

    // No css support detected
    if(typeof s == 'undefined') { return false; }

    // Tests for standard prop
    if(typeof s[p] == 'string') { return rp ? p : true; }

    // Tests for vendor specific prop
    v = ['Moz', 'Webkit', 'Khtml', 'O', 'ms', 'Icab'],
    p = p.charAt(0).toUpperCase() + p.substr(1);
    for(var i=0; i<v.length; i++) {
      if(typeof s[v[i] + p] == 'string') { return rp ? (v[i] + p) : true; }
    }
  }

  return cssProperty;
})();

function initFancybox() {
    $("#galleryWrap a[rel=gallery]").fancybox({
        'titleShow'     : false,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic',
        'easingIn'      : 'easeOutBack',
        'easingOut'     : 'easeInBack',
        'padding'       : 0,
        'overlayOpacity': 1,
        'overlayColor'  : '#000',
        'speedIn'       : 500
        
    });

}

$(document).ready(function() {
    
    var _supportAnimation = $.support.cssProperty('animation');
    
    $("body").queryLoader2({
        barColor: "#438fd7",
        backgroundColor: "#fbfbfb",
        percentage: true,
        barHeight: 1,
        onComplete: function() {
            if (_supportAnimation) {
                $("#layer0, #layer1, #layer2, #layer3, #layer4").addClass("cssanimation");
                
            } else {
                playAnimation();
                
            }
            
            timedCount();
            
        }
    });
    
    $(".processNavigation a").live("click", function(e) {
       e.preventDefault();
        
        if (!$(this).hasClass("active")) {
            var element = $($(this).attr("href"));
            
            $("#pageWrapper").animate({scrollTop: $("#pageWrapper").scrollTop()+element.position().top-169});
        }
        
    });
    
    var windowsHeight = $(window).height();
    $(window).resize(function() {
        windowsHeight = $(window).height();
    });
    
    
    var _pageTitle = document.title;
    $("#menu a").click(function(e) {
        e.preventDefault();
        var element = $(this);
        var href = element.attr("href");
        
        $("#menu a.active").removeClass("active");
        
        element.addClass("active");
        
        if (!_pageVisible) {
            clearTimeout(_innerTimer);
            clearTimeout(_timer);
            
            if (_supportAnimation) {
                $("#layer0, #layer1, #layer2, #layer3, #layer4").css({'-webkit-animation-play-state': 'paused', '-moz-animation-play-state': 'paused', '-ms-animation-play-state': 'paused', '-o-animation-play-state': 'paused'});
                
            } else {
                $("#layer0, #layer1, #layer2, #layer3, #layer4").pause();
                     
            }
            
            $("#wrap").animate({bottom: -windowsHeight}, "fast", function() {
                $("#head").addClass("bg");
    
                $.get(href, function(returnData) {
                    $("#content").html(returnData);
                    $("#content").fadeIn();
                    
                    if ($("#galleryWrap").length) { 
                        initFancybox();
                    }
                    _pageVisible = true;    
                });
    
            });
             
        } else {
            $("#content").fadeOut("fast", function() {
                $(this).html("");
                
                $.get(href, function(returnData) {
                    $("#content").html(returnData);
                    $("#content").fadeIn();
                    
                    if ($("#galleryWrap").length) { 
                        initFancybox();
                    }
                    
                    _pageVisible = true;    
                    
                });
                
            });

            
        }
                    
        
    });
    
    $("#playAgain").live("click", function(e) {
        e.preventDefault();
        
        $("#layer0, #layer1, #layer2, #layer3, #layer4").animate({left:0}, 2000);
        
        $("#playAgain").hide();
        
        setTimeout(function() {
            _count = 0;
            _timer;
            _pauseTimer = 0;
            _innerTimer = 0;
            _play = false;
        
            playAnimation();
            timedCount();
            
            $(".textBox").removeClass("active").removeAttr("style");
            $(".textBox:first").addClass("active").fadeIn();
            
            $("#car").removeClass("done").addClass("moving");
            
            $("#wrap").animate({bottom: 0});
                
        }, 2000);
        
    });
    
    
    $(".toAnimation").live("click", function(e) {
        e.preventDefault();
        
        $("#menu a.active").removeClass("active");
        
        if (_pageVisible) {
            $("#content").fadeOut("fast", function() {
                $("#content").html("");
                
                if (_count != 60) {
                    if (_supportAnimation) {
                        $("#layer0, #layer1, #layer2, #layer3, #layer4").css({'-webkit-animation-play-state': 'running', '-moz-animation-play-state': 'running', '-ms-animation-play-state': 'running', '-o-animation-play-state': 'running'});
                        
                    } else {
                        $("#layer0, #layer1, #layer2, #layer3, #layer4").resume();
                             
                    }
                    $("#wrap").animate({bottom: 0}); 
                    
                    timedCount(_innerTimer);
                    
                } else {
                    $("#head").removeClass("bg");    
                    $("#wrap").animate({bottom: -185});
                    
                }
                
                _pageVisible = false;
                
            });
        }

    });
    
});
	
