var loading = '<div id="loading"><img src="images/loading-icon.gif" alt=""/></div>';
$(function(){
	// close
	function close_box(){
		$("#btogether").empty().fadeOut("fast");
		$("#wrapoverlay").fadeOut("fast");
	}
	//Open popup mail
	$("#nyclickmail, .sendmail-top, .open-send-email, .open-send2contact").live("click",function(){
		var dates = new Date();
		var times = dates.getTime();
		var arr = new Array();
                var receiver_list = "";
		var opts = eval("(" + $(this).attr("rel") + ")");
			$.each(opts, function(i, n) {
			arr.push( i + '=' + n );
		});

                /*if(typeof(opts['username'])=="undefined"){
                    receiver_list = "";
		}else{
                    receiver_list = opts['username']+",";
		}
               if(typeof(opts['sent2contact'])=="undefined"){
                    //alert(opts['sent2contact']);
                    receiver_list = "";
		}else{
                    //alert(opts['sent2contact']);
                    receiver_list = opts['sent2contact']+",";
                    //alert(receiver_list);
		}
                */
		var frameWidth = opts['width'];
		var frameHeight = opts['height'];;
		var winWidth = $(window).width();
		var winHeight = $(document).height();
		var left = (winWidth/2) - frameWidth/2;
	  	var top = ($(window).height()/2) - frameHeight/2;
		var filepath = opts['url'];


		$("#wrapoverlay").css({
			"width":winWidth,
			"height":winHeight,
			"position":"absolute",
			"top":"0",
			"left":"0",
			"opacity": ".6",
			"filter": "alpha(opacity=60)",
			"-moz-opacity": ".6",
			"z-index":"10000"
		}).show();

		$("#btogether").css({
			"left":left,
			"top":top,
			"width":frameWidth,
			"height":frameHeight
		}).show().empty().html(loading);
               
		$.get(opts['url'],{times:times,draft_id:opts['draft_id'],sent2contact:opts['sent2contact']},function(req){ //alert(req);
			$("#btogether").html(req);
			//$("#TO").val(receiver_list+"");
		});
                
		return false;
	});

	$("#xx-close, #xx-close1").live("click",function(){
		close_box();
	});

	// SEND NOW
	$("#SEND-EMIAIL-BUTTON").live("click",function(){
		var DRAFT_ID = $("#DRAFT_ID").val();
		var data = $("#TOS");
		var TO = data.val();
		if(trim(data.val())==""){
			alert('Ange medlem Användarnamn!');
			data.focus();
			return false;
		}
		var data = $("#SUBJECT");
		var SUBJECT = data.val();
		if(trim(data.val())==""){
			alert('Ange ämne!');
			data.focus();
			return false;
		}
		var data = $("#DETAIL");
		var DETAIL = data.val();
		if(trim(data.val())==""){
			alert('Ange ämne!');
			data.focus();
			return false;
		}

		$.post("request/email.php",{TO:TO,SUBJECT:SUBJECT,DETAIL:DETAIL,OPTION:"SEND-NEW",DRAFT_ID:DRAFT_ID},function(req){
			if(req==1){
				alert('Ditt meddelande har skickats.');
				close_box();
			}
		});

	});


	// DRAFT
	$("#DRAFT-BUTTON").live("click",function(){
		var data = $("#TOS");
		var DRAFT_ID = $("#DRAFT_ID").val();
		var TO = data.val();
		if(trim(data.val())==""){
			alert('Ange medlem Användarnamn!');
			data.focus();
			return false;
		}
		var data = $("#SUBJECT");
		var SUBJECT = data.val();
		if(trim(data.val())==""){
			alert('Ange ämne!');
			data.focus();
			return false;
		}
		var data = $("#DETAIL");
		var DETAIL = data.val();
		if(trim(data.val())==""){
			alert('Ange ämne!');
			data.focus();
			return false;
		}

		$.post("request/email_draft_save.php",{TO:TO,SUBJECT:SUBJECT,DETAIL:DETAIL,DRAFT_ID:DRAFT_ID,OPTION:"SEND-DRAFT"},function(req){
			if(req==1){
				alert('Sparade');
				//close_box();
			}
		});
	});

	// Delete draft e-mail
	$(".delete-draft").live("click",function(){
		var hideOjbect=$(this);
		var arr = new Array();
		var opts = eval("(" + $(this).attr("rel") + ")");
			$.each(opts, function(i, n) {
			arr.push( i + '=' + n );
		});
		if(confirm("Är du säker??")){
			$.get("request/email_delete_draft.php",{ID:opts['id']},function(res){
				if(res==1){
					hideOjbect.parent().parent().next().remove();
					hideOjbect.parent().parent().fadeOut("slow",function(){
						hideOjbect.parent().parent().remove();
					});
				}
			});
		}
	});
	// Auto complete
	/*
        $("#TOS").autocomplete('request/search.php', {
		width: 300,
		multiple: true,
		matchContains: true,
		autoFill: true,
		formatItem: formatItem,
		formatResult: formatResult
	});

	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		//alert(row[1]);
		//return row[0].replace(/(<.+?>)/gi, '');
	}
        */
	//Open popup mail
	$("#REPLY-EMIAIL-BUTTON").live("click",function(){
               // close_box(); //return false;
                $("#btogether").empty().hide();
                $("#wrapoverlay").hide();
		var dates = new Date();
		var times = dates.getTime();
		var arr = new Array();
		var opts = eval("(" + $(this).attr("rel") + ")");
			$.each(opts, function(i, n) {
			arr.push( i + '=' + n );
		});
		if(typeof(opts['username'])=="undefined"){
			opts['username'] = "";
		}else{
			opts['username'] = opts['username']+",";
		}
		var frameWidth = opts['width'];
		var frameHeight = opts['height'];
		var winWidth = $(window).width();
		var winHeight = $(document).height();
		var left = (winWidth/2) - frameWidth/2;
	  	var top = ($(window).height()/2) - frameHeight/2;
		var filepath = opts['url'];


		$("#wrapoverlay").css({
			"width":winWidth,
			"height":winHeight,
			"position":"absolute",
			"top":"0",
			"left":"0",
			"opacity": ".6",
			"filter": "alpha(opacity=60)",
			"-moz-opacity": ".6",
			"z-index":"10000"
		}).show();

		$("#btogether").css({
			"left":left,
			"top":top,
			"width":frameWidth,
			"height":frameHeight
		}).show().empty().html(loading); //alert(opts['reply_id']); return false;
		$.get(opts['url'],{times:times,reply_id:opts['reply_id']},function(req){ //alert(req);
			$("#btogether").html(req);
			$("#TO").val(opts['username']+"");
		});
		return false;
	});
});


