var curItem = null;
var timeOut = null;
var tabId = "tab1";
var timeInterval = 10000;
var curBlock = 'plnStandard';
var blockTimeout = null

window.addEvent('domready', function() {
    var img = new Image();
    img.src = '/images/banners/webalife.lite.jpg';
    img.src = '/images/banners/webalife.standard.jpg';
    img.src = '/images/banners/webalife.vip.jpg';
    img.src = '/images/banners/webalife.vds.jpg';
    
    // rotater
    var timer = function() {
        var items = $$('div.banner');
        for(var i = 0; i < items.length; i++) {
            if (i == 0 && items[items.length - 1].id == curBlock)
                var prev = $(curBlock);
            else
                var prev = items[i-1];
            
            if (prev !== undefined && prev.id == curBlock)  {
                $(curBlock).setStyle('display', 'none');
                $(curBlock).fade('out');
                items[i].setStyle('display', 'block');
                items[i].fade('in');
                curBlock = items[i].id;
                break;                        
            }
        }
    };
    
    blockTimeout = timer.periodical(timeInterval);
    
    $$('div.banner').each(function(item) {
        if (item.id == 'plnStandard')  {
            item.setStyle('display', 'block');
            item.fade('in');                    
        } else {
            item.setStyle('display', 'none');
            item.fade('out');                        
        }
        item.addEvent('mouseenter', function() {
            clearTimeout(blockTimeout);
        });
        item.addEvent('mouseleave', function() {
            blockTimeout = timer.periodical(timeInterval);
        });
    });
    
	$$('div.plan').each(function(item) {
		item.addEvent('mouseenter', function() {
            clearTimeout(blockTimeout);
            var classN = item.className.split(" ")[1].replace("plan", "").trim();
            var el = $('pln' + classN);
            if (curBlock == el.id)
                return;
            $(curBlock).setStyle('display', 'none');
            $(curBlock).fade('out');
            el.setStyle('display', 'block');    
            el.fade('in');
            curBlock = el.id;
		});
        
        item.addEvent('mouseleave', function() {
            blockTimeout = timer.periodical(timeInterval);
        })
	});
	
	$$('div.tab').each(function(item) {
		item.addEvent('click', function() {
			var classN = item.id;
			var selItem = null;
			
			$$('div.tabArea').each(function(sItem) {
				var object = sItem.length > 0 ? sItem[0] : sItem;
				var id = object.id.replace("Area", "");
				if (object.hasClass('visible')) {
					object.removeClass('visible');					
				}
				if (object.id == classN + 'Area') 
					selItem = object;
			});
			
			
			if (selItem != null) {
				if ($('div.tabActive') === null)
					$(tabId).removeClass('tabActive');
				else
					$('div.tabActive').removeClass('tabActive');
				selItem.addClass('visible');	
				item.addClass('tabActive');
				tabId = item.id;
			}

		});
	});
});

/* Проверка формы для доменов */
function checkDomainForm()
{
    var q = document.domain.d.value;
    var p1 = /^\s*[A-Z|a-z|0-9|-]+$/;
    var p2 = /^\s*[A-Z|a-z|0-9|-]+\.[A-Z|a-z]{2,5}$/;
    var p3 = /^\s*[A-Z|a-z|0-9|-]+\.[A-Z|a-z]{2,5}\.[A-Z|a-z]{2,5}$/;
    if ((!p1.exec(q) && !p2.exec(q) && !p3.exec(q)) || (q.indexOf('-') == 0)) {
        $('error').set('html', 'Имя домена указано неверно! Введите имя как показано в примере ниже.');
        $('error').setStyle('display', 'block');
    }   else {
        $('error').setStyle('display', 'none');
        document.domain.submit();
    }
	return false;
}

/* Выбор популярных доменных зон */
function selectPopular()
{
    $$('input.zoneItem').each(function(item) {
        if (item.value == 1 ||
            item.value == 2 ||
            item.value == 3 ||
            item.value == 18 ||
            item.value == 19 ||
            item.value == 20 ||
            item.value == 22 ||
            item.value == 25 ||
            item.value == 39 ||
            item.value == 51)
            item.checked = true; 
        else
            item.checked = false; 
    });
}

/* Выбор всех зон */
function selectAllDomains()
{
    $$('input.zoneItem').each(function(item) {
        item.checked = true; 
    });
}

var totalPrice = 0.0;
var totalDomains = 0;

function loadDomainInfo(domain, zone)
{
    if (Cookie.read('domainsToSignup') != null)
        Cookie.dispose('domainsToSignup');
        
    new Element('div', {
        'id'    : 'zone' + zone,
        'class' : 'zone',
        'html'  : '<div style="width: 30px; float: left;"><img src="/images/loader.gif" alt="" style="margin-right: 10px;"/></div><div>Загрузка данных...</div>'
    }).inject('blockZone');
    
    new Request.JSON({
        url : 'check_domain.php',
        onSuccess : function(o) {
            $('zone' + zone).setStyle('border', '#6B7B95 1px solid'); 
            $('zone' + zone).setStyle('background-color', 'rgb(224, 255, 224)');
  
            if (o.success) {
                if (!o.domain.result) {
                    $('zone' + zone).set('html', '<div style="margin-top: -2px; width: 30px; float: left;"><input type="checkbox" class="zoneSelected" checked="checked" /></div><div><span>' + o.domain.name + '</span> - ' + o.domain.rate.replace('.00', '') + ' руб. в год</div>');
                    totalPrice += parseFloat(o.domain.rate);
                    totalDomains++;
                    if ($('blockItogo').getStyle('display') == 'none') {
                        $('blockItogo').setStyle('display', 'block');
                        $('blockRegIt').setStyle('display', 'block');
                    }
                    $('blockItogo').set('html', 'Итого доменов ' + totalDomains + ' на сумму ' + totalPrice + ' руб.');
                    $('zone' + zone).getElement('input').addEvent('change', function(item) {
                        if (item.target.checked) {
                            totalPrice += parseFloat(o.domain.rate);
                            totalDomains++;
                        } else {
                            totalPrice -= parseFloat(o.domain.rate);
                            totalDomains--;
                        }
                        $('blockItogo').set('html', 'Итого доменов ' + totalDomains + ' на сумму ' + totalPrice + ' руб.');
                    });   
                } else {
                    $('zone' + zone).set('html', '<div style="margin-top: -2px; width: 30px; float: left;"><input type="checkbox" disabled="true" /></div><div><a href="http://' + o.domain.name + '/" target="_blank">' + o.domain.name + '</a> - домен не доступен для регистрации</div>');
                    $('zone' + zone).setStyle('background-color', 'rgb(255, 224, 224)');
                }
            } else {
                $('zone' + zone).set('html', o.msg);
                $('zone' + zone).setStyle('background-color', 'rgb(255, 224, 224)');
            }
        }
    }).post({
        'domain' : domain,
        'zone'   : zone
    });
}

function gotoSignup()
{
    if (totalDomains == 0)
        return;
        
    var domains = [];
    var q= 0;    
    
    $$('input.zoneSelected').each(function(item) {
        if (item.checked) {
            var dos = item.getParent().getParent().getElement('span');
            domains[q] = dos.get('html');
            q++;
        }
    
    });
    
    Cookie.write('domainsToSignup', domains, {path : '/'});
    document.location = '/signup/Webalife.Partner';
}