var stopped = false;
	$(document).ready(function(){
		var os = navigator.platform;
		var  search_timeout = undefined;
		$("#clan").bind('keyup', function() {
			if(search_timeout != undefined)
			{
				clearTimeout(search_timeout);
			}
			var $element = $(this);
			search_timeout = setTimeout(function()
			{
				search_timeout = undefined;
					//ajax
				if($element.val())
				{
					var d = $element.val();
					$.ajax({type: "POST",url: ajaxClanCheck,data: "u="+d,success: checkedResult });
				}
			}, 300);
		});
		
		if(os.search(/win/i) >= 0)
			$("div#content div.features ul li").css({fontWeight:'normal', color:'#555', marginRight:'16px'});
		
		if($("div.carousel div.frame").html())
		$("div.carousel div.frame").carousel();
		
		$("select.select:not('#location')").selectbox();
		$("select#location").selectbox({imgLoc:"http://fragage.com/images/flags/"});
		
		var init = $("ul#reseller li").index($("ul#reseller li.active"));
		$("div#reseller div:not([class='marker'])").hide();
		$("div#reseller div[rel='"+init+"']").show();
		if(init == 1)
			$("div#reseller div.marker").css({marginLeft:"100px"});
		$("ul#reseller a").click(function(){
			$("ul#reseller li").each(function(){
				$(this).removeClass("active");
			});
			$(this).parents("li").addClass("active");
			var itemindex = $("ul#reseller li a").index(this);
			if(itemindex == 0)
				$("div#reseller div[class='marker']").animate({marginLeft:"-100px"});
			else
				$("div#reseller div[class='marker']").animate({marginLeft:"100px"});
			$("div#reseller div:visible:not([class='marker'])").fadeOut("fast", function(){
				$("div#reseller div[rel='"+itemindex+"']").fadeIn("slow");
			});
			return false;
		});
		
		$("div.featurelist div.item ul li").hide();
		$("div.featurelist div.item ul li:first").show();
		$("div.featurelist div.left a").click(function(){
			$("div.featurelist div.left a").each(function(){
				$(this).removeClass('active');
			});
			$(this).addClass('active');
			var itemIndex = $("div.featurelist div.left a").index(this);
			$("div.featurelist div.item ul li:visible").fadeOut("fast", function(){
				$("div.featurelist div.item ul li:eq("+itemIndex+")").fadeIn("normal");
			});
			return false;
		});
		
		$("form#kb input[type='submit']").hide();
		$("form#kb").click(function() {
			var d = $("div[class='ajaxreturn']").html();
			if(d)
			{
				$("div[class='ajaxreturn']").show();
			}
		});
		$("form#kb input[name='q']").keyup(search);
		$('form#kb').click(function(event){
			event.stopPropagation();
		});
		
		$("a.submit").click(function(){
			$(this).parents("form").submit();
			return false;
		});
		
		$("#calc span.error").hide();
		$("input[name='cema']").keyup(checkEmail);
		$("input").focus(checkEmail)
		
		$("input[name='clan']").alphanumeric({allow:" "});
		
		$("div#tooltip ul li").hide();
		$("div#tooltip ul li:first").show();
		$("input[class='textbox']:not(.notool)").focus(function(){
			var position = $(this).position().top-204;
			var itemindex = $("input").index(this);
			if(!isNaN(parseFloat($(this).attr('rel'))))
				itemindex = itemindex+parseFloat($(this).attr('rel'));
			$("div#tooltip").animate({marginTop: position+"px"});
			$("div#tooltip ul li").hide();
			$("div#tooltip ul li:eq("+itemindex+")").show();
		});
		$("select").click(function(){
			var position = $(this).position().top-206;
			var itemindex = 4;
			$("div#tooltip").animate({marginTop: position+"px"});
			$("div#tooltip ul li").hide();
			$("div#tooltip ul li:eq("+itemindex+")").show();
		});
		$("div.selectwrap").hover(function(){
			var position = $(this).position().top-206;
			var itemindex = 4;
			$("div#tooltip").animate({marginTop: position+"px"});
			$("div#tooltip ul li").hide();
			$("div#tooltip ul li:eq("+itemindex+")").show();
		});
		$("fieldset.config").hover(function(){
			var position = $(this).position().top-208;
			var itemindex = $("fieldset.config").index(this)+7;
			$("div#tooltip").animate({marginTop: position+"px"});
			$("div#tooltip ul li").hide();
			$("div#tooltip ul li:eq("+itemindex+")").show();
		});
		
		$("input[name='clan']").keyup(function(){
			var clan = $(this).val();
			clan = clan.toLowerCase().replace (/\s+/g, '');
			$("#clanname").text(clan.substr(0, 8));
		});
		
		if($("#calc input:radio[name='domain']:checked").val() == 'custom'){
			$("div.domain").each(function(){$(this).show()});
		} else {
			$("div.domain").each(function(){$(this).hide()});
		}
		if($("#calc input:radio[name='domain']:checked").val() == 'external'){
			$("div.external").each(function(){$(this).show()});
		} else {
			$("div.external").each(function(){$(this).hide()});
		}
		$("#calc input:radio[name='domain']").click(function(){
			var domain = $(this).val();
			if(domain == 'custom'){
				$("div.domain").each(function(){$(this).slideDown()});
			} else {
				$("div.domain:visible").each(function(){$(this).slideUp()});	
			}
			if(domain == 'external'){
				$("div.external").each(function(){$(this).slideDown()});
			} else {
				$("div.external:visible").each(function(){$(this).slideUp()});	
			}
		});
		
		var total = new Number(0);
		var currencie = new Number(1);
		reCalculate();
		$("#calc input:radio").click(reCalculate);
			function reCalculate()
			{
				total = 0;
				$("#calc input:radio:checked").each(function(){
					$(this).parent().parent().parent().children("li").each(function(){$(this).removeClass("active");});
					if($(this).is(":checked")){ $(this).parent().parent().addClass("active"); }
					var relation = parseFloat($(this).attr("rel"));
					var itemname = $(this).attr('name');
					if(!isNaN(relation)){
						$("#calc input[name='"+itemname+"']:radio").each(function(){
							var myrelation = parseFloat($(this).attr("rel"));
							var diff = new Number(myrelation-relation);
							diff *= currencie;
							var text = "";
							if(relation <= myrelation)
								text = "+";
							else
								text = "-";
							text = "("+text+currencieText+(Math.abs(diff).toFixed(2))+")";
							$(this).next().parent().find("small").text(text).show();
							if($(this).is(":checked"))
							{
								$(this).next().parent().find("small").hide();
							}
						});
						total += relation;
					} else if(itemname == 'domain'){
						$("#calc input[name='"+itemname+"']:radio").each(function(){
							var itemval = $(this).val();
							if(itemval == 'custom')
								var text = "+";
							else
								var text = "-";
							domainAmount = Math.abs(domainAmount).toFixed(2);
							text = "("+text+currencieText+domainAmount+"/year)";
							$(this).next().parent().find("small").text(text).show();
							if($("#calc input[name='domain']:radio:checked").val() != 'custom')
							{
								$("input[name='domain']:radio").each(function(){
									$(this).next().parent().find("small").hide();
								});
								$("input[value='custom']:radio").next().parent().find("small").show();
							}
							if($(this).is(":checked"))
							{
								$(this).next().parent().find("small").hide();
							}
						});
					}
				});
				total *= currencie;
				$('span#priceoutput').html(currencieText+total.toFixed(2)+"/<small>mo</small>");
			}
	});

