/**
 * JavaScrits
 *
 * @author Aurimas Tubis a.k.a Okanakis <aurimas@kryptis.lt>
 */

var pageSizes;

function getContentAjax(uri, container)
{
	var n = 0;
	var containerObj = $('#' + container);
	var href = siteHost + uri + '/ajax.1'
	
	if(!containerObj.length)
	{
		return true;
	}
	
	$(containerObj).ajaxStart(function() {
		if (n++ > 0) return;
	});
	
	$.ajax({
		url: href,
		cache: false,
		success: function(html) {
			if (html) {
				$(containerObj).html(html);
			}
		}
	});
	
	return false;
}
//---------------------------------------------------------------

function showRegisterPopup()
{
    closeOverlay();
    var registerButton = $('a#register_button');
    
    if(registerButton.length)
    {
        registerButton.trigger('click');
    }
    
    return false;
}
//---------------------------------------------------------------

function setOverlay()
{
    if (!(pageSizes instanceof Array))
    {
        pageSizes = getPageSizes();
    }
    
	var w = pageSizes[0];
	var h = pageSizes[1];
    
	var h_body = parseInt($('body').height());
	var h_wrapper = parseInt($('#wrapper').height());
    
    h = Math.max(h, h_body, h_wrapper);
    
	$('#custom_overlay').css({	width: w+'px' });
	$('#custom_overlay').css({	height: h+'px' });
	$('#custom_overlay').show();
}
//---------------------------------------------------------------

function closeOverlay()
{
	$('#custom_overlay').hide();
	$('#custom_overlay_box').hide();
	$('#custom_box_content').html('');
}
//---------------------------------------------------------------

function setOverlayBox(boxId)
{
    if (!(pageSizes instanceof Array))
    {
        pageSizes = getPageSizes();
    }
    
    var box = $(boxId);
    
    if(!box.length)
    {
        var boxSizes = new Array();
        boxSizes[0] = 770;
        boxSizes[1] = 495;
		
		var html = $('<div>').attr('id', 'login_popup');
		html.load(siteHost + boxId + '/ajax.1');
		
		$('#custom_box_content').append(html);
    }
    else
    {
        var boxInner = box.find(':first-child');
        
        var boxSizes = new Array();
        boxSizes[0] = Math.max(770, boxInner.width(), parseInt(boxInner.css('width')));
        boxSizes[1] = Math.max(495, boxInner.height(), parseInt(boxInner.css('height')));
        
		$('#custom_box_content').html(box.html());
    }
    
	var boxPaddingTop = 30;
    var boxPadding = 0;
    
	var top = parseInt((pageSizes[3] - boxSizes[1] - boxPadding) / 2);
	var left = parseInt((pageSizes[0] - boxSizes[0] - boxPadding) / 2);
    var iev = getIEVersion();
	
	if ((iev !== false) && (iev < 7))
	{
        var pageScrolls = getPageScrolls();
        
        top += parseInt(pageScrolls[1]);
        left += parseInt(pageScrolls[0]);
	}

	if ((!top) || (top < boxPaddingTop)) {
		top = boxPaddingTop;
	}
	if ((!left) || (left < 0)) {
		left = 0;
	}
    
	$('#custom_overlay_box').css({
        'width':  boxSizes[0]+'px',
        'height': boxSizes[1]+'px',
        'top':    top+'px',
        'left':   left+'px'
    });
	
    $('#custom_overlay_box').show();
    
}
//---------------------------------------------------------------

function getPageSizes()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}
//---------------------------------------------------------------

function getPageScrolls()
{
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

//---------------------------------------------------------------

function getIEVersion()
{
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	if (isIE)
	{
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		{
			rv = parseFloat( RegExp.$1 );
			return rv;
		}
	}
	
	return false;
}
//---------------------------------------------------------------

/*
	Strips spaces from start and end of string
	Param:
		str string (text)
*/
function trimString (str)
{
	while (str.charAt(0) == ' ')
		str = str.substring(1);
	while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);

	return str;
}
//---------------------------------------------------------------

function toggleBlocks(idA, idB, extra)
{
    //Papildoma salyga skirta login_remind_popup'ui:
    //      uzdaro popup'a, jei einamuoju metu yra atvesratas slaptazodzio priminimo puslapis
    if((idA == 'remind-form-container') && ($('#'+idA).parent().attr('id') != 'login_remind_popup'))
    {
        $('#TB_closeWindowButton').trigger('click');
        
        return false;
    }
    
    //standartinis funkcijos funkcionalumas
    $('#' + idA).show();
    $('#' + idB).hide();
    if (extra)
    {
        $('#' + idA).find(extra).remove();
    }
    
    return false;
}
//---------------------------------------------------------------

