/*************************************
    Detect Bowser
**************************************/
   
var g_browserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++)    {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return
parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
        {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
        },
        {     string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {        // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {         // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS : [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
               string: navigator.userAgent,
               subString: "iPhone",
               identity: "iPhone/iPod"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};
// init detection
g_browserDetect.init();

// alias to jQuery library, function noConflict release control of the $ variable 
var $j = jQuery.noConflict();

/**************************
    GLOBAL FUNCTIONALITY
**************************/

// global settings
function setupGlobal()
{
    // blur focus then user click on <a> element
    $j("a").focus(function(){$j(this).blur();});
    // the same for object with id searchBoxBtn
    $j("#searchBoxBtn").focus(function(){$j(this).blur();}); 
}

/***************************************
    ASYNCHRONOUS IMAGE LOADING CODE
****************************************/

// For all object with class "asyncImaLoad" (loader), function read
// image path from title attribute, create new Image object,
// next assign image from path to Image object and 
// append it to loader 
function setupLoadingAsynchronousImages()
{
    // for each object with class "asyncImgLoad"
    $j('.asyncImgLoad').each(
        function()
        {   
            // save handle to loader - caintainer which we gona insert loaded image    
            var loader = $j(this);
            // get image path from loader title attribute
            var imagePath = loader.attr('title');
            // create new image object
            var img = new Image();
            // set opacity for image to maximum
            // value 0.0 means completly transparent
            $j(img).css("opacity", "0.0")
            // next we set function wich gona be caled then
            // image load is finished  
                .load(
                    function() 
                    {
                        // insert loaded image to loader object 
                        // and remove unnecessary title attribute
                        loader.append(this).removeAttr('title');
                        // for inserted image we set margin to zero
                        // opacity to max and fire up 500ms opacity animation 
                        $j(this)
                            .css("margin", "0px")
                            .css("opacity", "0.0")
                            .animate({opacity: 1.0}, 500,
                                function()
                                {
                                    // after animation we remove loader background image 
                                    loader.css("background-image", "none");
                                }
                            );
                    }
                // set new value for attribute src - this means: load image from imagePath    
                ).attr('src', imagePath);                        
        }
    );
} // end of function setupLoadingAsynchronousImages
 

/***************************************
   SETUP FADE MOVER SLIDER (FMS)
****************************************/
// array that keeps handles to slides
var g_slideHandleFMS = new Array();
// number of slides
var g_slideCountFMS = 0;
// true if slider have only one slide, 
// then we turn off animation
var g_animationAllowedFMS = true;
// actual displayed slide
var g_actualSlideFMS = 0;
// slider width and height
var FMS_WIDTH = 960;
var FMS_HEIGHT = 275;
// time interval betwen slide show
var FMS_INTERVAL = 3100;
// animation type
var FMS_MIX = 100; // randow slide move and fade
var FMS_FADE = 200; // only fade effect
var FMS_MOVE = 300; // only move effect
var g_animationTypeFMS = FMS_FADE;
var g_autoplayHandleFMS = null;
var g_pauseAutoPlayFMS = false;
var g_timerHandleHideDesc = null;
var g_handleDescPanel = null;

// function switch slides then animation is allowed
function faderMoverSliderAutoplay()
{
    if(g_pauseAutoPlayFMS)
    {
        g_autoplayHandleFMS = setTimeout(faderMoverSliderAutoplay, FMS_INTERVAL);
        return;
    }
    
    // fade betwean two slides
    function fade()
    {
       // get index of next slide
       var nextSlide = g_actualSlideFMS + 1;
       if(nextSlide >= g_slideCountFMS)
       {
           nextSlide = 0;
       }
       $j(g_slideHandleFMS[g_actualSlideFMS]).css("z-index", 10);
       $j(g_slideHandleFMS[nextSlide]).css("z-index", 9).css("left", 0);
                
      $j(g_slideHandleFMS[g_actualSlideFMS]).animate({opacity: 0.0}, 1000,
          // after fade down
          function()
          {
             // hide actual slide
             $j(this).css("left", FMS_WIDTH);
             g_autoplayHandleFMS = setTimeout(faderMoverSliderAutoplay, FMS_INTERVAL);            
          }
      );
      
      $j(g_slideHandleFMS[nextSlide]).animate({opacity: 1.0}, 1000);
      g_actualSlideFMS = nextSlide;        
    } // end of function fade
    
    // move slides on left
    function move()
    {
       // get index of next slide
       var nextSlide = g_actualSlideFMS + 1;
       if(nextSlide >= g_slideCountFMS)
       {
           nextSlide = 0;
       }
       $j(g_slideHandleFMS[g_actualSlideFMS]).css("z-index", 10);
       $j(g_slideHandleFMS[nextSlide]).css("z-index", 9).css("left", FMS_WIDTH).css("opacity", 1.0);
                
      $j(g_slideHandleFMS[g_actualSlideFMS]).animate({left: -FMS_WIDTH}, 1000,
          // after fade down
          function()
          {
             // hide actual slide
             $j(this).css("left", FMS_WIDTH);
             g_autoplayHandleFMS = setTimeout(faderMoverSliderAutoplay, FMS_INTERVAL);            
          }
      );
      
      $j(g_slideHandleFMS[nextSlide]).animate({left: 0}, 1000);
      g_actualSlideFMS = nextSlide;     
    } // end of function move

   // check type of animation 
   if(FMS_FADE == g_animationTypeFMS)
   {
      fade();                               
   } // FMS_FADE 
   if(FMS_MOVE == g_animationTypeFMS)
   {
      move();                               
   } // FMS_MOVE
   if(FMS_MIX == g_animationTypeFMS)
   {
      var value = Math.random();
      
      if(value > 0.5)
      {
          fade();
      } else
      {
          move();
      }                               
   } // FMS_MOVE       

} // end of function faderMoverSliderAutoplay

// function initialize slider
function setupFaderMoverSlider()
{
    $j("#faderMoverSlider").each(
       function()
       {
          // get number of slides
          g_slideCountFMS = $j(this).find(".slide").length;
          // if there is less slides then 2, we turn off the animation
          if(g_slideCountFMS < 2)
          {
             g_animationAllowedFMS = false;
             return;
          }
          
          // collect to array all slides handle, and reverse slide z-index
          // leave first slide on screen, other hide behind right slider border
          for(var i = 0; i < g_slideCountFMS; i++)
          {
             var slideHandle = $j("#faderMoverSlider .slide:eq("+i+")");
             g_slideHandleFMS.push(slideHandle);
             // first on top, then second slide, etc.
             $j(slideHandle).css("z-index", 10 - i);            
             // leave first slide, other hide
             if(i > 0)
             {
                $j(slideHandle).css("left", FMS_WIDTH);
             }
          } // for
          
          
          // bind function to action hover for slides in mover/fader slider
          $j("#faderMoverSlider .slide").hover(
            // mouse hover on
            function()
            {
                  // clear timer which wait to hide slide description block
                  clearTimeout(g_timerHandleHideDesc);
                  // if description block was not hidden we must hide it
                  if(g_handleDescPanel != null)
                  {
                      // hide block in animation
                      $j(g_handleDescPanel).find('.slideFaderDescBack, .slideFaderDesc').stop().animate({bottom:-70, opacity: 0.0}, 600);  
                      // clear handle to block
                      g_handleDescPanel = null;                  
                  }
                  // block autoplaying of mover/fader slider
                  g_pauseAutoPlayFMS = true;
                  // save in global variable new description block handle, but only if exist
                  // and animate this block
                  if($j(this).find('.slideFaderDesc').length > 0)
                  {
                    g_handleDescPanel = this;
                    $j(this).find('.slideFaderDescBack').stop().animate({bottom:0, opacity: 0.8}, 600);
                    $j(this).find('.slideFaderDesc').stop().animate({bottom:0, opacity: 1.0}, 600);                   
                  }
                  
            },
            // mouse hover out
            function()
            {
                  // function definition which hide description block
                  function hideDescPanel()
                  {
                      $j(g_handleDescPanel).find('.slideFaderDescBack, .slideFaderDesc').stop().animate({bottom:-70, opacity: 0.0}, 600);  
                      g_handleDescPanel = null;
                  }
                  // if panel exist and was showed we fire up function to hide it
                  if(null != g_handleDescPanel)
                  {
                     g_timerHandleHideDesc = setTimeout(hideDescPanel, 800);
                  }
                  // allow to autoplay mover/fader slider
                  g_pauseAutoPlayFMS = false;                   
            }
          );
          
          // fire up slide autoplay
          g_autoplayHandleFMS = setTimeout(faderMoverSliderAutoplay, FMS_INTERVAL);      
       }
    );
} // end of function setupFaderMoverSlider


