﻿window.name = "jswidget.com";
function autoPlay(){
   window.setInterval(
         function(){
            if ( arrIPaint.stopped ){
               return;
            }
            if ( arrIPaint.direction === 1 ){
               showNext();
            }else{
               showPrev();
            }
         },8000);
}            
function showNext(){
   if ( arrIPaint.loaded ){
      if ( arrIPaint.index === arrIPaint.length  - 1 ){
         arrIPaint.index = 0;
      }else{
         arrIPaint.index ++;
      }

      $("#showcase_buffer .image")
         .css({
            background:"url(" + arrIPaint[arrIPaint.index].src +") center no-repeat"
         });
      $("#showcase_buffer .info")
         .html(arrIPaint[arrIPaint.index].info);
      
      $("#showcase_current")
         .animate({
            left: 40 - $("#showcase_current").width() + "px"
         },300,function(){
            $(this).css("display","none");
         });

      $("#showcase_buffer")
         .css("display","")
         .animate({
            left:40 + "px"
         },400,function(){
            reset_showcase();
         });
   }
}
function showPrev(){
   if ( arrIPaint.loaded ){
      if ( arrIPaint.index === 0 ){
         arrIPaint.index = arrIPaint.length  - 1;
      }else{
         arrIPaint.index --;
      }

      $("#showcase_buffer .image")                     
         .css({
            background:"url(" + arrIPaint[arrIPaint.index].src +") center no-repeat"
         });
      $("#showcase_buffer .info")
         .html(arrIPaint[arrIPaint.index].info);
      
      $("#showcase_current")
         .animate({
            left: 40 + $("#showcase_current").width() + "px"
         },300,function(){
            $(this).css("display","none");
         });

      $("#showcase_buffer")
         .css({
            "display":"",
            "left": 40 - $("#showcase_current").width() + "px"
         })
         .animate({
            left:40 + "px"
         },400,function(){
            reset_showcase();
         });
   }
}
function reset_showcase(){
   $("#showcase_current")
      .css({
         left:"40px",
         display:""
      });
   $("#showcase_current .image").css({background:"url(" + arrIPaint[arrIPaint.index].src +") center no-repeat"});
   $("#showcase_current .info").html(arrIPaint[arrIPaint.index].info);
   
   $("#showcase_buffer")
      .css({
         left:$("#showcase_current").position().left + $("#showcase_current").width() + "px",
         display:"none"
      });
}
      
function supports_canvas() {
   return !!document.createElement('canvas').getContext;
}

function preload_images(arr,fnBefore,fnLoaded,fnAfter){
   var i,result = [],nTotal = arr.length;
   
   if ( fnBefore ){fnBefore();}
   for ( i = 0; i < arr.length; i ++ ){
      var img = new Image();
      $(img).data("index",i);
      
      img.onload = function(){
         var index = $(this).data("index");
         result.push(this);
         
         if ( fnLoaded ){
            fnLoaded(index,this);
         }
         
         if ( result.length === nTotal ){
            if ( fnAfter ){
               fnAfter(result);
            }
         }
      };
      img.onerror = function(){
         nTotal --;
      };
      
      img.src = arr[i].src;
   }
}

function launchSite(url,newWin){
   if ( newWin ){
      window.open(url,"jswidget","width=" + screen.width + ",height=" + screen.height + ",status=no,toolbar=no,menubar=no,location=no");
   }else{
      location.href = url;
   }
}

