$(document).ready(function(){
	InitGallery();	
});
$(window).load(function(){
	if($.cookie('loadPageCounter') == null) loadPageCounter = 1;
	else loadPageCounter = ($.cookie('loadPageCounter')*1)+1;
	$.cookie('loadPageCounter', loadPageCounter);
	if(loadPageCounter == 5) {
		initPopup();
		$.cookie('loadPageCounter', 0);
	}
});
function initPopup(){
	var popup = $('div.sign-lightbox');
	var _fader = $('<div class="overlay"></div>');
	if(popup.length != 0){
		$('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 999,
			background: 'black',
			opacity: 0.7
		});
		
		var btnClose = popup.find('a.btn-no');
	
		var w = $('body').width();
		var h = $(window).height();
		var _offset = $(window).scrollTop();
		
		var ret = _offset+(h/2) - popup.outerHeight(true)/2;
		if (ret < 0) ret = 0;
		var te = $('body').height();
		if ($(window).height() > te) te = $(window).height();
		
		popup.css({
			top: ret,
			left: w/2 - popup.outerWidth(true)/2
		}).hide();
		_fader.css({
			width: w,
			height: te
		}).fadeIn(300, function(){
			popup.fadeIn(300);
		});
		btnClose.click(function(){
			closedPopup();
			return false;
		});
		function closedPopup(){
			popup.fadeOut(300, function(){
				popup.css({left: '-9999px'}).show();
				_fader.hide();
			});
		}
	}
}
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
function InitGallery(){
	var _speed = 1000,
		_duration = 7000;
	var _hold = $('.gallery');
	var _holder = _hold.find('.gallery-content')
	var _slide = _hold.find('ul.slide');
	var _list = _slide.find('>li');
	var _prev = _hold.find('a.prev');
	var _next = _hold.find('a.next');
	
	var _d = _list.eq(0).outerWidth(true);
	var _x=0;
	var _wh = _holder.width();
	var _ws = _list.length*_d;
	var _margin = parseInt(_list.eq(0).css('marginRight'));
	var _vis = Math.ceil(_hold.width()/_d);
	var _wp2 = _vis*_d;
	for	(var i=0; i < _vis; i++){
		_list.eq(i).clone().appendTo(_slide);
	};
	
/*--------CREATING THUMBNAILS----------*/
	var _num = $('.switcher').empty();
	//_hold.append(_num);	
	_list.each(function(i){
		$('<li><a href="#"></a></li>').appendTo(_num);
	});
/*-------------------------------------*/
	var _thumb = _num.find('li');
	var _a = _list.index(_list.filter('.active:eq(0)'));
	if(_a == -1) {
		_a = 0; 
		_thumb.eq(_a).addClass('active');
		_list.eq(_a).addClass('active');
	}
	var _new, _old = _a, _t, _t1;

	Run(_a,_x);
	function Run(_a){
		_t = setTimeout(function(){
			if (_x<_ws){
				_x = _x + _d;
				_a++
				if (_a==_list.length){_a=0}
			}else {
				_x = _d; 
				_a = 1;
			};
			Slide(_a,_x);
		}, _duration);
	};
	function Slide(_new,_x){
		
		_slide.animate({marginLeft: -_x}, {queue:false, duration:_speed, complete:function(){
			_list.eq(_old).removeClass('active');
			_thumb.eq(_old).removeClass('active');
			_list.eq(_new).addClass('active');
			_thumb.eq(_new).addClass('active');
			if (_new == 0){_slide.css({marginLeft:0}); }
			_old=_new;_a=_new;
		}});
		
		if(_t) clearTimeout(_t);
		Run(_new,_x);
	};
	
	_thumb.each(function(j){
		$(this).click(function(){
			_x = (j)*_d;
			_a = j;
			Slide(_a,_x)
			return false;
		});
	});

	_next.click(function(){
		if (_x<_ws){
			_x = _x + _d;
			_a++
			if (_a==_list.length){_a=0}
		}else {
			_x = _d; 
			_a = 1;
		};
		Slide(_a,_x);
		return false;
	});
	
	_prev.click(function(){
		if (_x>0){
			_x = _x - _d;
			_a--
			if (_a==-1){_a=_list.length - 1};
		}else {
			_slide.css({marginLeft:-_ws});
			_x = _ws-_d;
			_a=_list.length - 1;
		};
		Slide(_a,_x);
		return false;
	});
};
