var COOKIE_DOMAIN = ".bitka.net";


function doEmo(el, emo) {
	$(el).value += emo+' ';
	$(el).focus();
}


function checkWhat(field) {
	if (document.mail.ca.checked) {
		checkAll(field);
	}
	else {
		checkNone(field);
	}
}
function checkAll(field) {
	for (i = 1; i < field.length; i++) {
		field[i].checked = true ;
	}
}
function checkNone(field) {
	for (i = 1; i < field.length; i++) {
		field[i].checked = false ;
	}
}


function fixTipWidth(tip) {
	var w = 0;
    if (Browser.Engine.trident) {
        if(! $('debugger')) {
            document.getElement('body').grab(new Element('div', {
                'id': 'debugger',
                'styles': {
                    'visibility': 'hidden',
                    'width': 'auto',
                    'position': 'absolute'
                }
            }));
        }
        var dbgr = $('debugger');
        dbgr.empty();
        var tipClone = null;
        var tipText = tip.getElement('.tip-text');
        var tipTitle = tip.getElement('.tip-title');
        
        if(tipText && tipTitle) {
            tipTextClone = tipText.clone(true, false);
            tipTitleClone = tipTitle.clone(true, false);
            
            dbgr.grab(tipTextClone);
            var ttxw = tipTextClone.getCoordinates().width;
            
            dbgr.empty();
            dbgr.grab(tipTitleClone);
            var tttw = tipTitleClone.getCoordinates().width + 20;
            
            w = Math.max(ttxw, tttw);
        } else if (tipText) {
            tipClone = tipText.clone(true, false);
            dbgr.grab(tipClone);
            
            w = tipClone.getCoordinates().width;
        } else {
            tipClone = tipTitle.clone(true, false);
            dbgr.grab(tipClone);
            
            w = tipClone.getCoordinates().width + 2;
        }
        
        if(parseInt(Browser.Engine.version) > 4) {
            w -= 14;
        }
    } else {
        w = tip.getElement('.tip').getStyle('width');
    }
    
//	 tip.getElement('.tip-top').set('html', '<span></span>');
//	 tip.getElement('.tip-bottom').set('html', '<span></span>');
	 
	 var tipText = tip.getElement('.tip-text');
	 var tipTitle = tip.getElement('.tip-title');
	 if(tipText) {
         tipText.setStyle('width', (w)+'px');
         
     } 
     if (tipTitle) {
         tipTitle.setStyle('width', (w)+'px');
     }
	 tip.getElement('.tip-top').setStyle('width', (w)+'px');
	 tip.getElement('.tip-bottom').setStyle('width', (w)+'px');
}


function trainShow(ms) {
	$('civilian').setStyle('display', 'none');
	$('civilianTab').className = 'tab';
	
	$('attack').setStyle('display', 'none');
	$('attackTab').className = 'tab';
	
	$('defence').setStyle('display', 'none');
	$('defenceTab').className = 'tab';
	
	$(ms).setStyle('display', '');
	$(ms+'Tab').className = 'tab active';
}
              
function forgeShow(ms) {
	$('attack').setStyle('display', 'none');
	$('attackTab').className = 'tab';
	
	$('defence').setStyle('display', 'none');
	$('defenceTab').className = 'tab';
	
	$(ms).setStyle('display', '');
	$(ms+'Tab').className = 'tab active';
}
              
function hideAlertBox() {
	$('alertBox').destroy();
}


function submitBugReport() {
		var req = new Request.HTML({
			method: 'post',  
			url: '/ajax/system.php',
			data: { 
				'bugText' : $('bugReportText').value,
				'bugURL' : $('bugReportURL').value,
				'bugGET' : $('bugReportGET').value,
				'bugPOST' : $('bugReportPOST').value
			},  
			onComplete: function(response) {
				 $('bugFormContainer').destroy();
				 $('bugThankYou').setStyle('display', 'block');
//				 $('bugReportLink').destroy();
				 
//				 setTimeout(function(){$('bugReport').destroy();},1000);
			}
		 }).send();
}

