$(document).ready(function() {	

/******************* funds list *********************/
	// go back to full list
	$('#main-go-back').click(function(){
		location = g_params['mod_domain']+'/list/&clear_search';
	});

	$('.fun-list-row-det').click(function(){
		var url = g_params['base_domain']+g_params["non_def_lang"]+'/funds/one_fund/' + $(this).attr('fun_id') + '.htm';
		location = url;
	});
	
	
/******************* one fund *********************/

	
	// Add message click
	$('.fun-one-fund-det-btn-1-add-msg').click(function(){
		$('.fun-add-msg').css('display', ($('.fun-add-msg').css('display')=="none" ? "block" : "none"));
		$('.fun-add-msg-error').remove();
	});	
	
	// Add message hover
	$('.fun-one-fund-det-btn-1-add-msg').hover(function(){
		$(this).css('color', '#007eff');
	},function(){
  		$(this).css('color', '#002aff');
	});
	
	// view large photo
	$('.fun-one-fund-det-4-con-img img, .fun-one-fund-det-1-msg-img img').click(function(){
		$('.fun-one-fund-large-img').expose({
				api: true,
    			color: "#333333",
    	
				onBeforeClose: function(event) { 
        			$(".fun-one-fund-large-img").hide();
				}
			}).load();
		var new_src = $(this).attr('src').replace('icon/', '');
		$("#one_fund_large_img").attr('src', new_src);
		$("#one_fund_large_img").attr('width', '430');
		$(".fun-one-fund-large-img").css('display', 'block');
	})
	
	// close large media
	$('.fun-one-fund-large-img').click(function(){
		$('.fun-one-fund-large-img').expose().close();
	})

	// link to donate
	$('.fun-one-fund-det-btn-2-title-donate').click(function(){
		var my_fun = $(this).parent().parent().parent();
		var url = g_params['secure_domain']+g_params["non_def_lang"]+'/donate/funds/' + $(my_fun).attr('fun_id') + '/';
		location = url;
	})

	
	// link to help
	$('.fun-one-fund-det-btn-2-title-help').click(function(){
		$('.fun-add-msg').css('display', ($('.fun-add-msg').css('display')=="none" ? "block" : "none"));
		$('.fun-add-msg-error').remove();
	})
	
	// link to forum
	$('.fun-one-fund-det-btn-2-title-forum').click(function(){
		var my_fun = $(this).parent().parent().parent();
		var url = g_params['base_domain']+g_params["non_def_lang"]+'/funds/one_fund/' + $(my_fun).attr('fun_id') + '.htm&forum';
		location = url;
	})

	// print fund
	$('.fun-one-fund-det-btn-1-print').click(function(){
		var my_fun = $(this).parent().parent().parent();
		var url = g_params['base_domain']+g_params["non_def_lang"]+'/funds/one_fund/' + $(my_fun).attr('fun_id') + '.htm&print';
		window.open(url, 'print_funds','width=500,height=350,top=50,left=50,status=0,titlebar=0,scrollbars=1');
	})
	
	// send to friend
	$('.fun-one-fund-det-btn-1-send').click(function(){
		var my_fun = $(this).parent().parent().parent();
		var url = g_params['base_domain']+g_params["non_def_lang"]+'/_sys_app/send_friend/funds-' + $(my_fun).attr('fun_id') + '.htm';
		window.open(url, 'send_funds','width=500,height=350,top=50,left=50,status=0,titlebar=0,scrollbars=1');
	})
	
/******************* fund messages *********************/
	// set the color of td
	if ($('.fun-dl').size()){
		$('.fun-dl').each(function(i){
			$('dt', this).css('background', (i%2 ? '#EBF3F6' : '#fff'));
		});
	}
	// hover on each row
	$('.fun-dl dt').hover(function(){
		if ($(this).parent().attr('open')!='yes')
			$(this).css('background', '#DAE7ED');
	}, function(){		
		if ($(this).parent().attr('open')!='yes')
			$(this).css('background', ($(this).parent().attr('zugi')%2 ? '#fff' : '#EBF3F6'));
	});
	
	$('.fun-msg-row-title, .fun-msg-row-date').click(function(){
		var my_dl = $(this).parent().parent();
		// hide
		if ($('dd', my_dl).css('display')=='block'){
			$('dd', my_dl).hide();
			$(my_dl).attr('open', 'no');
			$('dt', my_dl).css('background', ($(my_dl).attr('zugi')%2 ? '#fff' : '#EBF3F6'));
		}
		else{
			$('dd', my_dl).show();
			
			if (!$(my_dl).attr('open')){
				// send ajax call to update DB with read message
				$('#gdata').val(my_dl.attr('fm_id'));
				$('#gtype').val("read_one");
				var data = $('#gdata, #gtype').serialize();
				$.post(g_params['mod_domain']+'/messages/'+my_fun_id+'/'+(nav_page ? nav_page+'.htm' : ''), data, function(msg){
					$('#new_messages').html($('#new_messages').html()-1);
					$('.fun-msg-sys-lbl').css('display', "block");
					setTimeout("$('.fun-msg-sys-lbl').hide();", 3000);
					
				});
			}
			$(my_dl).attr('open', 'yes');
			$('dt', my_dl).css('background', '#B8D8F1');
			$('span', $(this)).attr('class', 'fun-msg-row-title-lbl-r');
			$('img', $(this)).attr('src', '/_media/public/global/msg-r.gif');
		}
	});
	
	// close dd
	$('.fun-msg-row-det-2-icn-btn-close').click(function(){
		var my_dl = $(this).parent().parent().parent().parent().parent();
		$('dd', my_dl).hide();
		$('dt', my_dl).css('background', ($(my_dl).attr('zugi')%2 ? '#fff' : '#EBF3F6'));
	})

	// checkbox of rows
	var total_chk = $('.fun-msg-row-sign-chk').size();
	$('#check-all').click(function(){
		if ($(this).attr('checked')==true){
			$('.fun-msg-row-sign-chk').attr('checked', true);
			$('.fun-msg-btm-2-0, .fun-msg-btm-2-1, .fun-msg-btm-2-2, .fun-msg-btm-2-3, .fun-msg-btm-2-4').css({cursor:'pointer', color:'#444'});
		}
		else{
			$('.fun-msg-row-sign-chk').attr('checked', false);
			$('.fun-msg-btm-2-0, .fun-msg-btm-2-1, .fun-msg-btm-2-2, .fun-msg-btm-2-3, .fun-msg-btm-2-4').css({cursor:'default', color:'gray'});
		}
	});
	
	$('.fun-msg-row-sign-chk').click(function(){
		var chk_sign = $('.fun-msg-row-sign-chk[checked]').size();
		if (chk_sign==total_chk){
			$('#check-all').attr('checked', true);
		}
		else{
			$('#check-all').attr('checked', false);
		}
		if (chk_sign)
			$('.fun-msg-btm-2-0, .fun-msg-btm-2-1, .fun-msg-btm-2-2, .fun-msg-btm-2-3, .fun-msg-btm-2-4').css({cursor:'pointer', color:'#444'});
		else
			$('.fun-msg-btm-2-0, .fun-msg-btm-2-1, .fun-msg-btm-2-2, .fun-msg-btm-2-3, .fun-msg-btm-2-4').css({cursor:'default', color:'gray'});
			
	});
	
	// click on show sign only
	$('.fun-msg-btm-2-0, .fun-msg-btm-2-1, .fun-msg-btm-2-2, .fun-msg-btm-2-3, .fun-msg-btm-2-4').click(function(){
		if ($(this).css('cursor')=='pointer'){
			var arr = new Array(), i=0;
			$('.fun-msg-row-sign-chk[checked]').each(function(){
				arr[i++] = $(this).parent().parent().parent().attr('fm_id');
			});
			
			var type = $(this).attr('type')
			switch(type){
				case "open":
				case "close":
					$('.fun-msg-row-sign-chk[checked]').each(function(){
						var my_dl = $(this).parent().parent().parent();
						if(type=="open")
							$('dd', my_dl).show();
						else
							$('dd', my_dl).hide();
					});
					break;
				case "not_read":
				case "read":
				case "delete":
						$('#gdata').val(arr.join(' '));
						$('#gtype').val(type);
						var data = $('#gdata, #gtype').serialize();
						$.post(g_params['mod_domain']+'/messages/'+my_fun_id+'/'+(nav_page ? nav_page+'.htm' : ''), data, function(msg){
							fm_ids = $('#gdata').attr("value");
							fm_ids = fm_ids.split(" ");
							switch(type){
								case "delete":
										for(i=0; i<fm_ids.length; i++)
											$("#msg_"+fm_ids[i]).css('display', "none");
									break;
								case "read":	
										for(i=0; i<fm_ids.length; i++){
											$("#title_"+fm_ids[i]+" span").attr('class', "fun-msg-row-title-lbl-r");
											$("#title_"+fm_ids[i]+" img").attr('src', g_params['media_domain']+"/public/global/msg-r.gif");
										}
									break;
								case "not_read":
										for(i=0; i<fm_ids.length; i++){
											$("#title_"+fm_ids[i]+" span").attr('class', "fun-msg-row-title-lbl-n");
											$("#title_"+fm_ids[i]+" img").attr('src', g_params['media_domain']+"/public/global/msg-n.gif");
										}
									break;
							}
							
							$('#new_messages').html(msg);
							$('.fun-msg-sys-lbl').css('display', "block");
							setTimeout("$('.fun-msg-sys-lbl').hide();", 3000)	
						});
					break;
			}
		}
	});
});
