// JavaScript Document
 $(document).ready(function() {

				$("#twitter").getTwitter({

					userName: "limmerick",

					numTweets: 2,

					loaderText: "Loading tweets...",

					slideIn: false,

					showHeading: false,

					headingText: "Latest Tweets",

					showProfileLink: false, 
				});

			});



$(document).ready(function() {
						   
				   
 var ajax_file =   "apps/for-ajax.php";
 var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
 
 /* SEND EMAIL */
 
 
 
  $("<form id='form_send' name='form_send' method='post' ></form>").insertBefore(".send_table");
  $("#form_send").html("<table>" + $(".send_table").html() + "</table>");
  $(".send_table").remove();
  
 $('.send_button').click(function() { 
								  
								  if ( document.form_send.mail.value == "" )
								  {
										alert("Please fill your email address!");
										return false;
								  }
								  if ( document.form_send.vraag.value == "" )
								  {
										alert("Please fill your message!");
										return false;
								  }

  $.post( ajax_file + "?" +  $("#form_send").serialize()  , {  act : "send_contact" , type_data : "get"}, function (data)
            { 	
				 if ( trim(data) == "1" )
				  {
						alert("Thank you for contacting us!");
						$("#form_send").reset();
				  }
				  else
				  {
						alert("Please fill all fields!");  
				  }
            });
});
  
 
 
	/* CONTROL HANDLE START */
$("*[class*='cmd_']").live("click", function()
  {
    var cmds = $(this).attr("class").split(" ");
    var cmd = null;
    for (var i in cmds)
    {
      if (cmds[i].match(/^cmd_/))
      {
        cmd = cmds[i].replace("cmd_", "");
        break;
      }
    }
	
	clear_all_message();
    switch (cmd)
    {
 
 	  case "set_lang":
	  var id_lang = $(this).attr("rel");
	   $.post( ajax_file, { id_lang : id_lang , act : "set_lang" }, function (data)
            { 	
				 if ( data == 1 )
				  {
						window.location = BASE_URL;
				  }
				  
            });
	  break;
 	
	  case "subcribe_news":
	  email_subscribe = $("#email_subscribe").val();
	  if ( email_subscribe == "" )
	  {
			alert("Please fill your email address!"); $("#email_subscribe").focus();
			return false; 
	  }
	  
	   if ( !email_subscribe.match(emailExp ) ) {
	 
			alert("Please enter a valid email!");
			$("#email_subscribe").focus();
			return false;
		}
	
	
 
	  $.post( ajax_file, { email_subscribe : email_subscribe , act : "process_subscription" }, function (data)
            { 	
				 if ( process == 1 )
				  {
						alert("Congratulations your email has been added into our mailing list!");
						$("#email_subscribe").val("");
				  }
				  else
				  {
						alert("The subscription process failed, please try again later!");  
				  }
            });
	  
	  
       
	  break;
	  
 	  case "register":
	   var email = $("#email").val();  
	  if ( $("#member_type").val() == ""  ) { show_main_msg("Please select membership type!");  $("#member_type").focus(); return false; }
	  if ( $("#first_name").val() == ""  ) { show_main_msg("Please enter your first name!"); $("#first_name").focus(); return false; }
	  if ( !email.match(emailExp ) ) {
			show_main_msg("Please enter valid email!");
			$("#email").focus();
			return false;
		  }
	   if ( email != $("#email2").val()  )
	   {
		   show_main_msg("Email addresses didn't match!");
			$("#email2").focus();
			return false; 
	   }
	   if ( $("#address").val() == ""  ) { show_main_msg("Please enter your sreet, city!"); $("#address").focus(); return false; }
	   if ( $("#province").val() == ""  ) { show_main_msg("Please enter your province!"); $("#province").focus(); return false; }
	   if ( $("#postcode").val() == ""  ) { show_main_msg("Please enter your postcode!"); $("#postcode").focus(); return false; }
	   if ( $("#country").val() == ""  ) { show_main_msg("Please enter your country!"); $("#country").focus(); return false; }
	   if ( $("#captcha_code").val() == ""  ) { show_main_msg("Please enter captcha code!"); $("#captcha_code").focus(); return false; }
	   
	   var interest_num = $(".interest_options:checked").length;
	    if ( interest_num <  1 ) { show_main_msg("Please select at least one interest!");  return false; }
	   
	   $("#register_form").submit();
	  break;
	  
	  
	  case "logout":
	  window.location = "index.php?mpage=log-out";
	  break;
	  
	  case "search" :
	  if ( $("#search_req").val() != "" )
	  	$("#search_data").submit();
	  else
	  	 show_main_msg("Please fill keyword for search!");
	  break;

 	  case "login":
	 
	  	 
		var users = getVal("username");
		var psw = getVal("password");
	 
		if ( users == "" ) { show_main_msg("Please fill username!");  return false; }
		else if ( psw == "" ) { show_main_msg("Please fill password!");  return false; }
		else
		{
			GlobalLoader(true);
			$.post( ajax_file, { username : users, password : psw, act : "login" }, function (data)
            { 
				var str = "";
				str = trim(data);
				 
				if ( str  == trim("success") )
				{
				  
					GlobalLoader(false);
					clear_main_msg();
					show_main_msg2("Login successed!");
					window.location = "index.php?mpage=overview";
				}
				else
				{
				  	show_main_msg("Login Failed!"  );
					GlobalLoader(false);
				}
            });
			
		}
	  break;
	  
	  default:
      break; 
    }// end of switch //
	  GlobalLoader(false);
  }); // end of control handle //


});

