function gallery(_obj) {
    var _this = this;
    _this.options = {
        hold: 'div.gallery-wrapper',
        duration: 200,
        autoRotation: 3600,
        effect: 'fade',
        listOfSlides: 'ul.main-gallery > li'
    };
    for(key in _obj) _this.options[key] = _obj[key];
    $(_this.options.hold).each(function(){
        var _hold = $(this);
        if($.browser.msie) var _speed = 0;
        else var _speed = _this.options.duration;
        var _timer = _this.options.autoRotation;
        var _el = _hold.find(_this.options.listOfSlides);
        if (_this.options.effect) var _list = _el;
        else var _list = _el.parent();
        var _count = _el.index(_el.filter(':last'));
        var _w = _el.outerWidth(true);
        var _h = _el.outerHeight(true);
        if (_timer) var _t;
        var _active = _el.index(_el.filter('.active:eq(0)'));
        if (_active < 0) _active = 0;
        var _last = _active;
        // Setting "fade" or "slide" effect
        if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
        else var rew = _count;
        if (!_this.options.effect) {
            _list.css({marginLeft: -(_w * _active)});
        }
        else {
            _list.css({
                opacity: 0
            }).removeClass('active').eq(_active).addClass('active').css({
                opacity: 1
            }).css('opacity', 'auto');
        }
        // Function to "fade"
        function fadeElement(){
            if (_last != _active) {
                _list.eq(_last).fadeOut(300,function(){
                        setTimeout(function(){
                            _list.removeClass('active').eq(_active).addClass('active').css({'display':'block'}).animate({opacity:1.0},{
                            	duration : '300',
                            	complete: function(){
                                $(this).css('opacity','1');
                                if (_el.eq(_active).hasClass('slide')) {
                                    _el.parent('ul').css({
                                        'width':'99999px'
                                    });
                                        _el.css({
                                            'opacity':1,
                                            'position':'relative',
                                            'display':'block'
                                        });
                                    _this.options.effect = false;
                                    _list = _el.parent();
                                    _list.css({marginLeft: -(_w * (_active))});
                                    _speed = _this.options.duration;
                                    if (_t) clearTimeout(_t);
                                }
                            }});
                            _last = _active;
                        }, 350);
                });
                
                
            }
        }
        // Function for "slide"
        function scrollEl(){
            $(window).resize(function(){
            if(navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)){
             _w = $(window).width();
            _w = _w +30;
            }
            else{
            _w = $(window).width();
            }    
                if(_w<$('ul.main-gallery > li').find('img').width()){ _w=$('ul.main-gallery > li').find('img').width();}
                $('ul.main-gallery').css({marginLeft:-(_w*_active)+"px"});
            });
            if (_last != _active) {
                _list.animate({marginLeft: -(_w)}, {queue:false, duration: _speed, complete: function(){
                    if(_active==0){
                        _el.parent('ul').css({
                            'width':'100%'
                        });
                        _el.css({
                            'opacity':0,
                            'position':'absolute'
                        });
                        if ($.browser.msie) {
                            _el.eq(_active).css({
                                'opacity':1,
                                'display':'block'
                            });
                        }
                        else{
                            _el.eq(_active).css({
                                'opacity':1,
                                'display':'block'
                            });
                        }
                        _this.options.effect = 'fade';
                        _list = _el;
                        _speed = 0;
                        if (_timer) runTimer();
                        
                    }
                }});
                _last = _active;
            }
        }
        function toPrepare(){
            if ((_active == rew)) _active = -1; // circle -1(auto), 0(false)
            _active++;
            if (!_this.options.effect) scrollEl();
            else fadeElement();
        }
        var the_val = 0;
        $('a.next').click(function(){
        	the_val++;
    		  if(navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)){
             _w = $(window).width();
            _w = _w +30;
            }
            else{
            	 if(_w<$('ul.main-gallery > li').find('img').width()){
            	 	_w=$('ul.main-gallery > li').find('img').width();
            	 	_w = _w-60;
            	 }
            	 else{
            	 	_w = $(window).width();
            	 }
            } 
            	_list.animate({marginLeft: '-='+(_w)}, 800);

        });
        $('a.back-button').click(function(){
           if(navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)){
             _w = $(window).width();
            _w = _w +30;
            }
            else{
            	 if(_w<$('ul.main-gallery > li').find('img').width()){
            	 	_w=$('ul.main-gallery > li').find('img').width();
            	 	_w = _w-50;
            	 }
            	 else{
            	 	_w = $(window).width();
            	 }
            }  
            var prevPos = $('ul.main-gallery').css("marginLeft");
            $('ul.main-gallery').animate({marginLeft: '+='+(_w)}, 800);
        });
        
        $(window).resize(function(){
           if(navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)){
            _w = $(window).width();
            _w = _w +30;
            }
            else{
            _w = $(window).width();
            }  
            if(_w<$('ul.main-gallery > li').find('img').width()) _w=$('ul.main-gallery > li').find('img').width();
            $('ul.main-gallery > li').width(_w);//RESIZE THE LIST! IMPORTANT!
        });
        function runTimer(){
            _t = setInterval(function(){
                toPrepare();
            }, _timer);
        }
        if (_timer) runTimer();
        
        _this.stop = function(){
            if (_t) clearTimeout(_t);
        }
        _this.play = function(){
            if (_t) clearTimeout(_t);
            if (_timer) runTimer();
        }
    });
}

