$(document).ready(function(){
	
	// Competition form
	$("form.competition").submit(function(){
		$("p.error").remove();
		
		var error = 0;
		for(i=1;i<=10;i++){
			if( $("input[name=answer_"+i+"]").length > 0 && $("input[name=answer_"+i+"]:checked").length != 1 ){
				error = 1;
			}
		}
		if( error == 1 ){
			$(this).find("input[type=submit]").before('<p class="error">Please answer all of the questions.</p>');
			return(false);
		}else if( $("input[name=name]").val() == "" || $("input[name=email]").val() == "" ){
			$(this).find("input[type=submit]").before('<p class="error">Please fill out your name and email address.</p>');
			return(false);
		}
	});
	
	// Latest Blogs slide-y things on home page
	$("a.slide_hide").click(function(){
		$("div.homeblog_container p:visible").slideUp( 500 ).parents("div.homeblog_container:first").find("a.slide_hide").removeClass("expand");
		$(this).addClass("expand").parents("div.homeblog_container:first").find("p").slideDown( 500 );
		return(false);
	});
	
	// "Fetch more comments"
	$("a.fetch_more_comments").live( "click", function(){
		var url = $(this).attr("href");
		$(this).replaceWith('<span class="fetching_more_comments">Fetching more comments...</span>');
		$.ajax({
			url: url,
			success: function( html ){
				$("span.fetching_more_comments").replaceWith( html );
			}
		});
		return(false);
	});
	
	// Your Questions Answered
	
	$("div.question_answered_cat h2 a").click(function(){
		$(this).parent("h2").next("div").next("div").slideToggle();
		return false;
	});
	

	$("a.question_answered_q").click(function(){
		$(this).next().slideToggle();
		return false;
	});

	
	/*
	$("a.question_answered_q").click(function(){
		$(this).next().slideDown();
		return false;
	});
	*/
	
	// Question forms "inform me" links
	
	$("div.question_form input[name=inform_me]").change(function(){
		if( $(this).is(":checked") ){
			$(this).next().next().next().slideDown();
		}else{
			$(this).next().next().next().slideUp();
		}
	});
	
	$("div.sendus input[name=inform_me]").change(function(){
		if( $(this).is(":checked") ){
			$(this).next().next().slideDown();
		}else{
			$(this).next().next().slideUp();
		}
	});
	
	
	// Question form stuff
	
	$(".postboxlink").click(function(event) {
		event.preventDefault();
		$(".sendus").slideToggle();
	});
	
	$(".sendus a").click(function(event) {
		event.preventDefault();
		$(".sendus").slideUp();
	});
	
	$(".questionmore").click(function(event) {
		event.preventDefault();
		$(".question_form").slideToggle();
	});
	
	$(".question_form a").click(function(event) {
		event.preventDefault();
		$(".question_form").slideUp();
	});
	
	if( getQueryVariable("ask") == "true" ){
		$(".questionmore").click();
	}
	
	// Region guide related articles link
	$("a#show_all_region_links").click(function(){
		$(this).parent("p:first").hide();
		$("ul#region_related_articles li").show();
		return false;
	});
	
	// Search box 
	
	$("form#search input.text").focus(function(){
		if( $(this).hasClass("plztype") ){
			$(this).val("").removeClass("plztype");
		}
	});
	
	$("form#search input.text").blur(function(){
		if( $(this).val() == "" ){
			$(this).val("Search Ajarn.com").addClass("plztype");
		}
	});
	
	$("form#search input.text").blur();
	
	// Currency Converter
	$("input#currency_amount").keyup(function(e){
		var currvalue = $(this).val();
		if( currvalue.match( /[^0-9\.]/ ) ){
			var newvalue = currvalue.replace( /[^0-9\.]/g, "" );
			$(this).val( newvalue );
		}
		var amount = $(this).val();
		if( amount == "" ){
			amount = 0;
		}
		$("table#currency_rates td.col_thb span").each(function(){
			var rate = $(this).html();
			$(this).parent("td.col_thb").next("td.col_end").children("span").html( ( amount / rate ).toFixed(2) );
		});
	});
	
});

function page_init(){
	setTimeout( function(){ $("a.slide_hide:first").click() }, 1000 );
}

function getQueryVariable(variable) { 
  var query = window.location.search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++) { 
    var pair = vars[i].split("="); 
    if (pair[0] == variable) { 
      return pair[1]; 
    } 
  } 
  return false;
}

