if(!TMT){
  var TMT = {};
};
TMT.com = {
	init : function(){
	  // if we are on the index page, lets execute the image handler
	  if($("#home").length>0){
	    $("#prev").text('').append("<img src=\"common/img/nav_prev.png\" alt=\"Previous\" />");
	    $("#next").text('').append("<img src=\"common/img/nav_next.png\" alt=\"Next\" />");
		  $("#images").cycle({ 
          fx:     'fade', 
          speed:   300, 
          timeout: 4200,
          next : '#next',
          prev : '#prev',
          pause : 1
      });
		};
		
		// if we're on the questions page, let's execute question manager
		if($("#faq").length>0){
		  TMT.com.faq();
		};
		
		// if we are on the contact page, let's execute contact manager
		if($("#contact").length>0){
		  // apply hover to calendar
		  $("div.event").hover(function(){
		    $(this).bind('click', function(){
		      var href = $(this).attr('hash');
		      window.location = "events.shtml#"+href;
		    });
		    if(!$(this).hasClass('hover')){
		      $(this).addClass('hover');
		    };
		  }, function(){
		    $(this).unbind('click').removeClass('hover');
		  });
		  // check for previous comment submission and clear form
		  if(window.location.hash.indexOf("c=true")>0){
		    $("form").each(function(){
          var type = this.type;
          var tag = this.tagName.toLowerCase();
          if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = "";
        });
		  };
		  TMT.com.contact.init();
		};
	},
	behaviors : {
	  overlay : function(o){
			if($("#ovrInner .inner").html()!=''){
			  $("#ovrInner .inner").html()=='';
			};
			$("#ovrInner .inner").append("<h2 style=\"margin-top: 0px;\">"+o.title+"</h2><p>"+o.body+"<br /><br /></p>");
			$(".close").text('').click(function(){
			  $("#overlay").hide();
			  clearTimeout($("#overlay").attr('timer'));
			  setTimeout(function(){clearTimeout(this);window.location = "contact.shtml?c=true";},500);
				return false;
			});	
			$("#overlay").attr('timer',setTimeout(function(){
				$("#overlay").hide();
				setTimeout(function(){clearTimeout(this);window.location = "contact.shtml?c=true";},500);
			},6000)).css("position","absolute").fadeIn('fast');
			return false;
	  }
	},
	faq : function(){
	  var questionsOvly = function(){
			$("#overlayq").bind('kill',function(e){
				$("#overlayq").hide();
				clearTimeout($(this).attr('timer'));
				return false;
			});		
			$("#ovrInner").load("questionsanswers.shtml #qanswers",function(){
				$("#ovrInner").wrapInner('<div class="inner"></div>');
				$(".next").removeClass("hidden").click(questionsOvly);
    		$(".prev").removeClass("hidden").click(questionsOvly);
				$(".close").click(function(){
					$("#overlayq").hide();
					return false;
				});
			});
			return function(e){
				var strng = $(this).attr('href').replace(/[^0-9]/g, '');		
				if(strng=="01"){
				  $(".prev").css('cursor','default').click(function(){return false;}).children("img").attr('src','common/img/nav_prevD.png');
				} else {
				  $(".prev").css('cursor','pointer').children("img").attr('src','common/img/nav_prev.png');
				};
				if(strng=="14"){
				  $(".next").css('cursor','default').click(function(){return false;}).children("img").attr('src','common/img/nav_nextD.png');
				} else {
				  $(".next").css('cursor','pointer').children("img").attr('src','common/img/nav_next.png');
				};
				$("#qanswers > div").show();
				$("#qanswers > div").not("#"+strng).hide();		
				$(".close").trigger('kill').text('');						
				$("#overlayq").attr('timer',setTimeout(function(){
					$("#overlayq").hide();
				},60000)).css("position","absolute").fadeIn('fast');
				return false;
			};
		}();
		$("#faq a").click(questionsOvly);
	},
	contact : {
	  init : function(){
	    $("form").bind('submit',function(){
        var valid = TMT.com.contact.validate($(this));
        if(valid){
          TMT.com.contact.submitForm($(this));
        } else {
          $("#frmError").show();
        };
        return false;
  	  });
	  },
	  validate : function(f){
	    var name = f.find("#txtNam").val();
	    var email = f.find("#txtEma").val();
	    var comment = f.find("#txtCom").val();
	    if(name!='' && email!='' && comment!=''){
	      if(email.indexOf("@")>0){
	        return true;
	      } else {
	        return false;
	      };
	    } else {
	      return false;
	    };
	  },
	  submitForm : function(f){
	    f.unbind('submit');
	    $("#frmError").fadeOut('fast');
	    var email = f.find("#txtEma").val();
	    email = email.split('@');
	    var mess = f.find("#txtCom").val().toString();
	    var options = {
	      cache : false,
	      type : "POST",
	      url : "common/mail/senm.php",
	      data : "nam="+f.find("#txtNam").val()+"&ema1="+email[0]+"&ema2="+email[1]+"&com="+mess,
	      success : function(r){
	        if(r=="true"){
	          TMT.com.behaviors.overlay({
  	          title : 'Your comment was sent',
  	          body : 'Thank you for telling us your thoughts!'
  	        });
	        } else {
	          TMT.com.behaviors.overlay({
  	          title : 'An error occured',
  	          body : 'We were unable to send your comment. Please try again.'
  	        });
	        };
	      },
	      error : function(e){}
	    };
	    $.ajax(options);
	  }
	}
};
$(function() {
	TMT.com.init();
});