jQuery.noConflict();

jQuery.fn.img_h_scroller = function(options) {
  settings = jQuery.extend({
     name: "defaultName",
     size: 5,
     global: true
  }, options);
  var positions=new Array();
  var active=new Array();
  jQuery(window).resize(function(){
       onWindowResize();
  });
  return this.each(function(index){
        var wd=0;
        var pos= new Array();
        jQuery("li", this).each(function(i){
            pos[i]=wd;
            wd+=jQuery(this).width()+1;
        });
        jQuery(this).addClass('img_h_scroller').css({'width':wd, 'position':'absolute', 'top':'0px', 'left':'0px'}).attr('id','sgallery-'+index);
        active['sgallery-'+index]=0;
        positions['sgallery-'+index]=pos;
        jQuery(this).parent().css({'overflow':'hidden', 'position':'relative'}).parent().append('<a href="#" class="prev">&laquo;</a> <a href="#" class="next">&raquo;</a>');

	    jQuery('.next', jQuery(this).parent().parent()).click(function(){
	        nextItem(1, jQuery(this).parent());
            return false;
	    });
	    jQuery('.prev', jQuery(this).parent().parent()).click(function(){
	        nextItem(-1, jQuery(this).parent());
            return false;
	    });
        setButtonVisibility(jQuery(this));
  });
  function setButtonVisibility(obj){
    var id=obj.attr('id');
    var parent=obj.parent().parent();
    if(obj.width()>obj.parent().width()){
        if(active[id]==0){
            jQuery('.prev:visible', parent).hide();
        }else{
            jQuery('.prev:hidden', parent).show();
        }
        if(positions[id][active[id]]+parent.width()>=obj.width()){
            jQuery('.next:visible', parent).hide();
        }else{
            jQuery('.next:hidden', parent).show();
        }
    }else{
        jQuery('.next, .prev', parent).hide();
    }
  }
  function onWindowResize(){
      jQuery('.img_h_scroller').each(function(){
            setButtonVisibility(jQuery(this));
      });
  }
    function nextItem(d, parent){
        if(!d){d=1;}
        var id=jQuery('ul', parent).attr('id');
        var str=jQuery('#'+id).css('left');
        var p=-parseInt(str.substring(0,str.length-2))+d*parent.width();
        if(active[id]+d>=0 && active[id]+d<positions[id].length){
            active[id]+=d;
            if(d>0){
                for (var j=active[id]; j<positions[id].length; j++){
                    if(positions[id][j]>p){
                        active[id]=j-1;
                        break;
                    }
                }
            }else{
                for (var j=active[id]; j>=0; j--){
                    if(positions[id][j]<=p){
                        active[id]=j+1;
                        break;
                    };
                    if(j==0){
                        active[id]=j;
                    };
                }
            }
            setButtonVisibility(jQuery('#'+id));
            jQuery('#'+id).animate({left: -positions[id][active[id]]+"px"}, 300);
        }
    }
};






jQuery(document).ready(function (){
    jQuery('.gallery a').each(function(){jQuery(this).attr('title_c', jQuery(this).attr('title'));});
    jQuery(".gallery").each(function(){
        jQuery("a[rel='"+jQuery(this).attr('id')+"']").colorbox({current:"", opacity:0.8, title:function(){return jQuery(this).attr('title_c');}});
    });
    jQuery(".list .gallery ul").img_h_scroller();  

    jQuery(".sf-menu").addClass('sf-vertical').superfish({ 
        animation: {height:'show'},   // slide-down effect without fade-in 
        delay:     1200               // 1.2 second delay on mouseout 
    });
    
    jQuery('.gallery a').tooltip({
        track: true, 
        showURL: false
    });
});

