jQuery.fn.jSticky = function(settings) {
    // if no paramaters supplied...
		settings = jQuery.extend({
			jSticky: this.offset().top - parseFloat(this.css('margin-top').replace(/auto/, 0))
		}, settings);

    return this.each(function(){
      var msie6 = jQuery.browser == 'msie' && jQuery.browser.version < 7;
      if (!msie6) {
        var self = jQuery(this);
        var top = settings.jSticky;
        
        jQuery(window).scroll(function (event) {
          // what the y position of the scroll is
          var y = jQuery(this).scrollTop();
          // whether that's below the form
          if (y >= top) {
            // if so, ad the fixed class
            self.addClass('fixed');
          } else {
            // otherwise remove it
            self.removeClass('fixed');
          } //end if
        }); //end (window).scroll
      } //end if
    });
};

function elementExists(id){
  return jQuery(".parent-pageid-2").length;
}

jQuery(document).ready(function() {
   var isSelectedWorks = elementExists(".parent-pageid-2");

  /**
  * Fixed image position
  **/
  jQuery(window).load(function () {
    if(elementExists("#carousel")) jQuery("#carousel").jSticky();

    var header = jQuery("#header");
    var subNav = jQuery("#sub-nav");
    var navDuration = 150; //time in milliseconds
  	var navJumpHeight = "0.25em";
  
  /*
    if(isSelectedWorks){
      subNav.css("display","inherit");
    }
  */
    jQuery('#nav').hover(
      function(){
        header.animate({ "top" : "+="+navJumpHeight }, navDuration);
        //if(!isSelectedWorks) subNav.fadeIn(navDuration);
      }, 
      function(){
        header.animate({ "top" : "0px" }, navDuration);
        //if(!isSelectedWorks) subNav.fadeOut(navDuration);
      });
    });

	
  
  jQuery("#carousel ul div").cycle({
    pager:  '.jcarousel-control',
    pagerAnchorBuilder: function(idx, slide) { 
      // return selector string for existing anchor 
      return '.jcarousel-control .gallery-item:eq(' + idx + ')'; 
    },
    timeout: 0,
    cleartype: 1,
    fit: 1,
    sync: 0,
    speed: 200,
    next: "#carousel ul div"
  });
});
