/* ***************************************************************************************
	VisiScience JavaScript functions
		by Pawel Spychala
**************************************************************************************** */
$(document).ready(function() {
  
  if ($('a[rel=lightbox]').lightBox) $('a[rel=lightbox]').lightBox({fixedNavigation:true});
  
  var timeoutMap = {};
  
  $('.header li').mouseover(function(eventObject) {
      $(this).addClass("hover");
    });
    
  $('.header li').mouseout(function(eventObject) {
      $(this).removeClass("hover");
    });
    
  $('.hov').mouseover(function(eventObject) {
      $(this).addClass("hover");
    });

  $('.hov').mouseout(function(eventObject) {
      $(this).removeClass("hover");
    });    
  
  $('.header li').click(function(eventObject){
    location.href = $('a', eventObject.target).attr("href");
  })
  
  $('#rib-offical-store, #rib-application-demos, #rib-free-slides, #rib-tutorials, #rib-content-overview').mouseover(function(eventObject) {
    clearTimeout(timeoutMap[$(this).attr("id")])
    var pos = $(this).offset();
    var dlg = $('#' + $(this).attr("id") + '-dlg');
    dlg.css({ display: 'block', top: (pos.top - dlg.height() + 3) + "px", left: (pos.left - 10) + "px", opacity: 1})
  });
  
  $('#rib-offical-store, #rib-application-demos, #rib-free-slides, #rib-tutorials, #rib-content-overview').mouseout(function(eventObject) {
    var node = this;
    timeoutMap[$(this).attr("id")]= setTimeout(function() {
          $('#' + $(node).attr("id") + '-dlg').animate({
              opacity: 0.0,
              top: '-=10'
            }, 100, function() {
            });
    }, 75)
  });
  
  $('#slidernext').click(function(eventObject) {
    
      var offset = -192;
      if (!isNaN(parseInt($("#xfade").css("left"), 10))) {
        offset = parseInt($("#xfade").css("left"), 10) - 192;
      }
      if (offset < ($(".xfade_item").length-2)*-192) return;
      $("#xfade").animate({left: offset + "px"}, 200, "swing", function() {});
  });
  
  $('#sliderprev').click(function(eventObject) {
    var offset = +192;
    if (!isNaN(parseInt($("#xfade").css("left"), 10))) {
      offset = parseInt($("#xfade").css("left"), 10) + 192;
    }
    if (offset > 0) return;
    $("#xfade").animate({left: offset + "px"}, 200, "swing", function() {});
  });
  
  setTimeout(switch_divs(1), 3000);
  
});

function switch_divs(frame) 
{
    return (function() {
      $("#testi_" + frame).fadeOut();
      if (frame == $('.testi_item').length) { frame = 1; } else { frame = frame + 1; }
      $("#testi_" + frame).fadeIn();
      setTimeout(switch_divs(frame), 3000);
    });
}

function viewcat_store(categoryId) 
{
    
    if (typeof pageTracker != 'undefined' && pageTracker != null) pageTracker._trackPageview("/ajax-overview-buy");
    
    var url = document.location.href.substring(0, document.location.href.lastIndexOf("/")) + '/#cat=' + categoryId    
    if (is_prod_environment) {
      url = 'https://store.visiscience.com/#cat=' + categoryId;
    }
    window.location.href = url;
}

ScienceSlidesUtil = {}

ScienceSlidesUtil.trackAjax = function(url, prepend) {
  
  if (typeof prepend == 'undefined') {
    prepend = true;
  }
  
  if (location.pathname && typeof(pageTracker) != 'undefined') {
    
    if (location.pathname.endsWith("/") && url.startsWith("/")) {
      url = url.substring(1);
    }
    
    pageTracker._trackPageview((prepend?location.pathname:"") + url);
  }
  
}

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)} 

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

String.prototype.trim = function (str) {
return this.replace(new RegExp("^" + str + "*"), "").replace(new RegExp("" + str + "*$"), "");}