function bugReport() {
//	$('bugReport').setStyle('display', 'block');
//	$('bugReportText').focus();

		SqueezeBox.initialize({
			size: {x: 330, y: 200}
		});
		SqueezeBox.open('bugReport.php'); // handler defaults to "ajax" for urls
		
//		SqueezeBox.open($('bugReport'), {handler: 'adopt'}); // and here we force "adopt" handler

//		SqueezeBox.assign($$('a[rel=boxed]'), {
//			size: {x: 300, y: 400},
//			ajaxOptions: {
//				method: 'get' // we use GET for requesting plain HTML (you can skip it, it is the default value)
//			}
//		});
	
	
	
	
}


function openScreenshot(name,lang) {
	
	SqueezeBox.initialize({
			size: {x: 330, y: 200}
		});
	
	SqueezeBox.open('http://cdn.bitka.net/revolution/images/screenshots/'+name+'_'+lang+'.png');
}

function rm(el) {
	$(el).destroy();
}


createCookie('mc',0);
function onDocClick(e) 
{

	/*
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	*/
	
	var posx = e.page.x;
	var posy = e.page.y;	
	
	var cc = Math.floor(posy*10000 + posx*1.0);
	createCookie('mc',posy*10000 + posx);
}

function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; domain="+COOKIE_DOMAIN+"; path=/";
}


function textCounter(field,cntfield,maxlimit) {
		
	var txtObj = document.getElementById(field);
	var countFld = document.getElementById(cntfield);

	if (txtObj.value.length > maxlimit) {
		txtObj.value = txtObj.value.substring(0, maxlimit);
	} else {
		countFld.innerHTML = maxlimit - txtObj.value.length;
		
	}
}

function reportMulti(id) {
	
	SqueezeBox.initialize({
			size: {x: 380, y: 220}
		});
	
	SqueezeBox.open('multiReport.php?id='+id);
}

function submitMultiReport() {
		
		var req = new Request.HTML({
			method: 'post',  
			url: '/ajax/system.php',
			data: { 
				'nameList':	$('nameList').get('value'),
				'userId':	$('userId').value
			},  
			onComplete: function(response) {
//				 $('bugFormContainer').destroy();
//				 $('bugThankYou').setStyle('display', 'block');
//				 $('bugReportLink').destroy();
				 
//				 setTimeout(function(){$('bugReport').destroy();},1000);
				SqueezeBox.close();
				alert('Благодарим ви за доклада. Ще бъде прегледан от администратор скоро');
			}
		 }).send();
}

var selected = false;
function massCheck(formName, element) {
	
	//$(\'avatarForm\').getElements(\'input[rel=avatarSelect]\').setProperty(\'checked\',\'checked\');
	if (!selected) {
		$(formName).getElements('input[rel='+element+']').setProperty('checked','checked');
		selected = true;
	} else {
		$(formName).getElements('input[rel='+element+']').setProperty('checked','');
		selected = false;
	}
}

function allianceMemberOnline(username, user_id, alliance) {
	
	var notimooManager = new Notimoo({
		locationVType: 'bottom',
  		locationHType: 'right'
	});
	
	notimooManager.show({
	   title: '<a href="?m=clans&board">'+alliance+'</a>',
	   message: 'Вашият съюзник "<a href="?m=profile&id=' + user_id + '" >' + username + 
	             '</a>" току-що дойде на линия.',
	   width: 200, 
	   visibleTime: 5000
	});
}

function banIp(ip, period) {
	
	var req = new Request({
		
		method: 'post',
		url: 'ajax.php',
		data: { 'punishIP' : 'true',
				'ip' : ip,
				'period' : period
		},
		onSuccess: function(response) {
			SqueezeBox.close();
			alert(response);
		}
	}).send();
}

function openPunish(id) {
	
	SqueezeBox.initialize({
		size: {x: 600, y: 400}
	});

	SqueezeBox.open('ajaxPunish.php?user='+id);
}

function massCheckbox(form,el) {
	
	$(form).getElements('input[type=checkbox]').each(function(elm) {
		if (el.checked) {
			$(elm).set('checked','checked');
		} else {
			$(elm).set('checked','');
		}
	});
}