$(document).ready(function(){
    $("#m_subscribe_yes").change(function(ev){
		if($(this).is(":checked"))
			$.post(base+'member/show_subscribe', {}, function(data){
			$("#show_subscribe_group").html(data).fadeIn();
			});
		else
			$("#show_subscribe_group").fadeOut('slow', function(){
			$(this).empty();
			});
	});
	cropper();
	gender_change();
	ajax_pagination_friend();
	ajax_pagination_friend2();
    ajax_pagination_friend_suggest();
});

function cropper(){
    $(".cropper").click(function(ev){
	ev.preventDefault();
	var caller=$(this);
	position=$(caller).offset();
	$("#cropper").remove();
	$("<div id=\"cropper\">Loading the Image Cropper....</div>").
	    appendTo('#wrapper').
	    css({'top': position.top-15+'px', 'left': '15px'}).
	    fadeIn('slow').
	    load(base+'member/cropper', function(){
		$("#uploaded_image").focus();
		$("#upload_init").click(function(ev){
		    $(this).siblings('p.error').remove();
		    ev.preventDefault();
		    var file;
		    $.ajaxFileUpload({
			url:base+'/member/uploader/',
			secureuri:false,
			fileElementId: 'uploaded_image',
			dataType: 'json',
			success: function (data, status){
			    if(data.error!= undefined){
				$("#uploader").append("<p class=\"error\">"+data.error_msg+"</p>");
			    }
			    else{
				$("select, iframe").hide(); // IE6 bug, always show dropdown on top of everything
				var image_path=path+data.path+data.file_name;
				$("#cropped_image").html('<img id="cropped" src="'+image_path+'">');
				$("#crop_engine").fadeIn()
				$("#preview_img").remove();
				$('<img src="'+image_path+'" style="position: relative;" id="preview_img">').appendTo("#cropped_photo");
				$("#cropped_filename").val(data.file_name);
				$("#origin_width").val(data.image_width);
				$("#origin_height").val(data.image_height);
				var ratio=$("#ratio");
				if($(ratio).length>0)
				    crop_ratio=$(ratio).val();
				else
				    crop_ratio="1:1";
				$('#cropped').imgAreaSelect({ aspectRatio: crop_ratio, onSelectChange: preview, minHeight: 100, minWidth:100, show: false, x1: 0, y1: 0, x2: 100, y2: 100, fadeSpeed: 1, handles: true});
				$("#crop_submit").submit(function(ev){
				    ev.preventDefault();
				    $.post($(this).attr('action'), {
					    'submit': 'DO',
					    'file': $("#cropped_filename").val(),
					    'x': $("#x1").val(),
					    'y': $("#y1").val(),
					    'w': $("#w").val(),
					    'h': $("#h").val()
					},
					function(data){
					    result=$.parseJSON(data)
					    if (!result.error){
						remove_cropper();
						$(caller).parents('.uploader').find('img').attr('src', path+'files/uploader/'+result.filename);
						$(caller).parents('.uploader').find('.filename').val(result.filename);
					    }
					    else{
						$("#cropped_image").find('.error').remove();
						$("#cropped_image").append(result.error_msg);
					    }
					}
				    );
				});
			    }
			},
			error: function (data, status, e){
			    alert(e);
			}
		    });
		});
		$("#crop_close").click(function(ev){
		    ev.preventDefault();
		    $('#cropped').imgAreaSelect({ remove: true});
		    remove_cropper();
		});
	    });
    });
}

function preview(img, selection) {
    var scaleX = 100 / (selection.width || 1);
    var scaleY = 100 / (selection.height || 1);
    $('#preview_img').css({
	width: Math.round(scaleX * $("#origin_width").val()) + 'px',
	height: Math.round(scaleY * $("#origin_height").val()) + 'px',
	marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
	marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
    }); 
    $('#x1').val(selection.x1);
    $('#y1').val(selection.y1);
    $('#x2').val(selection.x2);
    $('#y2').val(selection.y2);
    $('#w').val(selection.width);
    $('#h').val(selection.height);
}

function remove_cropper(){
    $('.imgareaselect-outer, .imgareaselect-border2, .imgareaselect-border1, .imgareaselect-selection').remove();
    $("#cropper").remove();
    $("select, iframe").show(); // IE6 bug, re-enable the dropdown after close the cropper
}

function checkvalid(){
    var url = base+'member/checkvalid';
	$.post(url,{ 	  
		   	  user_id:document.getElementById('user_id').value,
			  username:document.getElementById('m_username').value,
			  email:document.getElementById('m_email').value,
			  ran:Math.random()
			  },
			function(data){
			$('#action_checkvalid').html(data).fadeIn();
		});
}

function create_children(){
    var url = base+'member/create_children';
    var current=$(".child_wrap").length;
    var next=$("#num_children").val();
    if(next==0)
	$("#action_num_children").empty();
    else
    if(next>current){
	$.post(url, { 	 
		user_id:$("#user_id").val(),
		num_children:next-current,
		max_child: current,
		ran:Math.random()
	    },
	    function(data){
		$('#action_num_children').append(data);
		gender_change();
		cropper();
		$("#action_num_children .datepicker").datepicker({
		    changeMonth: true,
		    changeYear: true,
		    dateFormat: 'dd/mm/yy',
		    yearRange: '1950:c+5' 
		});
	    }
	);
    }
    else
	$(".child_wrap:gt("+(next-1)+")").remove();
}