$(function(){
	var dates = new Date();
	var times = dates.getTime();

	// Change tab
	$(".tab-mail").live("click",function(){
		var $id=$(this).attr("id");
		if($id=="inbox"){
			$("#bottom-tab-mail").removeClass("bgTabTwo").addClass("bgTabOne");
		}else{
			$("#bottom-tab-mail").removeClass("bgTabOne").addClass("bgTabTwo");
		}
		$("#tab-menu li").removeClass("active");
		$(this).parent().addClass("active");

		// Reload tab data
		var filequery;
		var $id=$(this).attr("id");
		if($id=="inbox"){
			filequery = "request/email_inbox.php";
		}else
		if($id=="send"){
			filequery = "request/email_outbox.php";
		}else
		if($id=="draft"){
			filequery = "request/email_draft.php";
		}
		$("#invite_body").empty().html(loading);
		var page = 0;
		/*
		$.get(filequery,{page:page,times:times},function(req){ //alert(req);
			$("#invite_body").empty().html(req);
		});
		*/
		$.ajax({
		   type: "get",
		   url: filequery,
		   data: "page="+page+"&times="+times,
		   cache: false,
		   async: false,
		   success: function(req){
				$("#invite_body").empty().html(req);
		   }
		 });
	});



	// Click page
	$(".divPaginator a").live("click",function(){
		var filequery;
		var chkTab = $("#inbox").parent();
		if(chkTab.hasClass("active")){
			filequery = "request/email_inbox.php";
		}
		var chkTab = $("#send").parent();
		if(chkTab.hasClass("active")){
			filequery = "request/email_outbox.php";
		}
		var chkTab = $("#draft").parent();
		if(chkTab.hasClass("active")){
			filequery = "request/email_dreft.php";
		}

		var arr = new Array();
		var opts = eval("(" + $(this).attr("rel") + ")");
		$.each(opts, function(i, n) {
			arr.push( i + '=' + n );
		});

		var page = opts['page'];

		$("#invite_body").empty().html(loading);
		/*$.get(filequery,{page:page,times:times},function(req){ //alert(req);
			$("#invite_body").empty().html(req);
		});*/
		$.ajax({
		   type: "get",
		   url: filequery,
		   data: "page="+page+"&times="+times,
		   cache: false,
		    async: false,
		   success: function(req){
				$("#invite_body").empty().html(req);
		   }
		 });
	});

	// Delete e-mail
	$(".Slet-mail").live("click",function(){
		var arr = new Array();
		var opts = eval("(" + $(this).attr("rel") + ")");

		$.each(opts, function(i, n) {
			arr.push( i + '=' + n );
		});

		if(confirm("Bekräfta ta bort?")){
			var hideOjbect = $(this).parent().parent();
			$.get("request/delete_email.php",{ID:opts['ID'],who:opts['who'],times:times},function(req){  //alert(req);
				if(req==1){
					hideOjbect.next().remove();
					hideOjbect.fadeOut("slow",function(){
						hideOjbect.remove();
					});
				}
			});
		}
	});

	$(".del_append").live("click",function(){
		var tmp_name = $(this).children().attr("alt");
		var ob = $(this);
		if(confirm("Är du säker?")){
			$.get("request/deleting.php",{tmp_name:tmp_name},function(res){
				if(res){
					ob.parent().remove();
				}
			});
		}
	});


	// Init
	$("#bottom-tab-mail").addClass("bgTabOne");

	$("#invite_body").empty().html(loading);
	$.get("request/email_inbox.php",{times:times},function(req){ //alert(req);
		$("#invite_body").empty().html(req);
	});

	$(".star-click").live("click",function(){
		var id = $(this).next().val();
		var ob = $(this);
		$.get("request/star-click.php",{ID:id},function(res){ //alert(res);
			ob.attr("src",res);
		});
	});

});