/*
 * jQuery Backstretch
 * Version 1.2.5
 * http://srobbin.com/jquery-plugins/jquery-backstretch/
 *
 * Add a dynamically-resized background image to the page
 *
 * Copyright (c) 2011 Scott Robbin (srobbin.com)
 * Dual licensed under the MIT and GPL licenses.
*/
(function(a){a.backstretch=function(l,b,j){function m(c){try{h={left:0,top:0},e=f.width(),d=e/k,d>=f.height()?(i=(d-f.height())/2,g.centeredY&&a.extend(h,{top:"-"+i+"px"})):(d=f.height(),e=d*k,i=(e-f.width())/2,g.centeredX&&a.extend(h,{left:"-"+i+"px"})),a("#backstretch, #backstretch img:not(.deleteable)").width(e).height(d).filter("img").css(h)}catch(b){}"function"==typeof c&&c()}var n={centeredX:!0,centeredY:!0,speed:0},c=a("#backstretch"),g=c.data("settings")||n;c.data("settings");var f="onorientationchange"in window?a(document):a(window),k,e,d,i,h;b&&"object"==typeof b&&a.extend(g,b);b&&"function"==typeof b&&(j=b);a(document).ready(function(){if(l){var b;0==c.length?c=a("<div />").attr("id","backstretch").css({left:0,top:0,position:"fixed",overflow:"hidden",zIndex:-999999,margin:0,padding:0,height:"100%",width:"100%"}):c.find("img").addClass("deleteable");b=a("<img />").css({position:"absolute",display:"none",margin:0,padding:0,border:"none",zIndex:-999999}).bind("load",function(b){var d=a(this),e;d.css({width:"auto",height:"auto"});e=this.width||a(b.target).width();b=this.height||a(b.target).height();k=e/b;m(function(){d.fadeIn(g.speed,function(){c.find(".deleteable").remove();"function"==typeof j&&j()})})}).appendTo(c);0==a("body #backstretch").length&&a("body").append(c);c.data("settings",g);b.attr("src",l);a(window).resize(m)}});return this}})(jQuery);

 /*
  * 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 qLimageCounter = 0;

     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(500, 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++) {
             $.ajax({
                 url: qLimages[i],
                 type: 'HEAD',
                 success: function(data) {
                     qLimageCounter++;
                     addImageForPreload(this['url']);
                 }
             });
         }
     }

     var addImageForPreload = function(url) {
         var image = $("<img />").attr("src", url).bind("load", function () {
             completeImageLoading();
         }).appendTo(qLimageContainer);
     }

     var completeImageLoading = function () {
         qLdone++;

         var percentage = (qLdone / qLimageCounter) * 100;
         $(qLbar).stop().animate({
             width: percentage + "%"
         }, 200);

         if (qLoptions.percentage == true) {
             $(qLpercentage).text(Math.ceil(percentage) + "%");
         }

         if (qLdone == qLimageCounter) {
             destroyQueryLoader();
         }
     }

     var destroyQueryLoader = function () {
         $(qLimageContainer).remove();
         qLoptions.onLoadComplete();
     }

     var createOverlayLoader = function () {
         qLoverlay = $("<div id='qLoverlay'></div>").css({
             width: "100%",
             height: "100%",
             backgroundColor: qLoptions.backgroundColor,
             backgroundPosition: "fixed",
             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: "absolute",
             top: "50%"
         }).appendTo(qLoverlay);
         if (qLoptions.percentage == true) {
             qLpercentage = $("<div id='qLpercentage'></div>").text("0%").css({
                 height: "40px",
                 width: "100px",
                 position: "absolute",
                 fontSize: "3em",
                 top: "50%",
                 left: "50%",
                 marginTop: "-" + (59 + qLoptions.barHeight) + "px",
                 textAlign: "center",
                 marginLeft: "-50px",
                 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");
         }

         if (url.indexOf("gradient") == -1) {
             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);