function gender_change(){
    // users/temp_children/Pic-Child.jpg = Girl
    // users/temp_children/no_picture.jpg = Boy
    var base_img=path+'users/temp_children/';
    $(".m_sex").unbind("click").click(function(){
	var pic=$(this).parents('.child_info').siblings('.child_photo').find(".img_b img");
	//var part=$(".img_b_0 img").attr("src");
	if(pic){
	    if($(pic).hasClass('m_girl')){
		$(pic).removeClass('m_girl').addClass('m_boy');
		$(pic).attr('src', base_img+"no_picture.jpg");
	    }
	    else{
		$(pic).removeClass('m_boy').addClass('m_girl');
		$(pic).attr('src', base_img+"Pic-Child.jpg");
	    }
	}
    });
}

function ajax_pagination_friend(){
$("div.ajax_pagination_friend ul.pagination li a").unbind().click(function(ev){
	  url=ev.target.href;
	  $.post(url,{ 
			  id:document.getElementById('id').value,
		   	  keyword:document.getElementById('keyword').value,
			  ran:Math.random()
			  }, 
		function(data){
		  $('div.ajaxify_friend').html(data);
		  ajax_pagination_friend();
	  });
  	  ev.preventDefault();
	  ajax_pagination_friend();
	  })
}

function ajax_pagination_friend2(){
$("div.ajax_pagination_friend_g ul.pagination li a").unbind().click(function(ev){
	  url=ev.target.href;
	  $.post(url,{ 
			  id:document.getElementById('id').value,
		   	  group:document.getElementById('group').value,
			  ran:Math.random()
			  }, 
		function(data){
		  $('div.ajaxify_friend_g').html(data);
		  ajax_pagination_friend2();
	  });
  	  ev.preventDefault();
	  ajax_pagination_friend2();
	  })
}

function ajax_pagination_friend_suggest(){
$("div.ajax_pagination_friend_s ul.pagination li a").unbind().click(function(ev){ 
      url=ev.target.href;
      $.post(url,{ 
              id:document.getElementById('id_s').value,
                 page:document.getElementById('page_s').value,
              ran:Math.random()
              }, 
        function(data){
          $('div.ajaxify_friend_s').html(data);
          ajax_pagination_friend_suggest();
      });
        ev.preventDefault();
      ajax_pagination_friend_suggest();
      })
}

function find_friend(start){
    var url = base+'friend/find_friend/'+start;
	$.post(url,{ 	 
		   	  id:document.getElementById('id').value,
		   	  keyword:document.getElementById('keyword').value,
			  ran:Math.random()
			  },
			function(data){
			$('#show_find_friend').html(data).fadeIn();			 
		});
	return false;
}

function find_friend_group(){
    var url = base+'friend/find_friend_group';
	$.post(url,{ 	 
		   	  id:document.getElementById('id').value,
		   	  group:document.getElementById('group').value,
			  ran:Math.random()
			  },
			function(data){
			$('#show_friend_group').html(data).fadeIn();			 
		});
}

function add_menu(user_id,menu_id){
    var url = base+'menu/add_menu';
	$.post(url,{ 	 
		   	  user_id:user_id,
			  menu_id:menu_id,
			  ran:Math.random()
			  },
			function(data){
			$('#tab_menu_member').html(data).fadeIn();			 
		});
}

function remove_menu(user_id,menu_id){
    var url = base+'menu/remove_menu';
	$.post(url,{ 	 
		   	  user_id:user_id,
			  menu_id:menu_id,
			  ran:Math.random()
			  },
			function(data){
			$('#tab_menu_member').html(data).fadeIn();			 
		});
}

function this_show_menu(user_id,paht){
	$('#show_page').html('<img src="http://localhost/momchannel/images/loading2.gif" >')
    var url = base+paht;
	$.post(url,{ 	 
		   	  user_id:user_id,
			  ran:Math.random()
			  },
			function(data){
			$('#show_page').html(data).fadeIn();	
			bind_general();
		});
}

function vote(type,id,user_id,rating){
    var url = base+'rating/vote/';
	$.post(url,{ 	
		   	  type:type,
			  id:id,
			  user_id:user_id,
			  rating:rating,
			  ran:Math.random()
			  },
			function(data){
			$('#show_vote_'+type+id).html(data).fadeIn();			 
		});
}

function vote2(type,id,user_id){
    var url = base+'vote/add_vote/';
	$.post(url,{ 	
		   	  type:type,
			  id:id,
			  user_id:user_id,
			  ran:Math.random()
			  },
			function(data){
			$('#show_vote_'+type+id).html(data).fadeIn();			 
		});
}

function vote_contest(type,id,user_id,rating){
    var url = base+'rating/vote2/';
    $.post(url,{     
                 type:type,
              id:id,
              user_id:user_id,
              rating:rating,
              ran:Math.random()
              },
            function(data){
            $('#show_vote_'+type+id).html(data).fadeIn();             
        });
}





