function LangSwitcher() {
	this._isVisible = false;
}

LangSwitcher.prototype.toggle = function(option) {

	if(this._isVisible != true) {
		langswitcher.show();
	} else {
		langswitcher.hide();
	}
    
}

LangSwitcher.prototype.hide = function() {
	document.getElementById('langswitcher_div').style.visibility = 'hidden';
	document.getElementById('langswitcher_div').style.display = 'none';
	this._isVisible = false;
	document.onclick = function () {}
}

LangSwitcher.prototype.show = function() {

	document.getElementById('langswitcher_div').style.display = 'block';
	document.getElementById('langswitcher_div').style.visibility = 'visible';
	document.getElementById('langswitcher_div').style.position = 'absolute';
	document.getElementById('langswitcher_div').focus();

	document.onclick = function () {
	
		if (langswitcher._isVisible == true) {
			langswitcher.hide();
		} else {
			langswitcher._isVisible = true;
		}
	}
	
}

var langswitcher = new LangSwitcher();

Array.prototype.in_array = function(needle) {

	for(var i=0; i < this.length; i++) if(this[ i] === needle) return true;
    return false;

}


function feedbackButton(target) {

	var feedback_button = document.createElement('div');	
	
	var feedback_button_style = 'display:block !important;' +
	'width:50px !important;' +
	'height: 100px !important;' +
	'position: fixed !important;' +
	'top: 30% !important;' +
	'left: -10px;' + 
	'padding: 0 !important;' + 
	'z-index: 20000 !important;' +
	'cursor: pointer !important;' + 
	'background-image:url('+ TC.get('config', 'staticUrlImages') +'/misc/feedback.png) !important;' +
	'background-repeat:no-repeat !important;' +
	'bottom: 0;' +
	'position: absolute;';

	if (feedback_button.style.setAttribute) {
		feedback_button.style.setAttribute('cssText', feedback_button_style);
	} else {
		feedback_button.setAttribute('style', feedback_button_style);
	}
	
	feedback_button.onclick = function() {		
		tb_show('', target + '?keepThis=true&TB_iframe=false&height=270&width=400', false);
	}
	feedback_button.onmouseover = function() {this.style.left = '0px';}
	feedback_button.onmouseout = function() {this.style.left = '-10px';}
	
	document.body.appendChild(feedback_button);

}

function calculateWorth() {

	var domain_name = $('#domain_name').val();
		domain_name = domain_name.toLowerCase();

	var domain_pattern = /[a-z0-9\-\.]+\.[a-z\.]+$/;
	if (domain_pattern.test(domain_name) === false) {
		alert(lang.error_submit_valid_domain);
		return false;
	}
	
	$('#worth_loader').show();
	$('#worth_loader').html('<img src="'+ TW.get('config', 'staticUrlImages') +'/misc/loader_tiny.gif" alt="" /> '+ lang.status_please_wait);
	
	$('#worth_button').hide();
	$('div#bottom').hide();
	
	setTimeout(function() {
		document.location.href = "/site/" + domain_name;
	}, 2000);
	
	return false;
	
}


function updateWorthLimit() {
	alert(lang.error_update_worth_limit);
	return false;
}

function updateWorth(domain_name) {

	    $.blockUI({
	    message: '<img src="'+ TW.get('config', 'staticUrlImages') +'/misc/loader_tiny.gif" alt="" style="vertical-align:middle;line-height:16px;padding-right:5px;" /><span style="line-height:16px;">'+ lang.update_worth_collecting +'</span>',	
	    css: {
	        border: 'none', 
	        padding: '15px',
	        //backgroundColor: '#000', 
	        '-webkit-border-radius': '10px', 
	        '-moz-border-radius': '10px', 
	        opacity: .8, 
	        //color: '#fff' 
	    },
	    onBlock: function() {
	    
			$.ajax({
	   			type: 'GET',
	   			url: TW.get('config', 'baseUrl') + '/data/update.json?domain_name=' + domain_name,
	   			cache: false,
	   			global: false,
	   			dataType: 'json',
	   			success: function(data) {
					
					if (data.status == 'ok') {
						updateWorthGet(domain_name, 'all');
					} else if (data.status == 'idle') {
						$.unblockUI();
						alert(lang.error_update_idle);
					}

				},
				error: function(data) {
					$.unblockUI();
					alert('error update');
				}
			
			});	    	
	    	
	    }
	    });	
	
}

function updateWorthGet(domain_name, type) {
	
	$.ajax({
			type: 'GET',
			url: TW.get('config', 'baseUrl') + '/data/get_all.json?domain_name=' + domain_name,
			cache: false,
			global: false,
			dataType: 'json',
			success: function(data) {
			
			if (data.status == 'idle') {
				
				setTimeout(function() {
					updateWorthGet(domain_name, type);
				}, 2000);
				
			} else if (data.status == 'ok') {
				
				// simple update by redirect ;)
				window.location.href=window.location.href;
				
			} else {
				$.unblockUI();
				alert('error get');
			}

		},
		error: function(data) {
			$.unblockUI();
			alert('error get');
		}
	
	});
	
}

