var fancyBoxOptions = { 'zoomSpeedIn': 500, 'zoomSpeedOut': 300, 'overlayShow': true,'overlayOpacity':0.9,  'centerOnScroll':false,  'hideOnContentClick': true, 'imageScale':false, 'zoomOpacity': false};

$(document).ready(function() {
  var fancyBoxNoHideOptions = jQuery.extend({}, fancyBoxOptions, {'hideOnContentClick': false, titleShow:false, scrolling:'no'});
  add_param_to_php_href(".ajaxFancyBoxNoHide", "ajax=1");
  $(".ajaxFancyBoxNoHide").fancybox(fancyBoxNoHideOptions);
  $("a[target='fancybox']").each(function(){ // ,a[target='contact_us'],a[target='what_we_do']
    add_param_to_php_href(this, "ajax=1");
    if($(this).attr('href').indexOf('what_we_do.php')>=0){
      add_param_to_php_href(this, "h1="+escape($(this).attr('title')));
      pipeIndex = $(this).attr('title').indexOf('|');
      if(pipeIndex > 0)
        $(this).attr('title', $(this).attr('title').substring(0, pipeIndex));
    }
    $(this).fancybox(fancyBoxNoHideOptions);
  });
  
  $('a').live('click', function () { // track events (external link click, mailto click, pdf click)
    if(typeof settings_google_analytics_code != 'undefined' && typeof _gat != 'undefined'){
      //_gaq.push(['_trackPageview'], $(this).attr('href'));
      var filetypes_track = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
      var pageTracker = _gat._getTracker(settings_google_analytics_code);
      var href = $(this).attr('href');
      if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
        var extLink = href.replace(/^https?\:\/\//i, '');
        pageTracker._trackEvent('External', 'Click - external link', extLink);
      }else if (href.match(/^mailto\:/i)){
        var mailLink = href.replace(/^mailto\:/i, '');
        pageTracker._trackEvent('Email', 'Click - mailto', mailLink);
      }else if (href.match(filetypes_track)){
        var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
        filePath = href.replace(new RegExp('^https?\:\/\/'+document.domain+'\/', "i"), '/');
        pageTracker._trackEvent('Download', 'Click - ' + extension, href);
      }
    }
  });
});