function trim(str){
    return str.replace(/^\s+|\s+$/g,'');
}

function goDown()
{
	
	$('#main_content').animate({marginTop: '-=90px'}, 500);
}
function goTop()
{
	var current_top =0;
	$("#main_content").css({marginTop: function(index, value) {
		current_top =  parseFloat(value);
	}});
	if ( current_top < 10 )
	{
		$('#main_content').animate({marginTop: '+=90px'}, 500);
	}
}
function search_submit()
	{
	 
		if ( $("#city").val() == "" &&  $("#zipcode").val() == "" )
		{
			show_main_msg("Please fill city or zipcode!");
			return false;
		} 
		if ( $("#city").val() != "" &&  $("#state").val() == "" && $("#zipcode").val() == "" )
		{
			show_main_msg("Please select state!");
			return false;
		}
		
		if ( $("#zipcode").val() != "" &&  $("#radius").val() == "" )
		{
			show_main_msg("Please select radius!");
			return false;
		}
		
	}

function send_contact()
{
	var ajax_file =   "apps/for-ajax.php";
 	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    var email = $("#email").val();
	if ( $("#name").val() == ""  )
		{
			show_main_msg("Please enter your name!");
			$("#name").focus();
			return false;
		} 
	 if ( !email.match(emailExp ) ) {
	 
			show_main_msg("Please enter your valid email!");
			$("#email").focus();
			return false;
		}
	
	 GlobalLoader(true);  
	  urls = $("#contact_us").serialize(); 
	  
	  $.post( ajax_file + "?" + urls , {  act : "contact_us" , type_data : "get" }, function (data)
				{	 
					if ( data == 1 )	
					{ 
						$("#result_page").html("<div id='message_info' class='message info' ></div>");
						show_main_msg2("Thank You!, your detail has been successfully sent to our staff. <br /> We will contact you as soon as posible.");   
					}
					else
					{
						show_main_msg(data); 
					}
				});
	  GlobalLoader(false);  
	  
	
}


function show_msg(msg)
{
	clear_msg();
	$("#alert_box .invalid_input_notify").html(msg);	
}
function show_main_msg(msg,idx)
{
	if ( idx === undefined ) {
      idx = "";
    }
	$("#message_alert" + idx).html("");
	$("#message_alert" + idx).removeClass("hidden");
	$("#message_alert" + idx).html('<p>'+  msg  + '</p><span title="Dismiss" class="close" onclick="clear_main_msg('+ idx  + ');">'); 
	$("#message_alert" + idx).fadeIn(100)
	//setTimeout("clear_main_msg()",2000);
	
}
function clear_main_msg(idx)
{
 	if ( idx === undefined ) {
      idx = "";
    }
	$("#message_alert" + idx ).fadeOut(700) ;
	
}
function show_main_msg2(msg,idx)
{
	if ( idx === undefined ) {
      idx = "";
    }
	$("#message_info" + idx ).html("");
	$("#message_info" + idx ).removeClass("hidden");
	$("#message_info" + idx ).html('<p>'+  msg  + '</p><span title="Dismiss" class="close" onclick="clear_main_msg2('+ idx  + ');">'); 
	$("#message_info" + idx ).fadeIn(100)
	//setTimeout("clear_main_msg()",2000);
	
}
function clear_main_msg2(idx)
{
 	if ( idx === undefined ) {
      idx = "";
    }
	$("#message_info" + idx ).fadeOut(700) ;
	
}

function clear_all_message()
{
	$(".message").fadeOut(700) ;
}
function confirmRemove(msg)
{
	return confirm(msg);	
}

// auto focus and blur act for textbox
function check_addr(state, caller, txt_def)
{
	if ( state == 1 && $("#" + caller ).val() == txt_def  )
	{
		$("#" + caller ).val("");
	}
	if ( state == 0 &&  $("#" + caller ).val() == "" )
	{
		$("#" + caller ).val(txt_def)
	}
	 
	
} 

function generate_direction()
{
	var from = "";
	if ( $("#address").val() == "Street Address" &&  $("#city").val() == "City, State" &&  $("#zipcode").val() == "ZIP Code" )
	{
		show_main_msg2("Please fill address or zipcode!");
		return false;
	}
	if ( $("#address").val() != "Street Address" )
	{
		from = $("#address").val();
		
	}
	if (  $("#city").val() != "City, State" )
	{
		if ( from != "" )
		from += ", ";
		from += $("#city").val();
	}
	if ( $("#zipcode").val() != "ZIP Code" )
	{
		from = $("#zipcode").val();
		
	}
	$("#direction").css("height","auto"); 
	$("#from").val(from);
	getDirection();
	
}

function print_map()
{
	$("#from2").val($("#from").val());
	$("#to2").val($("#to").val());
	$("#print_map").submit(); 
}

/* Global Loader */
function GlobalLoader(v)
{
  if (v == true)
  {
    // show loader
    $("div#loader").removeClass('hidden').fadeIn(0);
   // $("div#page_content").addClass('hidden');
	$("body").addClass('busy');
  }
  else
  {
    // hide loader
   // $("div#page_content").removeClass('hidden');
    $("div#loader").fadeOut(1000);
	$("body").removeClass('busy');
  }
}

