var current_value;
var overtext;

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

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

window.addEvent('domready', function() {	// Wait to run this until the window is DOM ready
	CreateSliders('li.parent','ul.children');	// Create the left column sliders
});

function CreateSliders(parent_css,child_css) {
	if (!$('container')) {
		//return;
	}
	
	var prevFx = null;															// Holds the prior slider so that we can collapse it upon the next click
	
	$('container').getElements(parent_css).each( function( elem ) {				// Loop through the categories
		var list = elem.getElement(child_css);									// Find the child element that is to be slid
		
		if (list) {																// If it exists...
			list.style.display = 'block';										// Make the slider object display (we hid it in CSS to prevent "blinky" page loads
			
			if (list.className != 'children open') {
				var myFx = new Fx.Slide(list, { duration: 400 }).hide();		// Create a new slider object tied to the list
			}
			else {
				var myFx = new Fx.Slide(list, { duration: 400 });		// Create a new slider object tied to the list
				prevFx = myFx;
			}

			elem.getElement('a').addEvents({													// Add events to the parent element
				'click' : function() {											// Add a "click" event
					if (prevFx) { prevFx.slideOut(); }							// If there's a previous element, slide it away
					myFx.cancel();												// Cancel anything happening already
					myFx.toggle();												// Toggle this element (this allows us to close them all again if we want)
					prevFx = myFx;												// Keep this effect as the previous
				}
			});
		}
	})
}

function DoNothing() {
}

function InitLanguageMenu() {
	// Set up proper links
	var current_url = document.location.href;
	if (current_url == 'http://www.billfishsoftware.com/#' || current_url == 'http://www.billfishsoftware.com/') {
		current_url = 'http://www.billfishsoftware.com/index.html';
	}

	$$('#language_menu ul a').each(function(link) {
		// The new language suffix is the hreflang of the link, but is made blank if the language is "en"
		var new_lang_suffix = link.hreflang != 'en' ? '_' + link.hreflang : '';

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

		// Create the new url, replacing the existing language tag (if present) with the new tag followed by the file suffix
		new_url = current_url.replace(/(_..)?\.(\w*)$/,new_lang_suffix + ".$2");

		// Ship it
		link.href = new_url;
	});
	

	// Go Ahead and Draw It
	var myMenu;
	if ($('language_menu')) {
	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) {
		value = value.replace(/\W/g,'');
		$('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;
};
