var current_value;
var overtext;

window.addEvent('domready',function() {
	if (!window.ie6) {
		ReplaceText();
	}

	CreateOverText();
	//InitCustomUrl();
	InitLanguageMenu();
});

function InitLanguageMenu() {
	// Set up proper links
	var current_url = document.location.href;
	$$('#language_menu ul a').each(function(link) {
		var new_lang = link.hreflang;

        // Remove the local page marker
        current_url=current_url.replace('#','');

        var new_url = current_url.replace('/login?lang=es' , '/login');

        // Remove the old lang url

        new_url = new_url.replace('/login?lang=es' , '/login');

        new_url = new_url.replace('/login?lang=en' , '/login');

        new_url = new_url.replace('/more?lang=en' , '/more');

        new_url = new_url.replace('/more?lang=es' , '/more');

        new_url = new_url.replace('/signup?lang=en' , '/signup');

        new_url = new_url.replace('/signup?lang=es' , '/signup');

        new_url = new_url.replace('/subscribe?lang=en' , '/subscribe');

        new_url = new_url.replace('/subscribe?lang=es' , '/subscribe');

        
        // Put the new lang url

        new_url = new_url.replace('/login' , '/login?lang='+new_lang);

        new_url = new_url.replace('/more' , '/more?lang='+new_lang);

        new_url = new_url.replace('/signup' , '/signup?lang='+new_lang);

        new_url = new_url.replace('/subscribe' , '/subscribe?lang='+new_lang);


		link.href = new_url;
        
	});
	

	// Go Ahead and Draw It
	var myMenu = new MenuMatic({
		id: 'language_menu',
		tweakInitial: {
			x: 5,
			y: -10
		}
	});
}

function ReplaceText() {
	$$('.replace').each(function(element) {
		var text = removeAccents(element.innerHTML).substring(0,100);
		text = text.toLowerCase();
		text = text.replace(/ /gi,'_');
		text = text.replace(/\W/gi,'');
		element.empty();
	
		var img = new Element('img', {
			'src': '../text/' + lang + '/' + text + '.png'
		});
		
		element.adopt(img);
	});
}

function CreateOverText() {
	$$('input').each(function(element) {
		overtext = new OverText(element, { wrap: false });
	});
}

function StoreCurrentValue() {
//	current_value = $('custom_url').value;
}

function InitCustomUrl() {
	if ($('signup_custom_url')) {
		current_value = default_value;
		$('signup_custom_url').value = default_value;
	}
}

function ValidateCustomUrl() {
	var validRE = /^www\.billfishbpmcloud\.com\/\w*$/;
	var value = $('signup_custom_url').value;
	if (value.match(validRE)) {
		current_value = $('signup_custom_url').value;
		return true;
	}
	else {
		$('signup_custom_url').value = current_value;
	}
}

function CheckAvailability() {
	var url = 'http://' + $('custom_url').value;
//	var url = 'http://www.dekodesign.com/asdfas'; // AVAILABLE
//	var url = 'http://www.dekodesign.com/'; // NOT AVAILABLE
	
	var myRequest = new Request({
		url: url,
		method: 'get',
		onSuccess: function() {
			alert('Sorry. Your Custom URL is no longer available. Please try another URL.');
		},
		onFailure: function() {
			alert('Your Custom URL is available');
		}
	}).send();
}

function SetCustomUrl() {
        
	var value = $('signup_company_name').value;                
	if (value != null && value !="") {
		value = value.replace(/\W/g,'');
                                value=removeAccents(value);
		$('signup_custom_url').value = 'www.billfishbpmcloud.com/' + value;
		current_value = $('signup_custom_url').value;        
		//$('check_availability').setStyle('opacity',1).addEvent('click',function() { CheckAvailability() });
	}
	else {
		//$('signup_custom_url').value = default_value;
		//$('check_availability').style.opacity = 0.5;
		//$('check_availability').removeEvents('click');
	}                       
}

function removeAccents(strAccents){
    strAccents = strAccents.split('');
    strAccentsOut = new Array();
    strAccentsLen = strAccents.length;
    var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
    var accentsOut = ['A','A','A','A','A','A','a','a','a','a','a','a','O','O','O','O','O','O','O','o','o','o','o','o','o','E','E','E','E','e','e','e','e','e','C','c','D','I','I','I','I','i','i','i','i','U','U','U','U','u','u','u','u','N','n','S','s','Y','y','y','Z','z'];
    for (var y = 0; y < strAccentsLen; y++) {
        if (accents.indexOf(strAccents[y]) != -1) {
            strAccentsOut[y] = accentsOut[accents.indexOf(strAccents[y])];
        }
        else
            strAccentsOut[y] = strAccents[y];
    }
    strAccentsOut = strAccentsOut.join('');
    return strAccentsOut;
};