function checkEmail(){
	var itemval = $("input[name='cema']").val();
	if(itemval != $("input[name='ema']").val()){
		$("input[name='ema']").parent("fieldset").find("span.error").show("fade");
	} else {
		$("input[name='ema']").parent("fieldset").find("span.error").hide("fade");
	}
}

function search(e)
{
	var d = $("form#kb input[name='q']").attr("value");
	if(d.length > 2)
	{
		stopped = false;
		if(!d)
		{
			$("div[class='ajaxreturn']").html("");
			$("div[class='ajaxreturn']").hide();
			stopped = true;
			return;
		}
		//send the data to the ajax page
		$.ajax({type: "POST",url: ajaxUrlSearch,data: "q="+d,success: searchResult });
		//show the result box
		$(".loading").show();
		//$("div[class='ajaxreturn']").show();
	}
}
function searchResult(result)
{
	if(!stopped)
	{
		$("div[class='ajaxreturn']").show();
		$("div[class='ajaxreturn']").html(result);
	}
	$(".loading").hide();
}

function checkedResult(result)
{
	if(result == "true")
	{
		$("#clan").parent("fieldset").find("span.error").hide("fade");
	}
	else
	{
		$("#clan").parent("fieldset").find("span.error").html(result);
		$("#clan").parent("fieldset").find("span.error").show("fade");
	}
	
}
