var $ut = jQuery.noConflict();
$ut(document).ready(function() {
//png images
	$ut(function(){$ut(document).pngFix();});
//smooth scrolling within the same page
	$ut('a[@rel=smooth]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname && this.hash.replace(/#/,'') ) {
		var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
		var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($target) {
			var targetOffset = $target.offset().top;
				$ut(this).click(function() {
					$ut('html, body').animate({scrollTop: targetOffset}, 400);
					return false;
				});
			}
		}
	});
//fadeout the success message after a number of seconds
//	$ut(".info_success").fadeOut(10000);
	
	$ut('#header_button_signup').click(function() {
		window.location = "/signup.php";
	});
//click for dynamic content
	$ut('a[@rel=get_dc]').click(function() {	
	$ut('#loading_image').show();
		post_url = this.href+'&t=dc';
		$ut.ajax({
			type: "GET",
			url: post_url,
			cache: false,
			timeout: 5000,
			error: function(response) {
				displayErrorMessage();
			},
			success: function(response){
				setTimeout("finishAjax('dc_inner', '"+escape(response)+"')", 600);
			}
	    });
		return false; 				
	});

	$ut('a[@rel=close_dc]').click(function() {	
		$ut('div#dynamic_content').slideUp();
		$ut('#dc_inner').html("");
		return false;
	});
//login form
	$ut(document).ready(function($){
		$ut("#a_username").Watermark("Username");
		$ut("#a_password").Watermark("Password");
	});
//highlight
	$ut('tr.on_off').addClass("tr_off");
	$ut('tr.on_off').hover(function () {
		$ut(this).removeClass("tr_off");
		$ut(this).addClass("tr_on");
	}, function () {
		$ut(this).removeClass("tr_on");
		$ut(this).addClass("tr_off");
	});
	
	$ut('span.image_user_type').hover(function () {
		$ut(this).find('span[@class^=text_type]').show();
		 $ut(this).parent("a[@href*=view_profile]").css("cursor","pointer").click(function () {
		 	window.location.href=this.href;
		 });
	}, function () {
		$ut(this).find('span[@class^=text_type]').hide();
	});

//app_menu
	$ut("a[@rel=app_menu]").click(function () {
		if($ut('#loginbox_outer:visible').length==0){
			$ut('#loginbox_outer').slideDown();
			$ut(this).removeClass("app_open");
			$ut(this).addClass("app_close");
			$ut.cookie('appBar1', 'expanded', { path:'/member/' });
			$ut.cookie('appBar2', 'expanded', { path:'/user/' });
		}else{
			$ut(this).removeClass("app_close");
			$ut(this).addClass("app_open");
			$ut('#loginbox_outer').slideUp();
			$ut.cookie('appBar1', 'closed', { path:'/member/' });
			$ut.cookie('appBar2', 'closed', { path:'/user/' });
		}
		return false;
	});
	
	var appBar1 = $ut.cookie('appBar1');
	var appBar2 = $ut.cookie('appBar2');
	var appBar2 = $ut.cookie('appBar3');
	var appBar2 = $ut.cookie('appBar4');
    // Set the user's selection for the left column
    if((appBar1 == 'expanded') || (appBar2 == 'expanded')){
		$ut("a[@rel=app_menu]").removeClass("app_open");
		$ut("a[@rel=app_menu]").addClass("app_close");
		$ut.cookie('appBar1', 'expanded', { path:'/member/' });
		$ut.cookie('appBar2', 'expanded', { path:'/user/' });
		$ut('#loginbox_outer').show();
	};
//check all
	$ut("input[@name=check_all]").click(function(){
	var checked_status = this.checked;
		$ut("input[@id^=check]").each(function(){
			this.checked = checked_status;
		});
	});	
	
//todo list
	$ut("#add_todo_form").submit(function () {
		var ErrorMsg = "";
		var todo_val = $ut("#todo_content").val();
		if(todo_val.length>2){
			$ut("#todo_content").removeClass("input_invalid");
		}else{
			ErrorMsg = "You must enter something todo."
			$ut("#todo_content").addClass("input_invalid");
		}
		if(ErrorMsg.length>0){
			return false;
		}else{
			$ut('#loading_image').show();
			$ut.ajax({
				type: "POST",
				url: "/member/todo_functions.php",
				data: "todo="+todo_val+"&action=add",
				cache: false,
				timeout: 5000,
				error: function(response) {
					displayErrorMessage();
					$ut('#loading_image').hide();
				},
				success: function(response){
					$ut("#todo_content").val("");
					getTodoList();
				}
			});
			return false;
		}	
		
	});
	
	$ut("a[@id^=todo_]").livequery('click', function(event) {
		$ut('#loading_image').show();
		$ut.ajax({
			type: "POST",
			url: "/member/todo_functions.php",
			data: "todo="+this.id+"&action=update",
			cache: false,
			timeout: 5000,
			error: function(response) {
				displayErrorMessage();
				$ut('#loading_image').hide();
			},
			success: function(response){
				getTodoList();
			}
		});
		return false;
	});
	$ut("a[@id^=del_todo_]").livequery('click', function(event) {
		if(confirm("Are you sure you want to delete this task?")){
			$ut('#loading_image').show();
			$ut.ajax({
				type: "POST",
				url: "/member/todo_functions.php",
				data: "todo="+this.id+"&action=delete",
				cache: false,
				timeout: 5000,
				error: function(response) {
					displayErrorMessage();
					$ut('#loading_image').hide();
				},
				success: function(response){
					getTodoList();
				}
			});
			return false;
		}else{
			return false;
		}
	});
});

function getTodoList(){
	$ut.ajax({
		type: "POST",
		url: "/member/todo_functions.php",
		data: "action=display",
		cache: false,
		timeout: 5000,
		error: function(response) {
			displayErrorMessage();
		},
		success: function(response){
			response = unescape(response);
			var myResponse = response.split("|");
			var responseType = myResponse[0];
			var responseMsg = myResponse[1];
			$ut("#todos").html(responseMsg);
			$ut('#loading_image').hide();
		}
	});
}

//load dynamic content
function finishAjax(id, response){
  $ut('#loading_image').hide();
  $ut('div#dynamic_content').slideDown();
  $ut('#'+id).html(unescape(response));
  $ut('#'+id).show();
}

function decision(message, url){
	if(confirm(message)) location.href = url;
}

function displayErrorMessage(){
	$ut("#loading_image").hide();
	alert("Requested data could not be loaded, please try again.\n\nIf the problem continues, please contact us with details of what you were attempting to do.");
}

function gotoPageLink(pageLink){
	if(pageLink!=""){
		$target = $ut('a[@name=' + pageLink +']');
		if ($target.length) {
			var targetOffset = $target.offset().top;
			$ut('html,body').animate({scrollTop: targetOffset}, 1000);
			return false;
		}
	}
}

function validateDate(form_value) {
    var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    if (form_value.match(RegExPattern)== null) {
        return true; 
    } else {
        return false; 
    } 
}


function isNumeric(form_value){ 
    if(form_value.match(/^\d+$/) == null){
        return true; 
    }else{
		return false; 
	}
}