var _auto;

function fadeGall(){
	var wait_time = 1500; // in ms
	var change_speed = 1200; // in ms
	var _hold = $('div.gallery-01');
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('ul > li');
		var _btn = $('<ul class="paging"></ul>');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		_list.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = _btn.find('a');
		var _a = _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.eq(_a).parent('li').addClass('active');
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		_next.click(function(){
			if (_auto)  clearInterval(_auto);
			_auto = setInterval(function() { $("div.gallery-01 a.link-next").click(); }, 4000);

			if(_a < _list.length-1){
				_k = _a+1;
			}
			else{
				_k = 0;
			}
			changeEl(_k);
			return false;
		});
		
		_prev.click(function(){
			if (_auto)  clearInterval(_auto);
			_auto = setInterval(function() { $("div.gallery-01 a.link-prev").click(); }, 4000);

			if(_a > 0){
				_k = _a-1;
			}
			else{
				_k = _list.length-1;
			}
			changeEl(_k);
			return false;
		});
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_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});
				_btn.eq(_a).parent('li').removeClass('active');
				_btn.eq(_ind).parent('li').addClass('active');
				_a = _ind;
			}
		}
	}
}

jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul');
		var _el = _hold.find('ul > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _active = 0;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
	});
}
jQuery.fn.slideVariable = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700
	},_options);

	return this.each(function(){
		var _timer = 1000; 
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = 100000;
		var _wrap = _hold.find('ul.slider');
		var _el = _hold.find('ul.slider > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _count = _el.index(_el.filter(':last'));
		var _length = 0;
		var _I = 0;
		var _F = true;
		var _t;
		var _w = _el.outerWidth(true);
		for (var _i = _el.length; _i > 0; _i--) {
			if(_length < _wrap.parent().outerWidth()){
				var _W = _el.eq(_i).outerWidth();
			}
			else{
				_W = 0;
				if (_F) {
					_I = _i;
					_F = false;
				}
			}
			_length += _W;
		}
		_I = _el.length - _I - 2;
		var _wrapHolderW = _I ;
		var _active = 0;
		var _margin = 0;
		
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		runTimer();
		clearInterval(_t);
		
		function scrollEl(){
			var _margin = 0;
			for (var _i = 0; _i <_active; _i++) {
				var _thisW = _el.eq(_i).outerWidth();
				_margin += _thisW;
			};
			var _marginLeft = _margin;
			_wrap.eq(0).animate({
				marginLeft: -(_marginLeft) + "px"
			}, {queue:false, duration: _speed});
		}
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
	});
}
$(window).bind("load", function() {
	_auto = setInterval(function() { $("div.gallery-01 a.link-next").click(); }, 4000);
	_auto2 = setInterval(function() { $("div.gallery-02 a.link-next").click(); }, 5000);
	_auto3 = setInterval(function() { $("div.gallery-03 a.link-next").click(); }, 3000);

});	

jQuery.repairLink2Anchor = function() {
    // exists tag BASE
    if ($("base").length) {
        // regular expression for anchor link
        var re1 = /#([\w-]+)/; // anchor parts
        var re2 = /^#([\w-]+)/; // only anchor
        // read current url
        var thisUrl = document.location.href;
        // remove anchor from current url
        thisUrl=thisUrl.replace(re1,"");
        // inicialization
        var href = '';
        var anchor = '';
        // loop all A tags whith attribute href
        $("a[href]").each(function(){
            href = $(this).attr("href");
            // check - href is anchor?
            if (re2.test(href)) {
                anchor = href.match(re2);
                $(this).attr("href", thisUrl + '#' + anchor[1]);
            }
        });
    }
}

$(document).ready(function(){
	$.repairLink2Anchor();
	fadeGall();
	$('div.gallery-02').gallSlide({
		duration: 700
	});
	$('div.gallery-03').slideVariable({
		duration: 700
	});

	//if (typeof ($.colorbox) != undefined)
	if ($.colorbox)
	$("a.youtube").colorbox({iframe:true, innerWidth:720, innerHeight:560, title:'<a href="javascript:$.colorbox.close()">&nbsp; CLOSE &nbsp;</a>'});
	
	$("div.gallery-03 a").click(function() {
          if (_auto3)  clearInterval(_auto3);
	    _auto3 = setInterval(function() { $("div.gallery-03 a.link-next").click(); }, 4000);
          });
	$("div.gallery-02 a").click(function() {
          if (_auto2)  clearInterval(_auto3);
	    _auto2 = setInterval(function() { $("div.gallery-03 a.link-next").click(); }, 4000);
          });
});


