var isIE    = !!(navigator.userAgent.toLowerCase().indexOf('msie') >= 0 && document.all);
var isOpera = !!(window.opera && document.getElementById);
var isMoz   = (!isIE && !isOpera);
var scroll_width = 151;
var elements_number;

var elementWidth = 151;
var elementsTotalWidth;

var nowElement = 0;
var elementCount = 0;

$(document).ready( function() {
	elementCount = $('#nav-bottom > li').length;
	elementsTotalWidth = elementWidth * elementCount;
	
	nowElement = 0;
	
	if(elementCount >  6) {
		_bind();		
	}
		else
	{
		$("#go-left, #go-right").hide();
	}
} );

function _bind() {
	$("#go-left").bind("click", function () {
		_rol();
	} );
	
	$("#go-right").bind("click", function () {
		_ror();
	} );
}


function _unbind() {
	$("#go-left").unbind("click");
	
	$("#go-right").unbind("click");
}

function _ror() {
	_unbind();

	for(var i = 0; i < elementCount; i++) {
		_el = $('#nav-bottom > li').get(i);
		
		if(nowElement == -1) {
			nowElement = elementCount - 1;
		}
		
		if( (i == (nowElement - 1)) || ((nowElement-1) < 0) ) {
			if( (nowElement - 1) < 0)
				_shift = elementCount - 1;
			else
				_shift = i;

			if(i == _shift) {
				$(_el).css('left', - ((_shift) * elementWidth) - elementWidth  + "px");
			}
		}

		var le = parseInt($(_el).css('left'));
	    $(_el).animate({ left: (le + elementWidth)  + "px" }, 500, "linear");
	}
	
	setTimeout(function () {
		_bind();
	}, 500);
	
	nowElement--;
}

function _rol() {
	_unbind();

	for(var i = 0; i < elementCount; i++) {
		_el = $('#nav-bottom > li').get(i);
		
		if(nowElement == elementCount-1) {
			nowElement = 0;
			
			$('#nav-bottom > li').css('left', 0);
		}
		
		if(i == (nowElement - 1))
			$(_el).css('left', (elementsTotalWidth - (elementWidth * (i+1)))  + "px");
		
		var le = parseInt($(_el).css('left'));
		$(_el).animate({ left: (le - elementWidth)  + "px" }, 500);
	}
	
	setTimeout(function () {
		_bind();
	}, 500);
	
	nowElement++;
}

function __scrollLeft()
{

}

function __scrollRight()
{

}

String.prototype.isInteger = function()
{
    if(isNaN(this)) return false;
    return (Math.round(this) == this);
};

function popupOpen(id)
{
    var s = '#' + id;

    if (!(document.getElementById('popup_bg')))
    {
        $('body').append('<div id="popup_bg"></div>');
    }

    $('#popup_bg').css('width', '100%');
    $('#popup_bg').css('height', '100%');
    $('#popup_bg').show();

    $(s).css('visibility', 'hidden');
    $(s).css('display', 'block');
    centerElement($(s).get(0), true);
    $(s).css('display', 'none');
    $(s).css('visibility', 'visible');

    $('#popup_bg').fadeIn('fast', function () { $(s).fadeIn(); });
}

function popupClose(id)
{
   $('#' + id).fadeOut('', function () { $('#popup_bg').fadeOut('fast'); });
}

function centerElement(element, consider_scroll)
{
    if (!element.offsetWidth) return false;

    var left = document.body.offsetWidth / 2 - element.offsetWidth / 2;
    var top  = (isMoz ? window.innerHeight : document.documentElement.clientHeight) / 2 - element.offsetHeight / 2;

    if (consider_scroll != false)
    {
        top += (window.pageYOffset || document.documentElement.scrollTop);
    }

    element.style.position  = 'absolute';
    element.style.left      = left + 'px';
    element.style.top       = top + 'px';
}