function add_param_to_php_href(myselector, param_to_add){
  $(myselector).each(function(){
    myhref= $(this).attr('href');
    if(myhref){
      if(myhref.indexOf('.php')>0)
        myhref += ((myhref.indexOf('.php?')>0) ? '&' : '?')+param_to_add;
      $(this).attr('href', myhref);
    }
  });
}

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('#carousel-next').bind('click', function() {
        carousel.startAuto(0);// Disable autoscrolling.
        carousel.next();
        return false;
    });

    jQuery('#carousel-prev').bind('click', function() {
        carousel.startAuto(0);
        carousel.prev();
        return false;
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

};

// Ride the carousel...
jQuery(document).ready(function() {
    jQuery("#home-carousel").jcarousel({
        auto: 5,
        wrap: 'circular',
        scroll: 4,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    jQuery("#internal-carousel").jcarousel({
        vertical: true,
        auto: 2,
        wrap: 'circular',
        animation: 'slow',
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
});

// home banner behaviour
$(document).ready(function() {
  $('#homeBannerItems .bannerLink').live('mouseover', function () {
    $(this).addClass('mouse-is-over');
    $(this).oneTime(10, function () {
      if(($(this).hasClass('mouse-is-over') || $('.bannerMouseover', this).hasClass('mouse-is-over')) && !$('.bannerMouseover', this).hasClass('efect-is-shown')) {
        $('.bannerMouseover', this).fadeIn('slow').addClass('efect-is-shown');
        $('.mouseoverEffectText', this).animate({width:'425px'}, 'slow');
        myObj = $(this).parseClassToObject('customImage');
        $('#'+myObj.id).fadeIn();
      }
    });
  });
  $('.bannerMouseover').live('mouseover', function () {
    $(this).addClass('mouse-is-over');
  });
  $('.bannerMouseover').live('mouseout', function () {
    $(this).removeClass('mouse-is-over');
  });
  $('#homeBannerItems .bannerLink').live('mouseout', function () {
    $(this).removeClass('mouse-is-over');
    $(this).oneTime(100, function () {
      if(!$(this).hasClass('mouse-is-over') && !$('.bannerMouseover', this).hasClass('mouse-is-over')) {
        $('.bannerMouseover', this).fadeOut('slow').removeClass('efect-is-shown');
        $('.mouseoverEffectText', this).animate({width:'0px'}, 'slow');
        myObj = $(this).parseClassToObject('customImage');
        $('#'+myObj.id).fadeOut();
      }
    });
  });

  // let's show the banners from side
  $(document).oneTime(500, function () {
    $('.sliding-add').slidingAdds({event: 'click', displayOpened: true, event_originator: '.clicker'}).find('.close').click(function () {
      disabled_sliders = getCookie('disabled_sliders');
      if(typeof disabled_sliders != 'string') disabled_sliders = '';
      if(preg=/id=([0-9]+)/.exec($(this).attr('href'))) {
        sid=preg[1];
        if(!$.inArray(sid, disabled_sliders.split(',')) != -1) {
          d=new Date();
          setCookie('disabled_sliders', (disabled_sliders.length > 0 ? disabled_sliders+',':'')+sid, new Date(d.getFullYear()+3, d.getMonth(), d.getDate()));
          $(this).parents('.sliding-add:eq(0)').trigger('destroy');
        }
      }
      return false;
    });
  });
});

/* myParseInt(): this will never return non-int type, not even NaN */
function myParseInt(i) {
  o = 0;
  if(typeof i != 'string' || typeof i != 'number')
    o = isNaN(parseInt(i)) ? 0 : parseInt(i);
  return isNaN(o) ? 0 : o;
}

/**
  * slidingAdds(): This will allow you to use ads from side
  *
  * available options:
  *   speed[='slow']: The speed of animations
  *   visible_selector[='div.visible']: selector (or element), which should be allways shown
  *   event[='mouse']: on which event you want to display the ad (mouse / click)
  *   event_originator[=&visible_selector]: Which selector / element should be clicked on (only for click), if empty, visible_selector is used
  *
  * example HTML:
  * <div class="sliding-adds2">
  *   <div class="hidden sliding-add from-bottom">
  *     <div class="visible">
  *       <a href="#" class="clicker buttons"></a>
  *       <a href="#id=1" class="close buttons"></a>
  *       Lorem ipsum dolor sit amet consectur adipiscing elit.
  *     </div>
  *     <div class="sliding-content">
  *       <img src="http://localhost/hrm/files/slider_image.png" />
  *       Proin scelerisque, turpis nec varius feugiat, orci ante consectur augue, sed molestie.
  *       <a href="http://localhost/hrm/executive-recruitment.html" class="link-out">Click here</a>
  *     </div>
  *   </div>
  * </div>
  *
  * expected CSS:
  *  .sliding-add { position: absolute; }
 */

sliding_adds_elms = [];
(function(bb) {$.extend($.fn, {
  slidingAddsGetSizes: function () {
    if(typeof $.browser != 'undefined' && $.browser.msie && $.browser.version == '6.0') {
      return { ww: $(window).width(), wh:$(window).height(), sct: myParseInt($(document).scrollTop()), scl: myParseInt($(document).scrollLeft()) };
    }
    return { ww: $(window).width(), wh:$(window).height(), sct: /*myParseInt($(document).scrollTop())*/0, scl: /*myParseInt($(document).scrollLeft())*/0 };
  },
  slidingAdds: function(opts) {
    // setup the options
    if (typeof $.browser == 'undefined') {
      alert('Error: jQuery.browser is not supported anymore, slidingAdds will fail in IE6');
    }
    if (typeof $.browser != 'undefined' && $.browser.msie && $.browser.version == '6.0') {
      $(this).css('position', 'absolute');
    } else {
      $(this).css('position', 'fixed');
    }
    options = $.extend({}, {speed: 'slow', displayOpened: false, visible_selector: 'div.visible', event: 'mouse'}, opts);
    if(typeof options.event_originator != 'string' && typeof options.event_originator != 'object') { options.event_originator = options.visible_selector; }

    if(typeof $.browser != 'undefined' && $.browser.msie && $.browser.version == '6.0') {
      //we need this only for browsers not supporting position: fixed (IE6)
      $(window).scroll(function (e) {
        $(window).resize();
      });
    }

    // link the resize function to scroll
    $(window).resize(function () {         // calculate new position of the ads, when the window changes
      sliding_adds_elms = $('div.sliding-add');
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      ow = $(document).find('#alldocs').outerWidth(); oh = $(document).find('#alldocs').outerHeight();
      if(scl+ww+1 >= ow) { $(window).scrollLeft(ow - ww); }
      if(sct+wh+1 >= oh) { $(window).scrollTop(oh - wh); }
      sliding_adds_elms.each(function () {
        vis = $(this).find('.visible');
        h=myParseInt($(this).outerHeight());
        w=myParseInt($(this).outerWidth());
        if(!$(this).hasClass('small')) {
          if($(this).hasClass('from-left')){ opts = { left: scl+w, width: w };
          } else if ($(this).hasClass('from-right') ) { opts = { left: scl+ww-w, width: w };
          } else if ($(this).hasClass('from-top') ) { opts = { top: sct+h, height: h };
          } else { opts = { top: sct+wh-h, height: h }; }
        } else {
          if($(this).hasClass('from-left')){ style = { left: scl-w }; opts = { left: scl+vw, width: vw };
          } else if ($(this).hasClass('from-right') ) { style = { left: scl+ww }; opts = { left: scl+ww-vw-w, width: vw };
          } else if ($(this).hasClass('from-top') ) { style = { top: sct }; opts = { top: sct-vh, height: vh };
          } else { style = { top: sct+wh }; opts = { top: sct+wh-vh, height: vh }; }
        }
        $(this).stop(true, true).css(opts);
      });
    });

    // set the event
    if(options.event == 'click') {
      o=$(options.event_originator, this).data('sliding_ad', this);
      if(o.length==0)
        o = $(opts.event_originator);
      if(o.length!=0)
        o.click(function () {
          s=$(this).data('sliding_ad');
          s.trigger(s.hasClass('full') ? 'hide' : 'show');
          return false;
        });
    } else {
      $(this).mouseenter(function () {
        $(this).trigger('show');
      }).mouseleave(function () {
        $(this).trigger('hide');
      })
    }

    // now apply the ad settings
    out = $(this).data('options', options).each(function () {

      // init function
      options = $(this).data('options');
      $(this).css('zIndex', 5);
      style = opts = {};
      $(this).css({overflow: 'hidden', display: ''}).removeClass('hidden');
      vis = $(this).find(options.visible_selector);
      h=0;$(this).children().each(function() { h+= myParseInt($(this).outerHeight()); });
      w=0;$(this).children().each(function() { w+= myParseInt($(this).outerWidth()); });
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      vh=vis.outerHeight();
      vw=vis.outerWidth()
      if($(this).hasClass('from-left')){ style = { left: scl, width: 0 }; opts = { left: scl+vw, width: vw }; }
      else if ($(this).hasClass('from-right') ) { style = { left: scl+ww, width: 0 }; opts = { left: scl+ww-vw, width: vw }; }
      else if ($(this).hasClass('from-top') ) { style = { top: sct, height: 0 }; opts = { top: sct-vh, height: vh }; }
      else { style = { top: sct+wh, height: 0 }; opts = { top: sct+wh-vh, height: vh }; }
      style.display="block";
      $(this).data('offset_top', $(document).scrollTop());
      $(this).css(style).animate(opts, options.speed, function () {
        $(this).addClass('small');
      });
    }).bind('show', function () {

      // show function
      options = $(this).data('options');
      vis = $(this).find(options.visible_selector);
      h=0;$(this).children().each(function() { h+= myParseInt($(this).outerHeight()); });
      w=0;$(this).children().each(function() { w+= myParseInt($(this).outerWidth()); });
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      if($(this).hasClass('from-left')){ opts = { left: scl+w, width: w };
      } else if ($(this).hasClass('from-right') ) { opts = { left: scl+ww-w, width: w };
      } else if ($(this).hasClass('from-top') ) { opts = { top: sct+h, height: h };
      } else { opts = { top: sct+wh-h, height: h }; }
      style.display="block";
      $(this).animate(opts, options.speed, function () {
        $(this).removeClass('small').addClass('full');
      });
    }).bind('hide', function () {

      // hide function
      options = $(this).data('options');
      vis = $(this).find(options.visible_selector);
      h=0;$(this).children().each(function() { h+= myParseInt($(this).outerHeight()); });
      w=0;$(this).children().each(function() { w+= myParseInt($(this).outerWidth()); });
      sizes=$(this).slidingAddsGetSizes(); ww=sizes.ww; wh=sizes.wh; sct=sizes.sct; scl=sizes.scl;
      vh=vis.outerHeight();
      vw=vis.outerWidth();
      if($(this).hasClass('from-left')){ style = { left: scl-w }; opts = { left: scl+vw, width: vw };
      } else if ($(this).hasClass('from-right') ) { style = { left: scl+ww }; opts = { left: scl+ww-vw-w, width: vw };
      } else if ($(this).hasClass('from-top') ) { style = { top: sct }; opts = { top: sct-vh, height: vh };
      } else { style = { top: sct+wh }; opts = { top: sct+wh-vh, height: vh }; }
      $(this).animate(opts, options.speed, function () {
        $(this).removeClass('full').addClass('small');
        $(this).css('height', vh+'px');
      });
    }).bind('destroy', function () {

      // hide the ad completely
      return $(this).fadeOut('slow', function () {
      });
    }).bind('reinit', function () {

      // show the ad again
      return $(this).fadeIn('slow', function () {
      });
    });

    // show whole ad, when requested
    if(options.displayOpened) {
      $(this).stop(true).trigger('show');
    }

    return out;
  }
});
/* -- end of slidingAdds */

/**
  * parseClassToArray(): parses element's class and returns known elements as array
  * @param String prefix: class prefix
  * example:
  *  elements class = 'blah--name--value blah--name2--val'
  *  out = $(element).parseClassToArray('blah')
  *  output: object:{name: 'value', name2: 'val'};
 */
$.extend($.fn, {
  parseClassToObject: function (prefix) {
    out = {};
    cl = $(this).attr('className');
    if(typeof cl != 'undefined' && cl.length > 0) {
      classes = cl.split(' ');
      reg=/^\-\-(.*)$/;
      for(i=0;i<classes.length;i++) {
        cl = $.trim(classes[i]);
        if(cl.indexOf(prefix) == 0){
          cl = cl.substr(prefix.length);
          if((preg = reg.exec(cl)) && preg[1].length > 0) {
            pos = preg[1].indexOf('--');
            if(pos > 0) {
              out[unescape((''+preg[1]).substr(0, pos))] = unescape((''+preg[1]).substr(pos+2));
            } else {
              out[out.length] = unescape(preg[1]);
            }
          }
        }
      }
    }
    return out;
  }
})}) (jQuery);

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++)
  {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0){
      tempstr = unescape(c.substring(nameEQ.length,c.length));
      //* replace all + with ' ' space - because of different interpretation in PHP
      while(tempstr.match(/\+/))
        tempstr = tempstr.replace(/\+/,' ');
      return tempstr;
    }
  }
  return null;
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=-1" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  }
}