jQuery.fn.preload = function(){
    var img_no = 2;
    for(var i = 0; i<arguments.length; i++)
       {
                //$("ul.main-gallery").append("loading img"+img_no);
               $(this).find("#img"+img_no).attr("src", arguments[i]);
                img_no++;
       }

};

$(window).bind('load',function(){
    var gal1 = new gallery();
    if(navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)){
    	$('ul.main-gallery > li').not('.active').css({'left':'0px', 'top':'18px'});
    	$(".button-now-on").css({left:15});
    	$(".back-button.second").css({left:-37});
    	$(".text-rohrer-plastics").css({left:620});
    	$(".text-rohrer-printing").css({left:635});
    	$(".text-rohrer-services").css({left:605});
    }
    else{
    	$('ul.main-gallery > li').not('.active').css({'left':'0px', 'top':'-1px'});
    }
    
});

$(document).ready(function() {
    initTabs();
    var gal = "ul.main-gallery > li";
    var win_width = $(window).width();
    $(gal).not('.active').css({'left':'9999px'});
    if(win_width < 1100){
    	$(".gallery-holder .next").css({left:55});
    	$(".gallery-holder .back-button").css({left:4});
    	$(".gallery-holder #back-started").css({left:-10});
    	$(".button-get-started").css({left:35});
    	$("ul.main-gallery > li ").find('img').width(win_width);
    	$(".main-gallery .gallery-holder").width(win_width);
    	$(".button-now-on").css({left:35});
    	if($(".gallery-holder > .image").find('.button-now-on')){
    		$(".back-button").not('.no-move, #back-started').css({left:-17});
    	}
    	$(".text-rohrer-plastics").css({left:620});
    	$(".text-rohrer-printing").css({left:665});
    	$(".text-rohrer-services").css({left:625});
    }
    
    if(navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)){
    	$(gal).width(win_width+30);
    }
    else{
    	$(gal).width(win_width);
    }
    $('ul.main-gallery').css({"display":"block"});
    $("a.tech-data").fancybox({
        'transitionIn'  :   'fadeIn',
        'transitionOut' :   'none',
        'speedIn'       :   600, 
        'speedOut'      :   200, 
        'overlayShow'   :   false
    });
    
    $("#items").css({"left":"0px"});
        $("ul.main-gallery").preload(
            "wp-content/themes/Rohrer/images/img-gallery02.jpg",
            "wp-content/themes/Rohrer/images/img-gallery03.jpg",
            "wp-content/themes/Rohrer/images/img-gallery04.jpg",
            "wp-content/themes/Rohrer/images/img-gallery05.jpg",
            "wp-content/themes/Rohrer/images/img-gallery06.jpg",
            "wp-content/themes/Rohrer/images/img-gallery07.jpg",
            "wp-content/themes/Rohrer/images/img-gallery08.jpg"
        );  
        
});


