var types = ['DOMMouseScroll', 'mousewheel'];
$.event.special.mousewheel = {
	setup: function() {
		if ( this.addEventListener ) for ( var i=types.length; i; ) this.addEventListener( types[--i], handler, false );
		else this.onmousewheel = handler;
	},
	teardown: function() {
		if ( this.removeEventListener ) for ( var i=types.length; i; ) this.removeEventListener( types[--i], handler, false );
		else this.onmousewheel = null;
	}
};
$.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	},
	unmousewheel: function(fn) {
		return this.unbind("mousewheel", fn);
	}
});
function handler(event) {
	var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
	event = $.event.fix(event || window.event);
	event.type = "mousewheel";
	if ( event.wheelDelta ) delta = event.wheelDelta/120;
	if ( event.detail     ) delta = -event.detail/3;
	args.unshift(event, delta);
	return $.event.handle.apply(this, args);
}
/*--- custom scroll ---*/
jQuery.fn.customScrollV = function(_options) {
var _options = jQuery.extend({
	lineWidth: 11
}, _options);
return this.each(function(){
	var _box = jQuery(this);
	if(_box.is(':visible')){
		if(_box.children('.scroll-content').length == 0){
			var line_w = _options.lineWidth;
			/*--- init part ---*/
			var scrollBar = jQuery('<div class="scroll-bar"><div class="scroll-up"></div><div class="scroll-line"><div class="scroll-slider"></div></div><div class="scroll-down"></div></div>');
			_box.wrapInner('<div class="scroll-content"><div class="scroll-hold"></div></div>').append(scrollBar);
			var scrollContent = _box.children('.scroll-content');
			var scrollSlider = scrollBar.find('.scroll-slider');
			var scrollSliderH = scrollSlider.parent();
			var scrollUp = scrollBar.find('.scroll-up');
			var scrollDown = scrollBar.find('.scroll-down');
			/*--- different variables ---*/
			var box_h = _box.height();
			var slider_h = 0;
			var slider_f = 0;
			var cont_h = scrollContent.height();
			var _f = false;
			var _f1 = false;
			var _f2 = true;
			var _t1, _t2, _s1, _s2;
			/*--- set styles ---*/
			_box.css({
				position: 'relative',
				overflow: 'hidden',
				height: box_h
			});
			scrollContent.css({
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 1,
				height: 'auto'
			});
			scrollBar.css({
				position: 'absolute',
				top: 0,
				right: 0,
				zIndex:2,
				width: line_w,
				height: box_h,
				overflow: 'hidden'
			});
			scrollUp.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			scrollDown.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			slider_h = scrollBar.height();
			if(scrollUp.is(':visible')) slider_h -= scrollUp.height();
			if(scrollDown.is(':visible')) slider_h -= scrollDown.height();
			scrollSliderH.css({
				position: 'relative',
				width: line_w,
				height: slider_h,
				overflow: 'hidden'
			});
			slider_h = 0;
			scrollSlider.css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: line_w,
				height: slider_h,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			box_h = _box.height();
			cont_h = scrollContent.height();
			if(box_h < cont_h){
				_f = true;
				slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
				if(slider_h < 5) slider_h = 5;
				scrollSlider.height(slider_h);
				slider_h = scrollSlider.outerHeight();
				slider_f = (cont_h - box_h)/(scrollSliderH.height() - slider_h);
				_s1 = (scrollSliderH.height() - slider_h)/15;
				_s2 = (scrollSliderH.height() - slider_h)/3;
				scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
			}
			else{
				_f = false;
				scrollBar.hide();
				scrollContent.css({width: _box.width(), top: 0, left:0});
				scrollContent.children('.scroll-hold').css('padding-right', 0);
			}
			var _top = 0;
			/*--- element's events ---*/
			scrollUp.mousedown(function(){
				_top -= _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top -= 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollDown.mousedown(function(){
				_top += _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top += 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollSliderH.click(function(e){
				if(_f2){
					_top = e.pageY - scrollSliderH.offset().top;
					/*
					if(scrollSlider.offset().top + slider_h < e.pageY) _top += _s2;
					else if(scrollSlider.offset().top > e.pageY) _top -= _s2;
					*/
					scrollCont();
				}
				else{
					_f2 = true;
				}
			});
			var t_y = 0;
			scrollSlider.mousedown(function(e){
				t_y = e.pageY - $(this).position().top;
				_f1 = true;
			}).mouseup(function(){
				_f1 = false;
			});
			$('body').mousemove(function(e){
				if(_f1){
					 _f2 = false;
					 _top = e.pageY - t_y;
					 scrollCont();
				}
			}).mouseup(function(){
				_f1 = false;
			});
			document.body.onselectstart = function(){
				if(_f1) return false;
			}
			_box.bind('mousewheel', function(event, delta){
				if(_f){
					_top -=delta*_s1;
					scrollCont();
					if((_top > 0) && (_top+slider_h < scrollSliderH.height())) return false;
				}
			});
			function scrollCont(){
				if(_top < 0) _top = 0;
				else if(_top+slider_h > scrollSliderH.height()) _top = scrollSliderH.height() - slider_h;
				scrollSlider.css('top', _top);
				scrollContent.css('top', -_top*slider_f);
			}
			this.scrollResize = function(){
				box_h = _box.height();
				cont_h = scrollContent.height();
				if(box_h < cont_h){
					_f = true;
					scrollBar.show();
					slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
					if(slider_h < 5) slider_h = 5;
					scrollSlider.height(slider_h);
					slider_h = scrollSlider.outerHeight();
					slider_f = (cont_h - box_h)/(scrollSliderH.height() - slider_h);
					if(cont_h + scrollContent.position().top < box_h) scrollContent.css('top', -(cont_h - box_h));
					_top = - scrollContent.position().top/slider_f;
					scrollSlider.css('top', _top);
					_s1 = (scrollSliderH.height() - slider_h)/15;
					_s2 = (scrollSliderH.height() - slider_h)/3;
					scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
				}
				else{
					_f = false;
					scrollBar.hide();
					scrollContent.css({top: 0, left:0});
					scrollContent.children('.scroll-hold').css('padding-right', 0);
				}
			}
			/*
			setInterval(function(){
				if(_box.is(':visible') && cont_h != scrollContent.height()) _box.get(0).scrollResize();
			}, 200);
			*/
		}
		else{
			this.scrollResize();
		}
	}
});
}
/*--- IE6 hover ---*/
function ieHover(h_list){
	if($.browser.msie && $.browser.version < 7){
		$(h_list).live('mouseover', function(){
			$(this).addClass('hover');
		}).live('mouseout', function(){
			$(this).removeClass('hover');
		});
	}
}
/*--- fade gallery ---*/
function fadeGall(){
	var stay_time = 5000; // in ms
	var change_speed = 700; // in ms
	var _hold = $('#fade-gallery');
	if(_hold.length){
		var _list = _hold.find('ul.rotate > li');
		if(_list.length > 1){
			var _t;
			var _f = true;
			var _btn = $('<ul class="nav"></ul>');
			for(var i = 0; i < _list.length; i++){
				_btn.append('<li><a href="#">'+(i+1)+'</a></li>');
			}
			_hold.append(_btn);
			_btn = _btn.find('a');
			var _a = _list.index(_list.index(_list.filter('.active:eq(0)')));
			if(_a == -1) _a = 0;
			_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
			_btn.removeClass('active').eq(_a).addClass('active');
			_btn.click(function(){
				changeEl(_btn.index(this));
				return false;
			});
			_hold.mouseenter(function(){
				_f = false;
				if(_t) clearTimeout(_t);
			}).mouseleave(function(){
				_f = true;
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			});
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_btn.eq(_a).removeClass('active');
				_btn.eq(_ind).addClass('active');
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration: change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration: change_speed});
				_a = _ind;
			}
			if(_f){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		}
	}
}
/*--- tabs function ---*/
function initTabs(){
    $('div.tabset').each(function(){
        var btn_h = $(this);
        var _btn = $(this).find('a.tab');
        var _a = _btn.index(_btn.filter('.active:eq(0)'));
        if(_a == -1) _a = 0;
        _btn.removeClass('active').eq(_a).addClass('active');
        var t_box = btn_h.siblings('div');
        _btn.each(function(_i){
            this._box = t_box.eq(_i);
            if(_i == _a) this._box.show();
            else this._box.hide();
            this.onclick = function(){
                changeTab(_i);
                return false;
            }
        });
        function changeTab(_ind){
            if(_ind != _a){
                _btn.get(_a)._box.hide();
                _btn.get(_ind)._box.show();
                _btn.eq(_a).removeClass('active');
                _btn.eq(_ind).addClass('active');
                _a = _ind;
            }
        }
    });
}
/*--- gallery function ---*/
function initGall(){
	var _hold = $('#brands-gallery');
	if(_hold.length){
		var list_hold = _hold.find('div.boxg > ul');
		var _list = list_hold.children();
		var slider_hold = _hold.find('div.line');
		var _slider = slider_hold.find('span');
		var btn_prev = _hold.find('a.prev');
		var btn_next = _hold.find('a.next');
		
		var list_w = _list.length * _list.outerWidth();
		var hold_w = list_hold.parent().width();
		var slider_w = _slider.width();
		var slider_hold_w = slider_hold.width();
		
		if(list_w > hold_w){
			var _k = (list_w-hold_w)/(slider_hold_w - slider_w);
			var _l = 0;
			var t_x = 0;
			var _f1 = false;
			var _f2 = false;
			var _f3 = false;
			var _t2, _t3;
			
			_slider.mousedown(function(e){
				t_x = e.pageX - $(this).position().left;
				_f1 = true;
			}).mouseup(function(){
				_f1 = false;
			}).click(function(){
				return false;
			});
			slider_hold.click(function(e){
				_l = e.pageX - slider_hold.offset().left - slider_w/2;
				 gallList();
			});
			btn_prev.mousedown(function(){
				if(_l > 0){
					_f2 = true;
					gallListP();
				}
			}).mouseup(function(){
				_f2 = false;
			}).click(function(){
				return false;
			});
			btn_next.mousedown(function(){
				if(_l+slider_w < slider_hold_w){
					_f3 = true;
					gallListN();
				}
			}).mouseup(function(){
				_f3 = false;
			}).click(function(){
				return false;
			});
			$(document).mousemove(function(e){
				if(_f1){
					 _l = e.pageX - t_x;
					 gallList();
				}
			}).mouseup(function(){
				_f1 = false;
				_f2 = false;
				_f3 = false;
			});
			
			document.body.onselectstart = function(){ if(_f1) return false;}
			slider_hold.css({'-moz-user-select':'none', '-khtml-user-select': 'none', 'user-select':'none'});
		}
		else{
			_hold.find('div.gallery-nav').hide();
		}
		
		function gallListP(){
			if(_f2){
				_l -= 10;
				gallList();
				if(_t2) clearTimeout(_t2);
				_t2 = setTimeout(function(){
					gallListP();
				}, 30);
			}
		}
		function gallListN(){
			if(_f3){
				_l += 10;
				gallList();
				if(_t3) clearTimeout(_t3);
				_t3 = setTimeout(function(){
					gallListN();
				}, 30);
			}
		}
		function gallList(){
			if(_l < 0) _l = 0;
			else if(_l+slider_w > slider_hold_w) _l = slider_hold_w - slider_w;
			_slider.css('left', _l);
			list_hold.css('left', -_l*_k);
		}
		
		
		/*--- ---*/
		var info_hold = $('div.about-container');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0.5).eq(_a).addClass('active').css('opacity', 1);
		_list.each(function(_i){
			var _btn = $(this);
			this._box = _btn.children('div.about-holder');
			info_hold.append(this._box);
			if(_i == _a) this._box.addClass('active').find('div.scrollable').customScrollV();
			else this._box.css({display:'none', opacity: 0});
			
			_btn.mouseenter(function(){
				_btn.animate({opacity: 1}, {queue:false, duration:200});
			}).mouseleave(function(){
				if(_i != _a) _btn.animate({opacity: 0.5}, {queue:false, duration:200});
			}).click(function(){
				changeEl(_i);
				$('.active  a.more').lightBox();
				return false;
			});
		});
		function changeEl(_ind){
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0.5}, {queue:false, duration:200});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:200});
				_list.get(_a)._box.stop().removeClass('active').animate({opacity: 0}, 700, function(){ $(this).hide();});
				_list.get(_ind)._box.stop().show().addClass('active').animate({opacity: 1}, 700);
				_list.get(_ind)._box.find('div.scrollable').customScrollV();
				_a = _ind;
			}
		}
	}
}
/*--- navigation ---*/
function initNav(){
	var _list = $('#link li');
	if(_list.length){
		var t_l = 0;
		_list.eq(0).addClass('none-bg');
		for(var i = 0; i < _list.length; i++){
			if(_list.eq(i).position().left > t_l){
				t_l = _list.eq(i).position().left;
			}
			else{
				_list.eq(i).addClass('none-bg');
				t_l = 0;
			}
		}
	}
}
$(document).ready(function(){
	ieHover('#visit, #contact');
	fadeGall();
	initTabs();
	initNav();
	initGall();
	if($.browser.msie && $.browser.version < 7 && typeof(DD_belatedPNG) == 'object') DD_belatedPNG.fix('img');
	$('.active  a.more,.active  img.more').lightBox();
    $('.brands').click(function(){
        $('.style-gallery').fadeOut('fast');
        $('.brand-gallery').fadeIn('slow');
        $('.styles').removeClass('active');
        $('.brands').addClass('active');
    });
    $('.styles').click(function(){
        $('.brand-gallery').fadeOut('fast');
        $('.style-gallery').fadeIn('slow');
        $('.brands').removeClass('active');
        $('.styles').addClass('active');
    });
});