// Smooth image scroller as adapted from Evan Ehat, http://www.e2interactive.com
var transspeed=500;
var currentpos=0;
var currentthumbpos=0;
var moveamount=78;
var maxthumbblock = 3; //Define how many thumbnails will be block at one time in the thumbbox
var moving = false;
function thumbScroller() {
    
    if ($('thumbs') != null) {
        
        var howmany = $('thumbs').className;
        if (howmany != "") {
            maxthumbblock = howmany;
        }
        
        var thumb_count=$("thumbs").getElementsByTagName("li")
        var thumbnailnum = thumb_count.length;
        
        if (thumbnailnum <= maxthumbblock) {
            $$(".btn_back").setStyle('background', 'none');
            $$(".btn_fwd").setStyle('display', 'none');
        }
        
        function addposition(addwidth){
            if ( !moving ) {
                if(addwidth=="minus"){
                    currentpos-=1;
                }else if(addwidth=="plus"){
                    currentpos+=1;
                }
            }
            return currentpos;
        }
        
        function checkbutton(currentpos) {
            if ( currentpos == 0 ) {
                mm_shl('back','none');
                mm_shl('more','block');
            } else if ( currentpos < thumbnailnum - maxthumbblock ) {
                mm_shl('back','block');
                mm_shl('more','block');
            } else {
                mm_shl('back','block');
                mm_shl('more','none');
            }
        }
        function mm_shl() { //v6.0
            var obj,args=arguments;
            if ((obj=mm_findObj(args[0]))!=null) {
                if (obj.style) {
                    obj=obj.style;
                }
                obj.display=args[1];
            }
        }
        function mm_findObj(n, d) { //v4.01
          var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
            d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
          if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
          for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=mm_findObj(n,d.layers[i].document);
          if(!x && d.getElementById) x=d.getElementById(n); return x;
        }
        function movethumbs(way){
            if(way=='plus'){
                move=(currentthumbpos+moveamount);
                var movethumbs = new Fx.Styles('thumbs', {duration: transspeed, transition: Fx.Transitions.quadOut});
                movethumbs.start({ left: [currentthumbpos, move]});
                currentthumbpos+=moveamount;
            
            }else if(way=='minus'){
                move=(currentthumbpos-moveamount);
                var movethumbs = new Fx.Styles('thumbs', {duration: transspeed, transition: Fx.Transitions.quadOut});
                movethumbs.start({ left: [currentthumbpos, move]});
                currentthumbpos-=moveamount;        
            }
        }
        
        $("back").onclick = function scrollBACK(){
            addposition("minus");
            checkbutton(currentpos);
            movethumbs('plus');
            return false;
        }
        
        $("more").onclick = function scrollFWD(){
            addposition("plus");
            checkbutton(currentpos);
            movethumbs('minus');
            return false;
        }
    }
}
function smoothToggle() {
    if ($('expand1') != null) {
        var getBtn = $$('.toggle');
        getBtn.each(function(btn) {    
            var ctlr1 = $ES('img',btn);
            var ctlr2 = ctlr1[0].id;
            btn.fadeFx = new Fx.Style(ctlr2, 'opacity', {duration:220});
            btn.fx = new Fx.Slide(btn.id + "_content");
            btn.state = "open";
            btn.addEvent('click', function(e){
                e = new Event(e);
                this.fx.toggle().chain(function(){
                    if (this.state == "open") {
                        btn.fadeFx.start(1,0);
                        this.state = "close";
                    } else {
                        btn.fadeFx.start(0,1);
                        this.state = "open";
                    }
                }.bind(this));
                e.stop();
            });
        });
    }
}
function tooltips() {
    var Tips1 = new Tips($$('.tooltip'), {
        initialize:function(){
            this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 400, wait: false}).set(0);
        },
        onShow: function(toolTip) {
            this.fx.start(1);
        },
        onHide: function(toolTip) {
            this.fx.start(0);
        }
    });
    // Remove alt tag popups in IE
    var getTips = $$('.tooltip');
    getTips.each(function(tip) {
        tip.alt = "";
    });
}

//Toogle active class + ajax
function ajaxToggle() {
    if ($$('.gallery1') != null) {
        
        var gall = $$('.gallery1');
        gall.each(function(gallery) {
            
            var whosChanged = null;
            
            var links = $ES('a', gallery);
            links.each(function(lnk) {
            
                $ES('img', lnk)[0].alt="";  //remove popup alt tag from IE browsers
                $ES('img', lnk)[0].className="tooltip2";
                
                var firstTime = "yes";
                lnk.addEvent('click', function(e){
                    e = new Event(e);
                    for (var i = 0; i < links.length ; i++) {
                        links[i].className="";
                    }
                    this.className = "active";
                    
                    var url = this.href;
                    var img_name = this.rel;
                    var alt = this.rev;
                    
                    //ajax                    
                    var fadeFx3 = new Fx.Style('detail_img', 'opacity', {duration:900}, {wait: false}).set(0);
                    
                    if (Cookie.get(img_name) != "loaded") {
                        var image =    $('detail_img').innerHTML = '<center><img class="preloader" src="/_imgs/preloader.gif" alt="Loading" /></center>';
                    }
                    new Asset.image('/_imgs/products/'+ img_name +'.jpg', {id: img_name, title: alt, 
                        onload: function(){
                            $('detail_img').setStyle('opacity', 0);
                            var image2 = $('detail_img').innerHTML = '<img src="/_imgs/products/'+ img_name +'.jpg" alt="'+ alt +'" />';
                            $('detail_link').href = url;
                            fadeFx3.start(1);
                        }
                    });
                    var myCookie = Cookie.set(img_name, 'loaded', {duration: 0});
                    firstTime = "no";
                    
                    e.stop();
                });
            });
        });
    }
}

window.addEvent('domready', init);
function init() {
    if (document.getElementById("mTabs")) {
        myTabs1 = new mootabs('mTabs', {height: '400px', width: '413px', mouseOverClass: 'over'});
    }
    thumbScroller();
    ajaxToggle();
    smoothToggle();
    tooltips();
    var Tips2 = new Tips($$('.tooltip2'));
    new SmoothScroll();
}

/*Setting scroll position for galleries*/
window.addEvent('load', function(){
	var strCook = document.cookie;
	if(strCook.indexOf("!~")!=0){
	  var intS = strCook.indexOf("!~");
	  var intE = strCook.indexOf("~!");
	  var strPos = strCook.substring(intS+2,intE);
	  if (document.getElementById("gallery1")) {
	    document.getElementById("gallery1").scrollTop = strPos;
	  }
	}
})

function SetDivPosition(){
var intY = document.getElementById("gallery1").scrollTop;
document.cookie = "yPos=!~" + intY + "~!";
}

sfHover = function() {     
        var sfEls = document.getElementById("tabs").getElementsByTagName("LI");     
        for (var i=0; i<sfEls.length; i++) {         
            sfEls[i].onmouseover=function() {             
            this.className+=" sfhover";         
            }         
            sfEls[i].onmouseout=function() {             
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");         
            }     
        } 
    } 
    if (window.attachEvent) window.attachEvent("onload", sfHover);
 